如何在获取WSDL(服务器端)时强制执行基本身份验证



我正在寻找一种方法来强制客户端使用HTTP基本身份验证时,它试图检索我的WSDL文件。使用JAX-WS,我创建了以下web服务,我使用GlassFish 3:

@WebService(serviceName = "Hello")
@Stateless
public class HelloService {
    @WebMethod(operationName = "sayHello")
    @RolesAllowed("myrole")
    public String sayHello(@WebParam(name = "name") @XmlElement(required=true) String name){
        return "Hello "+name;
    }
}

在谷歌周围,似乎添加一个安全约束的web.xml描述符应该照顾到这一点,所以我做了

<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<security-constraint>
    <display-name>HelloSC</display-name>
    <web-resource-collection>
        <web-resource-name>HelloRC</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
        <http-method>HEAD</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>myrole</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>
<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>file</realm-name>
</login-config>

现在,当我部署并将浏览器指向http://myserver/Hello时,浏览器要求凭据。此外,sayHello方法只能使用正确的凭据。

问题:到目前为止一切顺利,但如果我将浏览器指向WSDL (http://myserver/Hello/HelloService? WSDL),我不会要求凭证,它只是加载,这是一个要求,它应该是密码保护

我的理解是url模式也应该适用于WSDL。毕竟这是一个GET请求…

指针吗?

编辑:所以我将.war部署到JBoss实例,它按预期工作。GlassFish似乎缺少一些配置

您的配置看起来正确。我想,如果您在新浏览器中打开http://example.org/Hello/HelloService?wsdl(或关闭当前浏览器的所有窗口,再次打开它),您将需要执行基本身份验证。这里的问题是,在第一次成功的身份验证之后,浏览器在每个请求中发送基本身份验证头,请求由服务器进行身份验证。

在注释后添加请尝试添加额外的http-method

<http-method>DELETE</http-method>
<http-method>PUT</http-method>
<http-method>HEAD</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>

对于任何感兴趣的人来说,这只有在在GlassFish 3特定的描述符中添加了安全约束后才能工作: meta - inf/glassfish-ejb-jar.xml :

<glassfish-ejb-jar>
  <enterprise-beans>
    <ejb>
      <ejb-name>HelloService</ejb-name>
      <webservice-endpoint>
        <port-component-name>Hello</port-component-name>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
      </webservice-endpoint>
    </ejb>
  </enterprise-beans>
</glassfish-ejb-jar>
然后从web.xml中删除安全约束

最新更新