【问题标题】:Spring Security Blocking public rest serviceSpring Security 阻塞公共休息服务
【发布时间】:2016-01-11 03:28:05
【问题描述】:

我的 Spring Security 和我的公共 Rest 服务有点问题(只有我使用 POST)

我的 RestController 是:

@RestController
@RequestMapping(value="/public/rest/status")
Public class DoSomeThing {
    @RequestMapping(method=RequestMethod.GET)
    public String getStatus(){
        return "OK";
    }

    @RequestMapping(method=RequestMethod.POST)
    public String setON(){
        return "Done";
    }
}

我的 Spring Security 是:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private LogoutHandler logoutHandler;

    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
            .antMatchers("/public/**").permitAll()
            .antMatchers("/error/**").permitAll()
            .and()
        .authorizeRequests()
            .antMatchers("/adm/**").hasAnyRole(Role.ROOT,Role.ADM)
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .successHandler(authenticationSuccessHandler)
            .failureHandler(authenticationFailureHandler)
            .defaultSuccessUrl("/home",true)
            .permitAll()
            .and()
        .logout()
            .logoutSuccessUrl("/login")
            .permitAll()
            .addLogoutHandler(logoutHandler)
            .and()
         .exceptionHandling()
            .accessDeniedHandler(accessDeniedHandler);

    }
}

当我尝试通过 GET 访问我的 Rest 服务时,一切正常:

$curl -i -X GET localhost:8080/public/rest/status
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: text/plain;charset=UTF-8
Content-Length: 2
Date: Sun, 10 Jan 2016 05:23:10 GMT

但是当我尝试使用 POST 时,其余的都是拒绝

$curl -i -X POST localhost:8080/public/rest/status
HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Set-Cookie: JSESSIONID=57387564970B157C03310B6DAFE8E82B; Path=/; HttpOnly
Location: /error/denied
Content-Length: 0
Date: Sun, 10 Jan 2016 05:23:26 GMT

如何为我的 REST 服务(POST 和 GET)授予对所有人的访问权限?

【问题讨论】:

    标签: java spring rest spring-security spring-boot


    【解决方案1】:

    默认情况下,CSRF 保护是 ON,当您使用 Java Config 进行 spring 安全性时。您应该为POST 请求提供CSRF 令牌,或者通过添加以下配置禁用它:

    http
                    ...
                    .and()
                        .csrf().disable();
    

    【讨论】:

      【解决方案2】:

      感谢阿里,您的建议很好。

      在测试了这个建议后,我用代码修改了我的 WebSecurityConfig 类:

      and().csrf().ignoringAntMatchers("/public/**") ...
      

      使用此指令,spring security 将仅禁用我的公共映射。

      【讨论】:

        猜你喜欢
        • 2012-10-13
        • 2018-12-15
        • 1970-01-01
        • 2016-07-14
        • 2021-05-11
        • 1970-01-01
        • 2019-02-07
        • 2012-08-03
        • 2015-06-05
        相关资源
        最近更新 更多