SpringSecurity源码-过滤器
重要过滤器说明
UsernamePasswordAuthenticationFilter:是我们最常用的用户名和密码认证方式的主要处理类,构造了一个UsernamePasswordAuthenticationToken对象实现类,将用请求信息封装为Authentication
BasicAuthenticationFilter…:将UsernamePasswordAuthenticationFilter的实现类UsernamePasswordAuthenticationToken封装成的 Authentication进行登录逻辑处理
AuthenticationManager
AuthenticationProvider
…
UsernamePasswordAuthenticationToken(Authentication的一个实现)对象,其实就是一个Authentication的实现,他封装了我们需要的认证信息。之后会调用AuthenticationManager。这个类其实并不会去验证我们的信息,信息验证的逻辑都是在AuthenticationProvider里面,而Manager的作用则是去管理Provider,管理的方式是通过for循环去遍历(因为不同的登录逻辑是不一样的,比如表单登录、第三方登录(qq登录,邮箱登录…)。换句话说 不同的Provider支持的是不同的Authentication)。在AuthenticationManager调用DaoAuthenticationProvider。而DaoAuthenticationProvider继承了AbstractUserDetailsAuthenticationProvider ,从而也就获得了其中的authenticate方法去进行验证。
ExceptionTranslationFilter:主要用于处理AuthenticationException(认证)和AccessDeniedException(授权)的异常
FilterSecurityInterceptor:获取当前 request 对应的权限配置**,**调用访问控制器进行鉴权操作
自定义过滤器链
测试的controller
HelloControlelr
@RestController
public class HelloCtr {
@RequestMapping("/hello")
public Map hello(@RequestParam("name") String name,
HttpServletRequest request){
// 模拟抛出异常
int i = 1/0;
System.out.println("name= "+name);
Map<String,String> result = new HashMap<>();
result.put("hello","world");
return result;
}
}
模仿 DefaultLogoutPageGeneratingFilter
实现 OncePerRequestFilter
@Component
public class MySecuirityOnceFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
System.out.println("MySecuirityOnceFilter.doFilterInternal() 方法 *************************************");
// 一定加上该方法 , 否者过滤不了
try {
filterChain.doFilter(httpServletRequest,httpServletResponse);
} catch (Exception e) {
// e.printStackTrace();
httpServletResponse.getWriter().println(e);
System.out.println("捕捉到了异常-----------------------------");
}
}
}
登陆后页面可以抛出异常
}
}
登陆后页面可以抛出异常
[外链图片转存中...(img-Ekg0740P-1655780159225)]