• 【Spring Security】安全框架学习(十)


    4 自定义失败处理

    我们还希望在认证失败或者是授权失败的情况下也能和我们的接口一样返回相同结构的json,这样可以让前端能对响应进行统一的处理。要实现这个功能我们需要知道Spring Security的异常处理机制

    在Spring Security中,如果我们在认证或者授权的过程中出现了异常会被ExceptionTranslationFilter捕获到。在ExceptionTranslationFilter中会去判断是认证失败还是授权失败出现的异常。

    如果是认证过程中出现的异常会被封装成AuthenticationException然后调用AuthenticationEntryPoint对象的方法去进行异常处理。

    如果是授权过程中出现的异常会被封装成AccessDeniedException然后调用AccessDeniedHandler对象的方法去进行异常处理。

    所以如果我们需要自定义异常处理,我们只需要自定义AuthenticationEntryPoint和AccessDeniedHandler然后配置SpringSecurity即可。

    ①自定义实现类

    package handler;
    
    import ...
      
    @Component
    public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
        
        @Overrride
    	public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException{
        	
            ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED, "用户认证失败,请重新登陆");
            String json = JSON.toJSONString(result);
            //处理异常
            WebUtils.renderString(response,json);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    package handler;
    
    import ...
      
    @Component
    public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    
    	@Override
    	public void handle (HttpServletRequest request, HttpServletResponse 	response, AccessDeniedException accessDeniedException) throws IOException{
    		    	
            ResponseResult result = new ResponseResult(HttpStatus.FORBIDDEN, "您的权限不足");
            String json = JSON.toJSONString(result);
            //处理异常
            WebUtils.renderString(response,json);
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    ②配置给springSecurity

    上面的两个配置配一定要配置给Spring Security,也就是在SecurityConfig类中,在configure 添加过滤器。

    我们可以使用HttpSecurity对象的方法去配置。

    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
       
        @Autowired
    	private AuthenticationEntryPoint authenticationEntryPoint;
    
        @Autowired
    	private AccessDeniedHandler accessDeniedHandler;
    
        
    	@Overrride
    	protected void configure(HttpSecurity http) throws Exception {
    	
        	//之前的代码
        	...
    		//配置异常处理器
            http.exceptionHandling()
                //认证失败处理器
                .authenticationEntryPoint (authenticationEntryPoint)
                //授权失败处理器
    			.accessDeniedHandler(accessDeniedHandler);
    
                
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    【西瓜书】9.聚类
    C#WPF使转换器Converter使用实例
    什么是云手机?云手机有什么用?
    亚马逊卖家必看:测评补单提升店铺排名和转化率的有效方式
    LabVIEW 应用程序视窗始终置于顶层
    Java排序的那些事之sort方法的使用详解
    vue使用Echarts5实现词云图
    【无标题】
    【SA8295P 源码分析】103 - QNX DDR RAM 内存布局分析
    TensorFlow(R与Python系列第四篇)
  • 原文地址:https://blog.csdn.net/qq_38594872/article/details/126675155