我使用的Web应用程序向移动应用程序公开了REST API。我将Spring Boot版本从1.5.3.RELEASE升级到2.0.2.RELEASE,并修复了一些重大更改后,我面临的是我无法解决的更改。
我遵循了《Spring Boot 2.0迁移指南》和《Spring Boot Security 2.0》,并研究了Spring Boot 2.0 M4中的安全性更改。
问题在于该应用程序使用JWT身份验证,并且存在一个端点(/ auth / login)接受用户凭据并生成一个长期存在的JWT作为回报。
有一个过滤器检查客户端发送的JWT令牌,并确定客户端是否可以访问请求的资源。
自定义安全配置如下:
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfiguration {
@Configuration
@Order(1)
public class AuthenticationConfiguration extends WebSecurityConfigurerAdapter {
// Some dependencies omitted
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
// we don't need CSRF because JWT token is invulnerable
.csrf().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
// don't create session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers(HttpMethod.GET, "/version/**").permitAll()
// Some more antMatchers() lines omitted
.antMatchers("/auth/**").permitAll() …Run Code Online (Sandbox Code Playgroud)