由于
WebSecurityConfigurerAdapter使用强制命令方法,因此您可以注入
security.enable-csrf变量的值,并在变量为false时禁用CSRF。没错,我认为这应该可以立即使用。
@Configurationpublic class AuthConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Value("${security.enable-csrf}") private boolean csrfEnabled; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEnprer(new BCryptPasswordEnprer()); } @Override protected void configure(HttpSecurity http) throws Exception { super.configure(http); if(!csrfEnabled) { http.csrf().disable(); } }}我所做的是,在我激活了 dev spring概要文件时,在application.yml中将该变量设置为false ,尽管您也可以为此目的创建一个名为
nosecurity 的概要文件。它大大简化了此过程:
-– application.yml-
# Production configurationserver: port: ${server.web.port}admin.email: ${admin.email}#etc---spring: profiles: devsecurity.enable-csrf: false#other Development configurations我希望它能满足您的需求
2017年12月17日更新
根据Spring Boot成员的评论,此问题已在Spring的新版本中解决:我在Spring的版本中拥有此问题,
1.5.2.RELEASE但似乎在1.5.9.RELEASE版本(版本2之前的最新稳定版本)中已修复此问题。默认情况下 csrf
是禁用的,可以使用启用
security.enable_csrf: true。因此,可能的解决方案可能只是升级到version
1.5.9.RELEASE,然后再将主要版本升级到版本2,而该版本的体系结构可能会大不相同。



