配置 LoginInterceptor
@Service
public class LoginInterceptor implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(httpServletRequest,httpServletResponse);
}
@Override
public void destroy() {
System.out.println("完成了");
}
在 SecurityConfig里面配置
@Autowired
private LoginInterceptor myFilterSecurityInterceptor;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/authentication/require")
.loginProcessingUrl("/authentication/form")
.successHandler(myLoginAuthSuccessHandler).and()
.authorizeRequests().antMatchers("/authentication/require",
"/authentication/form",
"/**/*.js",
"/**/*.css",
"/**/*.jpg",
"/**/*.png",
"/**/*.woff2",
"/auth/*",
"/oauth/*",
"/login/**",
"/mylogout",
"/exit"
)
.permitAll()
.anyRequest().authenticated().and().anonymous().disable().exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login?error")).and()
.csrf().disable();
http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class);
}