https://www.cnblogs.com/xz816111/p/8528896.html
https://segmentfault.com/a/1190000012560773
首先,特别感谢大家没有 DISS 我是伸手党,现在论坛问个问题特害怕这个了。
上边两篇文章,让我对 Spring Security 的运行,有了一个更完整一点的概念,有需要的可以看下。
然后我说一下,现在我的解决方案,因为这里有一些问题,还是需要大家的帮助。
// 用来处理抛出的 AuthenticationException
class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint
// 用来获取前端传过来的 Token,交由 AuthenticationProvider 校验。(重要)
class CustomAuthenticationFilter extends OncePerRequestFilter
// 用户装载 Token,和检出的用户 UserDetails
class CustomAuthenticationToken implements Authentication
// 校验 Token 的正确性
class CustomAuthenticationProvider implements AuthenticationProvider
- 下边是 class WebSecurityConfig extends WebSecurityConfigurerAdapter 配置
@Override
protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
.authenticationProvider(customAuthenticationProvider);
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests().anyRequest().authenticated();
httpSecurity.headers().cacheControl();
httpSecurity.csrf().disable();
httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
httpSecurity.addFilterAfter(new CustomAuthenticationFilter(authenticationManager()), AnonymousAuthenticationFilter.class);
httpSecurity.exceptionHandling().authenticationEntryPoint(new CustomAuthenticationEntryPoint());
}
@Override
public void configure(WebSecurity webSecurity) throws Exception {
webSecurity.ignoring()
.antMatchers(HttpMethod.OPTIONS, "/**")
.antMatchers("/auth/login")
super.configure(webSecurity);
}