防止用户访问每个 wsdl 元素

Prevent users from accessing every wsdl element

我有一个包含大量不同操作的 SOAP Web 服务。我试图阻止所有用户访问所有这些。我试图通过编辑 web.xml 文件安全约束来完成此操作。

假设我的服务有操作 1、2 和 3。我有 user1 和 user2。我希望 user1 能够访问 1、2 和 3。user2 应该只能访问 3。我设置了两个具有不同角色的安全约束。

     <security-constraint>
        <web-resource-collection>
        <web-resource-name>SOAP Service</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>DELETE</http-method>
            <http-method>POST</http-method>
            <http-method>PUT</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>user1</role-name>
        </auth-constraint>
    </security-constraint>

    <security-constraint>
        <web-resource-collection>
        <web-resource-name>SOAP Service</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>DELETE</http-method>
            <http-method>POST</http-method>
            <http-method>PUT</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>user2</role-name>
        </auth-constraint>
    </security-constraint>

这工作正常,允许 user1 和 user2 访问 wsdl 公开的所有内容,因此我试图通过更改 url-pattern 进一步限制 user2 可以做什么。

<security-constraint>
    <web-resource-collection>
        <web-resource-name>SOAP Service</web-resource-name>
        <url-pattern>/3/*</url-pattern>
        <http-method>DELETE</http-method>
        <http-method>POST</http-method>
        <http-method>PUT</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>user2</role-name>
    </auth-constraint>
</security-constraint>

不幸的是,这不起作用。有人告诉我这是因为这是一个 soap 请求,所有请求都在同一个 url???所以我现在被困住了。允许某些用户只能访问部分 wsdl 的最佳方法是什么?

不得不做大量的研究,因为我对网络服务的了解还不够。最后,我拥有的是驻留在 tomcat 容器中的 Web 服务,我使用 web.xml 中配置的基本身份验证和 tomcat-users.xml 中的用户。 web.xml 看起来像

<security-constraint>
    <web-resource-collection>
        <web-resource-name>SOAP Service</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>DELETE</http-method>
        <http-method>POST</http-method>
        <http-method>PUT</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>serviceUsers</role-name>
    </auth-constraint>
</security-constraint>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>SOAP Service</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>DELETE</http-method>
        <http-method>POST</http-method>
        <http-method>PUT</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>otherUsers</role-name>
    </auth-constraint>
</security-constraint>

<security-role>
    <role-name>serviceUsers</role-name>
</security-role>

<security-role>
    <role-name>otherUsers</role-name>
</security-role> 

这会阻止任何人访问整个服务,除非他们具有 serviceUsers 或 otherUsers 角色。我想做的是阻止每个操作。为此,我必须编辑我的 serviceEndpointImpl。我添加了什么

@Resource
private WebServiceContext context;

if (context.isUserInRole("webServiceUsers")) { 
   //do whatever that user should be able to do or block them.
}

这样做是根据用户使用的 tomcat-user.xml 用户名和密码获取连接到您的服务的用户的角色。使用这个我能够将身份验证保持在容器级别,但仍然拒绝用户访问我不希望他们使用的方法。