2017-09-09 186 views
1

我正在使用過濾器來檢查用戶是否連接(令牌有效),如果令牌不是有效的我設置了一個名爲「錯誤」的屬性與錯誤的詳細信息,這裏是我的控制器彈簧引導控制器問題

@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]

回答

1

要麼第一chain.doFilter(request, response);之後加一個return語句 - 早日迴歸方法

   if (authHeader == null || !authHeader.startsWith("Bearer ")) { 
        AppError error = new AppError("0001","Invalid bearer token."); 
        request.setAttribute("error", error); 
        chain.doFilter(request, response); 

        // ADD a RETURN STATEMENT HERE 
       } 

OR(IF-ELSE階梯式教學法)

// START: MOVE THIS inside AN ELSE Block 

       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 ; 
       } 
// END: MOVE THIS inside AN ELSE Block 
1

else塊,其中該條件爲真:authHeader == null || !authHeader.startsWith("Bearer ")兩次調用chain.doFilter()

一旦處於該塊:

if (authHeader == null || !authHeader.startsWith("Bearer ")) { 
    AppError error = new AppError("0001","Invalid bearer token."); 
    request.setAttribute("error", error); 
    chain.doFilter(request, response); 
} 

而第二次在else塊的末尾:

else { 

    ... 

    chain.doFilter(req, res); 
} 

一旦足夠所以無論是從所述底部移除最後chain.doFilter(request, response)否則阻止或刪除該塊中其他位置的if和catch子句中的chain.doFilter(request, response)