踩坑记录
springboot整合shiro的时候爆出的错误。
原因是认证授权的时候出错了。shiro抛出了错误。
但是好像springboot没有处理,自己定义全局异常捕获也没有处理。(没看源码我也不知道是不是,)
解决办法
加一个类,去捕获异常。具体如下
import com.llgz.hotelmanageapi.common.CommonResult;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Bernie_fang
* @Description: spring boot 异常处理:主要处理shiro注解抛出的异常,统一返回异常结果(json数据)给前端验证
* @create 2021/7/23 17:18
**/
@RestControllerAdvice //异常处理的注解
public class BaseExceptionHandler {
/**
*处理未授权的异常
*/
@ExceptionHandler(value = AuthorizationException.class)
public CommonResult returnErrorMsg(HttpServletRequest request, HttpServletResponse response, AuthorizationException exception){
return CommonResult.errorResponse("您没有权限",403);
}
/**
* 处理未登录的异常
*/
@ExceptionHandler(value = UnauthenticatedException.class)
public CommonResult returnUnLoginErrorMsg(HttpServletRequest request, HttpServletResponse response, UnauthenticatedException exception){
return CommonResult.errorResponse("您还未登录",401);
}
/**
* 处理运行时异常
*/
@ExceptionHandler(value = Exception.class)
public CommonResult returnRunningException(Exception e){
System.out.println(e.getMessage());
return CommonResult.errorResponse(e.getMessage());
}
}
加上再次运行就可以了。