我正在使用過濾器來檢查用戶是否連接(令牌有效),如果令牌不是有效的我設置了一個名爲「錯誤」的屬性與錯誤的詳細信息,這裏是我的控制器彈簧引導控制器問題
@RestController
public class HomeController {
@RequestMapping(value = "secure/info", method = RequestMethod.POST)
public Object login(@RequestBody User user,@RequestAttribute(name="error") AppError error) {
if(error!=null) return error ;
return "information";
}
這裏是我的過濾器:
@Override
public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain)
throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) req;
final HttpServletResponse response = (HttpServletResponse) res;
final String authHeader = request.getHeader("authorization");
if ("OPTIONS".equals(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
chain.doFilter(request, response);
} else {
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
AppError error = new AppError("0001","Invalid bearer token.");
request.setAttribute("error", error);
chain.doFilter(request, response);
}
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) {
AppError error = new AppError("0002","Invalid token signature.");
request.setAttribute("error", error);
chain.doFilter(request, response);
}
catch (final ExpiredJwtException e) {
AppError error = new AppError("0003","Expired token.");
request.setAttribute("error", error);
chain.doFilter(request, response);
}
catch (final MalformedJwtException e) {
AppError error = new AppError("0004","Malformed token.");
request.setAttribute("error", error);
chain.doFilter(request, response);
//return ;
}
chain.doFilter(req, res);
}
}
這裏是例外,我得到:
java.lang.IllegalStateException:在 響應已提交 位於org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:472) 〜[tomcat-embed-core-8.5.16.jar:8.5.16之後無法調用sendError() ] 在org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotReadable(DefaultHandlerExceptionResolver.java:386) 〜[彈簧webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在.. 。 .... 在com.inconso.LoginFilter.doFilter(LoginFilter.java:67)[類/:NA]