Spring Security:按客户端类型(浏览器/非浏览器)启用/禁用CSRF [英] Spring Security: enable / disable CSRF by client type (browser / non-browser )
问题描述
Spring Security documentation says:
当您使用CSRF保护时?我们的建议是使用CSRF 保护浏览器可以处理的任何请求 普通用户.如果您仅创建供以下人员使用的服务 非浏览器客户端,您可能希望禁用CSRF保护."
"When you use CSRF protection? Our recommendation is to use CSRF protection for any request that could be processed by a browser by normal users. If you are only creating a service that is used by non-browser clients, you will likely want to disable CSRF protection."
如果浏览器"和非浏览器"客户端(例如第三方外部服务)都将使用我的服务,Spring Security是否提供一种专门为某些类型的客户端禁用CSRF的方法?
What if my service is going to be used by both "browser" and "non-browser" clients such as third party external services, does Spring Security provide a way to disable CSRF exclusively for certain type of clients?
推荐答案
我确定在Spring Security XML中可以做到这一点,但是由于我使用的是Java Config,因此这是我的解决方案.
I am sure there is a way to do this in Spring Security XML, but since I am using Java Config, here is my solution.
@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()
}
}
}
这篇关于Spring Security:按客户端类型(浏览器/非浏览器)启用/禁用CSRF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!