gpt4 book ai didi

java - 抛出异常时 Servlet 过滤器不工作

转载 作者:搜寻专家 更新时间:2023-10-31 08:03:47 25 4
gpt4 key购买 nike

我想做什么?

我正在尝试从服务器端生成新的带时间戳记的 token ,客户端可以在其后续请求中使用

我试过什么?

我有一个 Servlet 过滤器,它环绕着 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);
}
}

在我的一个端点中,我这样做了

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

我正在为所有基于 REST 的工作使用 RESTEasy

而且我还在使用 Seam REST 库将服务器异常映射到基于 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 {
}

问题?
- 当端点抛出异常时,回调永远不会返回到过滤器代码。
- 这甚至在我使用 try/catch/finally 时也是如此

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

- 但是我可以测试 IllegalArgumentException 基于 Seam REST 异常映射映射到 HTTP 400,但它永远不会返回到 SecurityFilter 代码以防服务器异常。

必需吗?
- 即使应用程序抛出异常,我也想生成服务器 token ,以便客​​户端可以使用它们
- 如果出现异常,我如何才能通过 SecurityFilter 路由我的响应?

最佳答案

我认为您应该为此目的使用自己的异常处理程序,它可以在 web.xml 中定义,如果出现异常,您应该在异常处理程序中而不是在过滤器中处理它。

您可以在文章"Servlets - Exception Handling" 中获得更多详细信息

关于java - 抛出异常时 Servlet 过滤器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18600355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com