2

我正在寻找一种方法来强制客户端在尝试检索我的WSDL文件时使用 HTTP 基本身份验证。使用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:/// myserverHello/HelloService?wsdl),则不会要求我提供凭据,它只是加载,并且要求它应该受密码保护

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

任何指针?

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

4

2 回答 2

0

您的配置看起来正确。我想如果您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>
于 2013-04-05T08:48:41.723 回答
0

对于任何感兴趣的人,这仅在 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中删除安全约束

于 2013-06-10T15:45:40.780 回答