
文章图片
SpringSecurity原理主要过滤器链SpringSecurity的功能主要是由一系列的过滤器链相互配合完成的 。 验证一个过滤器之后放行到下一个过滤器链 , 然后到最后 。
认证流程【显卡|SpringSecurity过滤器原理】
过滤器作用
- SecurityContextPersistenceFilter:会在每次请求处理之前从配置好的SecurityContextRepository中获取SecurityContext安全上下文信息 , 然后加载到SecurityContextHolder中 , 然后在该次请求处理完成之后 , 将SecurityContextHolder中关于这次请求的信息存储到一个“仓库”中 , 然后将SecurityContextHolder中的信息清除 , 例如在Session中维护一个用户的安全信息就是这个过滤器处理的 。
- DefaultLoginPageGeneratingFilter:如果没有配置自定义登录页面 , 那系统初始化时就会配置这个过滤器 , 并且用于在需要进行登录时生成一个登录表单页面 。
- BasicAuthenticationFilter:检测和处理http basic认证 。
- UsernamePasswordAuthenticationFilter:用于处理基于表单的登录请求 , 从表单中获取用户名和密码 。 默认情况下处理来自/login的表单action 。 从表单中获取用户名和密码时 , 默认使用的表单name属性值为username和password , 这俩个值也可以通过usernameParameter和passwordParameter在配置中自定义 。 这个过滤器在表单提交登录请求之时会起作用 。 那么假设现在采用SpringSecurity整合Jwt , 那么我需要配置一个Jwt登录认证类(继承BasicAuthenticationFilter或者继承OncePerRequestFilter都可以 , 因为BasicAuthenticationFilter继承了OncePerRequestFilter) , 重写过滤器方法 。 Jwt的token认证登录是需要在在采用用户名密码登录认证之前 , 所以在配置Jwt登录认证类的时候需要在UsernamePasswordAuthenticationFilter之前添加过滤器 。 //配置自定义过滤器 添加jwt登录授权过滤器//在过滤器UsernamePasswordAuthenticationFilter之前http.addFilterBefore(jwtAuthenticationFilterUsernamePasswordAuthenticationFilter.class);
- RequestCacheAwareFilter:用来处理请求的缓存 。
- SecurityContextHolderAwareRequestFilter:主要是包装请求对象request 。
- AnonymousAuthenticationFilter:检测SecurityContextHolder中是否存在Authentication对象 , 如果不存在则为其提供一个匿名Authentication 。
- SessionManagementFilter:管理Session的过滤器
- ExceptionTranslationFilter:捕获来自过滤器链的所有异常 , 并进行处理 。 但是只处理两类异常:AccessDeniedException和AuthenticationException 异常 , 其他的异常会继续抛出 。 如果捕获到的AuthenticationException , 那么将会使用其对应的AuthenticationEntryPoint的commence()方法处理 。 在处理之前 , ExceptionTranslationFilter先使用RequestCache将当前的HTTPServletRequest的信息保存起来 , 方便用户登录成功后可以跳转到之前的页面 。 可以自定义AuthenticationException的处理方法 。 需要实现AuthenticationEntryPoint接口 , 然后重写commence()方法 。 /** * 当未登录或者token失效时访问接口自定义的返回结果 */@ComponentpublicclassRestfulAuthorizationEntryPointimplementsAuthenticationEntryPoint{@Overridepublicvoidcommence(HttpServletRequest request HttpServletResponse response AuthenticationException e)throwsIOException ServletException { response.setCharacterEncoding(\"utf-8\"); response.setContentType(\"application/json\"); PrintWriter writer = response.getWriter(); RespBean bean = RespBean.error(\"请先登录!\"); bean.setCode(401); writer.write(newObjectMapper().writeValueAsString(bean)); writer.flush(); writer.close();如果捕获的AuthenticationDeniedException , 那么将会根据当前访问的用户是否已经登录认证做不同的处理 , 如果未登录 , 则会使用关联的AuthenticationEntryPoint的commence()方法进行处理 , 否则将使用关联的AccessDeniedHandler的handle()方法进行处理 。 可以进行自定义AuthenticationDeniedException的处理方法 。 需要实现AccessDeniedHandler接口 , 然后重写handle()方法 。 @ComponentpublicclassRestfulAccessDeniedHandler implements AccessDeniedHandler { @Overridepublicvoid handle(HttpServletRequestrequest HttpServletResponseresponse AccessDeniedException e) throws IOException ServletException {response.setCharacterEncoding(\"utf-8\");response.setContentType(\"application/json\"); PrintWriter writer =response.getWriter(); RespBeanerror= RespBean.error(\"权限不足 , 联系管理员!\"); writer.write(newObjectMapper().writeValueAsString(error));error.setCode(403); writer.flush(); writer.close();
- 显卡|这就是RTX 4090 Ti显卡?又厚又重,超过三槽要1200W电源
- 显卡|3个不买RTX 3080的理由:没钱只能排最后
- 显卡|如何组装旗舰游戏电脑?这里有你想要的答案
- 等等党就是不买,黄牛、JS贷款买显卡囤货,如今着急了
- 显卡|Intel独立显卡首发全新接口:暂时只能残血
- Intel|Intel两款工作站显卡曝光:还是“小家伙”
- 保安过滤器滤芯 过滤器型号:HFU640UY060J 华源大流量滤元
- 显卡|购机别犹豫,vivo S15系列满足你的用机需求,入手更划算
- 显卡|全球十大畅销手机榜更新:三星S22 Ultra排名第5,榜首实至名归
- 显卡|好价不常有!DDR5、液金散热、满血显卡都有,高端游戏本这款最值
