【发布时间】:2018-01-18 01:45:22
【问题描述】:
我有一个使用 ReactJS + Spring Boot/Social/Security 构建的 Web 应用程序。
我想确保当我发布它时,我受到 CSRF 的保护。我正在使用大部分开箱即用的 Spring Security,下面是我的基本 configure() 覆盖:
@Configuration
@Order (SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login/facebook").permitAll()
.antMatchers("/logout").permitAll()
.antMatchers("/api/**").authenticated()
.and().csrf().disable();
}
}
我的问题是,由于我对后端的所有 (/api/**) 请求仅在经过身份验证后才被允许,这是否可以保护我免受 CSRF 的影响?
需要明确的是,当用户通过 Spring Social 插件向 Facebook 进行身份验证时,我会将调用重定向到后端应用程序中的端点 (/login/facebook) 以处理成功登录。在这种方法中,我执行以下操作:
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(theUser.getFacebookId(), null, null);
SecurityContextHolder.getContext().setAuthentication(authentication);
这确保对 /api 的任何后续请求都将被识别为已通过身份验证。
【问题讨论】:
标签: spring spring-boot spring-security csrf-protection spring-social-facebook