WebSecurityConfigurerAdapter过期处理

1.从原方案继承WebSecurityConfigurerAdapter改为使用注解@EnableWebSecurity

2.过期以后原有的configure方法和AuthenticationManager也可能会出问题,建议使用新方案

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Autowired
    private AuthenticationConfiguration authenticationConfiguration;

    @Bean
    public AuthenticationManager authenticationManager() throws Exception{
        AuthenticationManager authenticationManager = authenticationConfiguration.getAuthenticationManager();
        return authenticationManager;
    }

    @Bean
    SecurityFilterChain filterChain(HttpSecurity http)throws Exception{
        return http.csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                //过滤接口
                .antMatchers("/user/login").anonymous()
                .anyRequest().authenticated()
                .and()
                .build();
    }


  /*  @Bean
    @Override
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors()
                .disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/user/login")
                .anonymous()
                .anyRequest()
                .authenticated();
    }*/
}

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Spring Security的登录接口中自定义一个过期时间过滤器,你可以按照以下步骤进行操作: 1. 创建一个过滤器类,实现`javax.servlet.Filter`接口。你可以命名为`ExpirationFilter`。 ```java import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class ExpirationFilter extends UsernamePasswordAuthenticationFilter { @Override protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException { UserDetails userDetails = (UserDetails) authResult.getPrincipal(); // 在这里获取用户登录成功的信息,包括过期时间 // 比较过期时间与当前时间,如果已过期则进行相应的处理 // 比如返回一个自定义的错误信息或者进行重新登录等操作 // 调用父类的方法,继续处理认证成功后的操作 super.successfulAuthentication(request, response, chain, authResult); } } ``` 2. 在Spring Security配置类中,将自定义的过滤器添加到登录接口的过滤器链中。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private ExpirationFilter expirationFilter; @Override protected void configure(HttpSecurity http) throws Exception { http .addFilterBefore(expirationFilter, UsernamePasswordAuthenticationFilter.class) .authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .and() .logout() .logoutUrl("/logout"); } } ``` 在上述配置中,通过`addFilterBefore`方法将`ExpirationFilter`添加到`UsernamePasswordAuthenticationFilter`之前,确保过期时间的检查在身份验证之后进行。 请注意,以上代码只是一个示例,你需要根据你的实际需求进行相应的修改和逻辑处理
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值