Spring Security阻止公共休息服务

时间:2016-01-10 05:35:16

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

我对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)授予所有访问权限?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

感谢阿里你的建议工作正常。

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

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

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