需要抛自定义异常的地方
@Autowired
private HttpServletResponse response;
public boolean verify(String token, String username, String secret) {
try {
Algorithm algorithm = Algorithm.HMAC256(secret);
JWTVerifier verifier = JWT.require(algorithm)
.withClaim("username", username)
.build();
DecodedJWT jwt = verifier.verify(token);
return true;
} catch (Exception exception) {
//这里在catch直接转发
try {
response.sendRedirect("/402");
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
然后controller层
@RestController
public class TestController {
@GetMapping("/402")
public ResponseBean a402() {
return new ResponseBean(999, "token校验失败了.请重新登录", null);
}
注意是get请求,路径别写错了,不然不好使.这样就可以避开源码里面的上层try catch 捕获了