2016-07-06 71 views
0

我有一個SOAP Web服務,其中包含一堆不同的操作。我試圖阻止所有用戶都可以訪問它們。我試圖通過編輯web.xml文件安全約束來實現這一點。防止用戶訪問每個wsdl元素

可以說我的服務有操作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暴露,然後我試圖進一步限制哪些用戶2可以通過改變url-pattern的事。

<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> 

不幸的是,這是行不通的。我被告知這是因爲它是一個肥皂請求,所有請求都在同一個網址上?所以我現在卡住了。允許某些用戶只能訪問wsdl的一部分的最佳方法是什麼?

回答

0

不得不做很多研究,因爲我對Web服務的理解不夠深入。最後,我所擁有的是駐留在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用戶名和密碼的用戶的角色。使用這個我能夠保持身份驗證在容器級別,但仍拒絕用戶訪問我不希望他們使用的方法。