2013-09-03 8 views
5

무엇을하려고합니까?예외가 발생할 때 서블릿 필터가 작동하지 않습니다.

내가 시도 무슨 클라이언트가 자신의 후속 요청

에서 사용할 수있는 서버 측에서 새로운 타임 스탬프 토큰을 생성하기 위해 노력하고

?

나는 주위 REST 전화를 감싸고

@WebFilter(urlPatterns = "/rest/secure") 
public class SecurityFilter implements Filter { 

    private static final Pattern PATTERN = Pattern.compile(":"); 
    private static final Logger LOGGER = LoggerFactory.getLogger(SecurityFilter.class); 

    @Override 
    public void init(final FilterConfig filterConfig) throws ServletException { 
     //LOGGER.info("initializing SecurityFilter"); 
    } 

    @Override 
    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { 
     final HttpServletResponse httpServletResponse = (HttpServletResponse) response; 
     final String authToken = getAuthenticationHeaderValue((HttpServletRequest) request); 

     try { 
      validateAuthToken(authToken); 
     } catch (IllegalArgumentException tokenNotValidException) { 
      LOGGER.error("invalid token"); 
      httpServletResponse.sendError(401); 
     } 

     try { 
      chain.doFilter(request, response); 
     } catch (Exception e) { 
      LOGGER.error("exception: " + e.getMessage()); 
     }finally { 
      final String newAuthToken = generateNewAuthToken(authToken); 
      httpServletResponse.addHeader(AUTH_TOKEN, newAuthToken); 
      LOGGER.info("added new security token: " + newAuthToken); 
     } 
    } 

처럼 보이는 내 엔드 포인트 중 하나에 내가 모든 REST 기반 작업 RESTEasy을 사용하고

@PUT 
public Response updateUser() { 
    throw new IllegalArgumentException("just for test purposes"); 
} 

을 서블릿 필터가 있습니다.

와 나는 또한 REST 기반 예외

@ExceptionMapping.List({ 
     @ExceptionMapping(exceptionType = IllegalArgumentException.class, status = 400, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = PersistenceException.class, status = 400, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = ConstraintViolationException.class, status = 400, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = ValidationException.class, status = 400, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = NoResultException.class, status = 404, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = IllegalStateException.class, status = 406, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = NoClassDefFoundError.class, status = 404, useExceptionMessage = true), 
     @ExceptionMapping(exceptionType = UnsupportedOperationException.class, status = 400, useExceptionMessage = true), 
}) 
@ApplicationPath("/rest") 
public class MarketApplicationConfiguration extends Application { 
} 

문제로 서버 예외를 매핑하는 Seam REST 라이브러리를 사용하고 있습니다?
- 끝 점이 예외를 throw하면 콜백이 필터 코드로 반환되지 않습니다.
는 - 내가 그러나 IllegalArgumentExceptionSeam REST 예외 매핑을 기반으로 HTTP 400에 매핑되는 것을 테스트 할 수 있지만, 경우에 SecurityFilter 코드로 다시 반환되지 않습니다 -

 try { 
       chain.doFilter(request, response); 
      } catch (Exception e) { 
       LOGGER.error("exception: " + e.getMessage()); 
      }finally { 
       final String newAuthToken = generateNewAuthToken(authToken); 
       httpServletResponse.addHeader(AUTH_TOKEN, newAuthToken); 
       LOGGER.info("added new security token: " + newAuthToken); 
      } 

을 다음과 같이 내가 try/catch/finally를 사용하는 경우도있다 서버 예외.

필수 입력 사항입니다.
- 응용 프로그램에서 예외를 throw하여 클라이언트가 사용할 수 있도록 서버 토큰을 생성하려고합니다.
- 예외가 발생하면 SecurityFilter을 통해 내 응답을 라우팅 할 수 있습니까?

+2

+1 질문과 대답에 대한 자세한 내용을 확인할 수 있습니다. –

답변

0

나는이 목적을 위해 web.xml에 정의 될 수있는 예외 처리기를 사용해야한다고 생각하며, 예외가 발생하면 예외 핸들러 내에서 필터를 처리해야한다.

"Servlets - Exception Handling"

+0

위 코드에서'@ ExceptionMapping'이 처리하는 것입니다 – daydreamer