我有一个HttpHandler,如下所示。
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
System.out.println("In the handler");
SecurityContext securityContext = exchange.getSecurityContext();
if(securityContext != null){
if(securityContext.isAuthenticated()){
if(somechecks() ) {
//redirect to login error page
}
}
}
}
next.handleRequest(exchange);
}
如何从处理程序重定向到错误页面?
试试这个。
public static void redirectTo(HttpServerExchange exchange, String uri) {
exchange.getResponseHeaders().add(HttpString.tryFromString("Location"), uri);
exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
exchange.getResponseSender().close();
}
对于我使用的Handlers重定向,RedirectHandelr:
RedirectHandler redirectHandler = new RedirectHandler("yourErrorPageURL");
redirectHandler.handleRequest(exchange);