2013-09-03 18 views
5

Ne yapmaya çalışıyorum?Servlet Filtresi, özel durum ortadan kaldırıldığında çalışmaz

Ben denedim ne o müşteri kendi sonraki isteği

kullanabileceğiniz sunucu tarafında yeni Timestamped belirteçleri üretmek çalışıyorum

?

Etrafa REST aramaları sarar ve

@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); 
     } 
    } 

benziyor ve benim uç noktaları birinde ben bütün REST bazlı çalışmalar için RESTEasy kullanıyorum

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

yapmak bir Servlet filtresi var.

ve ben de REST bazlı istisnalar

@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 { 
} 

Sorun sunucu istisnalar eşleştirmek için Seam REST kitaplığı kullanıyorum?
- uç nokta istisnayı atarken, geri arama asla filtre koduna geri dönmez.
- Ben ancak IllegalArgumentExceptionSeam REST istisna haritalama dayalı HTTP 400 eşleştirilmiş olduğunu test edebilirsiniz, ancak bu durumda SecurityFilter koduna geri döndü asla -

 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); 
      } 

şöyle ı try/catch/finally kullandığınızda bu olduğunu bile sunucu istisnaları.

Gerekli?
- İstemci bunları kullanabilmek için istisna (ler) eklediğinde bile sunucu belirteçleri oluşturmak istiyorum
- İstisnalar durumunda, yanıtımı SecurityFilter üzerinden nasıl yönlendirebilirim?

+2

1 daha fazla ayrıntı elde edebilirsiniz. –

cevap

0

Bu amaçla web.xml dosyasında tanımlanabilecek kendi istisnalar işleyicisini kullanmanız gerektiğini ve istisnai durumlarda bunun filtrede değil, istisna işleyicisinin içinde işlemesini gerektiğini düşünüyorum.

Eğer Eh soru soruldu maddeye "Servlets - Exception Handling"

+0

Bu, yukarıdaki kodumda 'ExceptionMapping' tarafından ele alınan şeydir – daydreamer