@Override public Response toResponse(UnauthenticatedException exception) { ResponseBuilder builder = Response.status(Response.Status.UNAUTHORIZED); builder.header("WWW-Authenticate", HttpServletRequest.BASIC_AUTH + " realm=\"" + appName + "\""); if (exception.getMessage() != null) builder = builder.entity(exception.getMessage()).type("text/plain"); return builder.build(); }
@Override protected Response convert(UnauthenticatedException exception, String id) { return Response.status(Status.UNAUTHORIZED) .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_TYPE) .entity(exception.getMessage()) .build(); } }
@ExceptionHandler(UnauthenticatedException.class) @ResponseStatus(HttpStatus.FORBIDDEN) @ResponseBody public Response<Void> handleException(UnauthenticatedException e) { log.debug("{} was thrown", e.getClass(), e); ShiroExceptionHandler.remove(); shiroEventListener.afterLogin(currentUserName.get(),false,e.getMessage()); return new Response<>(HttpStatus.FORBIDDEN.value() + "", "当前用户未登录", null); }