Spring Security .permitAll()在升级到Spring Boot 2.0.2后不再有效

And*_*oke 5 java spring spring-security jwt spring-boot

我使用的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()
                .anyRequest().authenticated();

        // Custom JWT based security filter
        httpSecurity
                .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);

        // disable page caching
        httpSecurity.headers().cacheControl();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
        return new JwtAuthenticationTokenFilter(jwtTokenUtil);
    }
}

@Configuration
@Order(2)
public class ClientVersionSupportConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry
                .addInterceptor(versionCheckingFilter())
                .addPathPatterns("/**")
                .excludePathPatterns("/error"); // Some more endpoints omitted
    }

    @Bean
    public VersionCheckingInterceptor versionCheckingFilter() {
        return new VersionCheckingInterceptor();
    }
}
Run Code Online (Sandbox Code Playgroud)

}

注意.antMatchers("/auth/**").permitAll()行。/auth在没有JWT的情况下,应该可以访问端点,因为在用户尚未登录时尚未生成JWT。

在升级Spring Boot之前,它工作正常,但现在不起作用。登录尝试被检查JWT的过滤器拒绝。看起来好像.permitAll()没有使请求通过。/version/**也不起作用。从浏览器中找到它会显示错误页面。

我还尝试从配置中删除行,直到仍然存在:

httpSecurity
        .authorizeRequests()
                .antMatchers("/auth/**").permitAll()
Run Code Online (Sandbox Code Playgroud)

它没有帮助。您能否帮助恢复原始行为?

var*_*en_ 6

你有你的 api 的基本路径,例如/api

server.contextPath默认春天属性名称已更改为server.servlet.context-path

因此,如果您为 api 使用默认基本路径,您将无法在您期望的位置找到端点。除非你更新财产;)