【问题标题】:Apply HTTP basic authentication to jax ws (HttpSpiContextHandler) in embedded Jetty将 HTTP 基本身份验证应用于嵌入式 Jetty 中的 jax ws (HttpSpiContextHandler)
【发布时间】:2014-05-23 17:43:43
【问题描述】:

对于早期版本的 Jetty(pre 9)有一些类似的问题,但没有一个可以解决这个特定问题:

Server server = new Server();
System.setProperty("com.sun.net.httpserver.HttpServerProvider",
     JettyHttpServerProvider.class.getName()); 
JettyHttpServer jettyServer = new JettyHttpServer(server, true);
Endpoint endpoint = Endpoint.create(new SOAPService()); // this class to handle all ws requests
endpoint.publish(jettyServer.createContext("/service")); // access by path
server.start()

上面的简化代码示例显示了我发现的在 Jetty 和传入我的 jax-ws 服务的肥皂请求之间建立桥梁的唯一方法。所有设置都在没有 web.xml 的代码中,这是一个更大的解决方案的一部分,该解决方案具有用于不同目的(servlet 等)的多个上下文和连接。

我尝试向 jettyServer.createContext("/service",new handler()) 添加一个处理程序类,以查看是否可以执行标头提取来模拟基本身份验证,但它永远不会被执行。

我的问题是我找不到通过针对 Jetty 服务器的代码指定使用基本身份验证的方法。使用 ServletContextHandler 的 setSecurityHandler 方法很容易,并且适用于其他上下文,我只是不知道如何将这个概念用于 jax-ws 服务。 任何帮助将不胜感激。

附言SSL已经实现了,我只需要添加http基本认证。

【问题讨论】:

    标签: jetty jax-ws basic-authentication embedded-jetty


    【解决方案1】:

    对于其他可能遇到同样问题的人来说,这是我最终偶然发现的答案。

    final HttpContext httpContext = jettyServer.createContext("/service");
    com.sun.net.httpserver.BasicAuthenticator a = new com.sun.net.httpserver.BasicAuthenticator("") {
                    public boolean checkCredentials (String username, String pw) 
                    {
                        return username.equals("username") && pw.equals("password");
                    }
                };
    httpContext.setAuthenticator(a);
    endpoint.publish(httpContext);//access by path
    

    当然,您可以扩展 checkCredentials 以获得更复杂的东西,但这显示了基本的工作方法。

    【讨论】:

      猜你喜欢
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      相关资源
      最近更新 更多