【Java】统一异常处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Mr_EvanChen/article/details/80696872

场景如下:

 @RequiresPermissions(value={"version:delete","version:delete2"},logical = Logical.OR)
    @GetMapping("/delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable String id){
        versionService.deleteVersion(id);
        return success();
    }

从打印的信息可以看出,当前用户aaa,具有admin的角色,该角色具有[add, query, version:list, version:release, update, delete, version:edit]的权限,但其中并没有version:delete,因此会报错AuthorizationException。

用户: aaa 具有的角色: [admin]
用户: aaa 具有的权限: [add, query, version:list, version:release, update, delete, version:edit]
2018-06-14T18:31:23.180+0800 WEBERROR 8852 --- [nio-8890-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.shiro.authz.UnauthorizedException: Subject does not have permission [version:delete]] with root cause

org.apache.shiro.authz.AuthorizationException: Not authorized to invoke method: public com.yealink.microservice.core.result.Result com.yealink.version.controller.VersionController.delete(java.lang.String)
	at org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor.assertAuthorized(AuthorizingAnnotationMethodInterceptor.java:90)
	at org.apache.shiro.authz.aop.AnnotationsAuthorizingMethodInterceptor.assertAuthorized(AnnotationsAuthorizingMethodInterceptor.java:100)

需求:

项目中可能会有很多的方法都会加上权限注解,当当前角色没有该方法的权限标识时都会抛出这个异常,那么我们自然希望能够对异常进行捕获,并且最好能够对这个异常进行统一的处理,比如返回一个提示的页面,或者返回一点信息给前端。

解决办法:

新建统一异常处理类,@ControllerAdvice注解定义全局异常处理类,@ExceptionHandler注解声明异常处理方法。现在就可以在该类中定义各种不同异常的处理方法。

package com.yealink.version.exception;

import com.yealink.microservice.core.controller.BaseController;
import com.yealink.microservice.core.result.Result;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by yl1794 on 2018/6/14.
 */
@ControllerAdvice
public class ExceptionAdvice extends BaseController{

    /**
     * 全局捕获AuthorizationException异常,并进行相应处理
     */
    @ExceptionHandler({AuthorizationException.class})
    @ResponseBody
    public Result handleException(Exception e){
//        e.printStackTrace();
        System.out.println("AuthorizationException--------------");
        return success("AuthorizationException");
    }

}

优化后结果:

再次执行delete方法,虽然还是没有权限,但此时已经不报错了,从打印结果来看,确实捕获到了该异常。

用户: aaa 具有的角色: [admin]
用户: aaa 具有的权限: [add, version:list, query, version:release, update, delete, version:edit]
AuthorizationException--------------

猜你喜欢

转载自blog.csdn.net/Mr_EvanChen/article/details/80696872