【问题标题】:Java HttpHandler related issueJava HttpHandler 相关问题
【发布时间】:2015-12-11 05:07:19
【问题描述】:

我有一个像下面这样的 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);
    }

如何从处理程序重定向到错误页面?

【问题讨论】:

    标签: java wildfly undertow


    【解决方案1】:

    试试这个。

    public static void redirectTo(HttpServerExchange exchange, String uri) {
        exchange.getResponseHeaders().add(HttpString.tryFromString("Location"), uri);
        exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
        exchange.getResponseSender().close();
    }
    

    【讨论】:

    • 您可能需要解释在哪里输入代码并在之后向return 解释 - 否则可能会访问已关闭的响应并产生不必要的错误。
    • 它抛出了下面的异常。 java.lang.NoSuchMethodError: io.undertow.server.HttpServerExchange.setStatusCode(I)Lio/undertow/server/HttpServerExchange;
    • 我已经用undertow 1.3.9.Final版本对其进行了测试。如果它编译成功但在运行时出现异常。比您在运行时可能有不同的版本。你能告诉我实际的代码和堆栈跟踪吗
    • 如何在 Wildfly 上找到 undertow 的版本?
    • 你可以从这里更新你的wildfly版本mvnrepository.com/artifact/org.wildfly/wildfly-undertow/…
    【解决方案2】:

    对于我使用的处理程序重定向,RedirectHandelr:

    RedirectHandler redirectHandler = new RedirectHandler("yourErrorPageURL");
    redirectHandler.handleRequest(exchange);
    

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 2016-08-10
      相关资源
      最近更新 更多