Spring Security:按客户端类型(浏览器/非浏览器)启用/禁用CSRF
问题内容:
Spring Security文档说:
“当您使用CSRF保护时?我们的建议是对普通用户可以由浏览器处理的任何请求使用CSRF保护。如果您仅创建非浏览器客户端使用的服务,则可能需要禁用CSRF保护。”
如果“浏览器”和“非浏览器”客户端(例如第三方外部服务)都将使用我的服务,Spring
Security是否提供一种专门为某些类型的客户端禁用CSRF的方法?
问题答案:
我确定在Spring Security XML中可以做到这一点,但是由于我使用的是Java Config,因此这是我的解决方案。
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Configuration
@Order(1)
public static class SoapApiConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/soap/**")
.csrf().disable()
.httpBasic();
}
}
@Configuration
public static class WebApiConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginProcessingUrl("/authentication")
.usernameParameter("j_username")
.passwordParameter("j_password").permitAll()
.and()
.csrf().disable()
}
}
}