[英]Spring boot @ExceptionHandler isn't catching relevant exception
I'm building an API using Spring Boot. 我正在使用Spring Boot构建API。 I've setup an authentication filter to grab a token from the request header and authenticate it.
我已经设置了一个身份验证过滤器,以从请求标头中获取令牌并对其进行身份验证。 If a user couldn't be found using the token, then an InvalidWSUserException is thrown.
如果找不到使用令牌的用户,则抛出InvalidWSUserException。
I've created a class to hold a number of @ExceptionHandlers so that I can catch the InvalidWSUserException and respond with a JSON string advising the requesting client that the token in their request was invalid. 我创建了一个用于容纳多个@ExceptionHandlers的类,以便可以捕获InvalidWSUserException并使用JSON字符串进行响应,以通知发出请求的客户端其请求中的令牌无效。
I've had this working on another project, but I seem unable to get it working in this new project. 我曾经在另一个项目上工作过,但似乎无法在新项目中使用它。
The InvalidWSUserException is empty as the Handler should handle the response; InvalidWSUserException为空,因为处理程序应该处理响应;
public class InvalidWSUserException extends RuntimeException {
}
I've defined the handler class as so; 我已经定义了处理程序类;
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler(value=InvalidWSUserException.class)
public ResponseEntity invalidWSUserException(InvalidWSUserException exception) {
return new WSResponse().send(HttpStatus.UNAUTHORIZED, "Invalid token.");
}
}
I have had a problem in a previous project where the class that was throwing the exception hadn't been instantiated by Spring via a Bean, but I've checked over this class, and the class that is raising the exception is instantiated by an @Autowired annotation. 我在以前的项目中遇到了一个问题,在那个项目中,引发异常的类尚未由Spring通过Bean实例化,但是我检查了此类,而引发异常的类则由@实例化。自动装配注释。
I'm struggling to understand why the @ExceptionHandler isn't doing what I think it should. 我正在努力理解为什么@ExceptionHandler没有按我的预期去做。
To confirm, the output I see in the browser is an error 500 json response; 确认一下,我在浏览器中看到的输出是错误500 json响应; where as it should be the EntityResponse from the @ExceptionHandler
应该是@ExceptionHandler中的EntityResponse
{
"timestamp": 1470833849289,
"status": 500,
"error": "Internal Server Error",
"exception": "digital.sheppard.exceptions.InvalidWSUserException",
"message": "No message available",
"path": "/api/product"
}
I'm working with JSON Web Token for Java library, and, as you put above, I had the same error in authentication service. 我正在使用用于Java库的JSON Web令牌 ,并且如上所述,在身份验证服务中出现了相同的错误。 I've tried with @ControllerAdvice, but had no solution.
我已经尝试使用@ControllerAdvice,但是没有解决方案。
After a long research, I've found a solution. 经过长时间的研究,我找到了解决方案。 You have to implement your
JwtFilter
, and, instead of launching an exception, you have to modify your HttpServletResponse
value in order to add the following: 您必须实现
JwtFilter
,而不是启动异常,而必须修改HttpServletResponse
值以添加以下内容:
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
//throw new UnauthorizedErrorException(UnauthorizedErrorException.ErrorCodes.INVALID_TOKEN);
response.addHeader("Content-Type", "application/json");
response.getWriter().print(getErrorJson());
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
final String token = authHeader.substring(7);
try {
final Claims claims = Jwts.parser().setSigningKey("secretkey").parseClaimsJws(token).getBody();
request.setAttribute("claims", claims);
} catch (final SignatureException e) {
//throw new UnauthorizedErrorException(UnauthorizedErrorException.ErrorCodes.INVALID_TOKEN);
response.addHeader("Content-Type", "application/json");
response.getWriter().print(getErrorJson());
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
chain.doFilter(req, res);
As you can see, I've commented the exception launch and I change it for the response header override. 如您所见,我已经注释了异常启动,并为响应标头覆盖对其进行了更改。
getErrorJson()
returns a JSON value as a string (Or you can only put an string as your body). getErrorJson()
返回一个JSON值作为字符串(或者您只能将字符串作为正文)。
I hope that this works for you. 我希望这对您有用。
PD: I've follow this instructions in order to implement auth. PD:我已按照以下说明进行身份验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.