首页 > 解决方案 > Spring Security 不适用于其他帖子网址

问题描述

对于这个问题,我没有找到令人满意的答案。

我有一个安全配置,到目前为止一直运行良好。

我想再添加一个POST网址,所有人都可以访问。

虽然其他排除的网址运行良好,但添加的额外添加的网址不起作用。

我的代码:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors()
        .and()
        .csrf().disable()
        .authorizeRequests()
        .antMatchers(HttpMethod.POST, "/ws/**").authenticated()
        .antMatchers(HttpMethod.DELETE, "/**").authenticated()
        .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
        .antMatchers(HttpMethod.GET, "/ws/getEvents").permitAll()// ---> While this is working
        .antMatchers(HttpMethod.POST, "/ws/persons/createNotificationsSubscriber*").permitAll()// -->this not working
        .anyRequest().authenticated()
        .and()
        .logout()
        .logoutSuccessUrl("http://localhost:3006/eventsMainView")
        .and()
        .csrf().disable()
        .httpBasic();
}

标签: javaspringspring-bootspring-security

解决方案


这里的问题是

.antMatchers(HttpMethod.POST, "/ws/**").authenticated()

表示使用 POST 请求验证从/ws开始的所有 URL,但

.antMatchers(HttpMethod.POST,"/ws/persons/createNotificationsSubscriber*").permitAll() // --> this not working

这从相同的/ws开始,它是一个 POST 请求,所以 Spring 不允许这样做

做你的工作,请使用这个-

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors()
        .and()
        .csrf().disable()
        .authorizeRequests()
        .antMatchers(HttpMethod.POST, "/ws/persons/createNotificationsSubscriber*").permitAll()// --> This will work
        .antMatchers(HttpMethod.POST, "/ws/**").authenticated()
        .antMatchers(HttpMethod.DELETE, "/**").authenticated()
        .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
        .antMatchers(HttpMethod.GET, "/ws/getEvents").permitAll()// ---> While this is working
        
        .anyRequest().authenticated()
        .and()
        .logout()
        .logoutSuccessUrl("http://localhost:3006/eventsMainView")
        .and()
        .csrf().disable()
        .httpBasic();
}

推荐阅读