小编joh*_*kis的帖子

如何将已弃用的 WebSecurityConfigurerAdapter 迁移到 SecurityFilterChain?

正如他们在这里描述我们的那样,该遗嘱WebSecurityConfigurerAdapter很快就被弃用了。

由于我想实现 JWT 模式,我尝试重构WebSecurityConfigurerAdapterwith的实现。SecurityFilterChain我面临的主要考虑因素是配置返回无效。

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    CustomAuthenticationFilter customAuthenticationFilter = new CustomAuthenticationFilter(authenticationManagerBean(), accessTokenExpiredInDays, refreshTokenExpiredInDays, jwtSecret);
    customAuthenticationFilter.setFilterProcessesUrl("/api/login");
    http
        .csrf().disable();
    http
        .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    http
        .authorizeRequests()
            .antMatchers("/error").permitAll();
    http
        .authorizeRequests()
            .antMatchers("/api/login/**", "/api/token/refresh/**").permitAll();
    http
        .authorizeRequests()
            .anyRequest().authenticated();
    http
        .addFilter(customAuthenticationFilter);
    http
        .addFilterBefore(new CustomAuthorizationFilter(jwtSecret), UsernamePasswordAuthenticationFilter.class);
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception{
    return super.authenticationManagerBean();
}
Run Code Online (Sandbox Code Playgroud)

java spring spring-security

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

spring ×1

spring-security ×1