Java Code Examples for org.apache.shiro.authz.UnauthorizedException#getMessage()

The following examples show how to use org.apache.shiro.authz.UnauthorizedException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GlobalExceptionHand.java    From spring-boot-shiro with Apache License 2.0 5 votes vote down vote up
/**
 * 403 - Unauthorized
 */
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(UnauthorizedException.class)
public Response handleLoginException(UnauthorizedException e) {
    String msg = e.getMessage();
    log.error("用户无权限:", e);
    return new Response().failure(HttpStatus.FORBIDDEN, "用户无权限!", null);
}
 
Example 2
Source File: UnauthorizedExceptionMapper.java    From onedev with MIT License 5 votes vote down vote up
@Override
  public Response toResponse(UnauthorizedException exception) {
ResponseBuilder builder;
if (!SecurityUtils.getSubject().isAuthenticated()) {
	builder = Response.status(Response.Status.UNAUTHORIZED);
	builder.header("WWW-Authenticate", HttpServletRequest.BASIC_AUTH + " realm=\"" + appName + "\"");
} else {
	builder = Response.status(Response.Status.FORBIDDEN);
}
  	if (exception.getMessage() != null)
  		builder = builder.entity(exception.getMessage()).type("text/plain");
  	
  	return builder.build();
  }
 
Example 3
Source File: GlobalExceptionHandler.java    From wetech-admin with MIT License 5 votes vote down vote up
@ExceptionHandler(UnauthorizedException.class)
public Result handleUnauthorizedException(HttpServletRequest request, UnauthorizedException e) {
    Map<String, String> regexMap = new HashMap<>();
    regexMap.put("Subject does not have permission (\\S+)", "操作失败,用户不存在权限$1,请检查权限配置");
    String message = e.getMessage();
    for (Map.Entry<String, String> entry : regexMap.entrySet()) {
        if (message.matches(entry.getKey())) {
            message = message.replaceAll(entry.getKey(), entry.getValue());
            break;
        }
    }
    log.error("execute method exception error.url is {}", request.getRequestURI(), e);
    return Result.failure(CommonResultStatus.UNAUTHORIZED, message);
}
 
Example 4
Source File: ExceptionAdvice.java    From ShiroJwt with MIT License 2 votes vote down vote up
/**
 * 单独捕捉Shiro(UnauthorizedException)异常
 * 该异常为访问有权限管控的请求而该用户没有所需权限所抛出的异常
 * @param e
 * @return
 */
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(UnauthorizedException.class)
public ResponseBean handle401(UnauthorizedException e) {
    return new ResponseBean(HttpStatus.UNAUTHORIZED.value(), "无权访问(Unauthorized):当前Subject没有此请求所需权限(" + e.getMessage() + ")", null);
}
 
Example 5
Source File: DataControllerAdvice.java    From notes with Apache License 2.0 2 votes vote down vote up
/**
 * @Author fruiqi
 * @Description  处理没有权限异常
 * @Date 1:20 2019/3/9
 * @Param [e, request]
 * @return com.infervision.model.ResponseExceptionBody
 **/
@ExceptionHandler(UnauthorizedException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public ResponseExceptionBody handleUnauthException(UnauthorizedException e,HttpServletRequest request){
    return new ResponseExceptionBody(e.getMessage(),request);
}