2015-05-15 53 views
0

我想了解JAX-WS中的身份驗證,因此我使用JAX-WS webservice製作了一個小型Netbeans8.0.2/Glassfish4.1 Web應用程序,而且我試圖使其不公開,但僅供授權用戶使用。auth-constraint - 角色名 - 用戶 - 忽略

此WebService的web.xml文件中包含:

<security-constraint> 
     <web-resource-collection> 
      <web-resource-name>Fib Web Service</web-resource-name> 
      <url-pattern>/FibServiceWithAuth/*</url-pattern> 
      <http-method>*</http-method> 
     </web-resource-collection> 
     <auth-constraint> 
      <role-name>user</role-name> 
     </auth-constraint> 
    </security-constraint> 
    <login-config> 
     <auth-method>BASIC</auth-method> 
     <realm-name>file</realm-name> 
    </login-config> 

然而,當我讓正在使用這項服務, 它的工作原理,無需任何身份驗證另一個簡單的Web應用程序,在這裏看到:

http://kempelen.ii.fmph.uniba.sk:8080/FibApp/

我知道我應該連接到服務從JSF託管bean處理這個JSF頁面是這樣的:

package fibapp; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 
import javax.xml.ws.BindingProvider; 
import javax.xml.ws.WebServiceRef; 

@ManagedBean 
@RequestScoped 
public class FibBean 
{ 
    public FibBean() { } 

    int n; 
    String result; 

    public int getN() { return n; } 
    public void setN(int newN) { n = newN; } 

    public String getResult() { return result; } 
    public void setResult(String newResult) { result = newResult; } 

    @WebServiceRef(wsdlLocation = "http://kempelen.ii.fmph.uniba.sk:8080/FibServiceWithAuth/FibWithAuth?wsdl") 
    private FibWithAuth_Service fibService; 

    public void compute() 
    { 
     FibWithAuth fib = fibService.getFibWithAuthPort(); 
    // ((BindingProvider) fib).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "someuser"); 
    // ((BindingProvider) fib).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "somepass"); 
     result = fib.fib(n).toString(); 
    } 
} 

但即使這些用戶/傳遞行被註釋掉了,bean仍然會從web服務獲得結果。

請問缺少什麼?

回答

1

在看着你WSDL(在指定的託管bean的@WebServiceRef),該服務的終點是

<soap:address 
location="http://kempelen.ii.fmph.uniba.sk:8080/FibServiceWithAuth/FibWithAuth"/> 

這意味着你的web服務資源是/FibWithAuth

然而,你的web.xml <security-constraint>網址是

<url-pattern>/FibServiceWithAuth/*</url-pattern> 

我想你想改變這

<url-pattern>/FibWithAuth/*</url-pattern> 

如果你真的想安全約束添加到整個FibServiceWithAuth Web應用程序,那麼您的<security-constraint>網址格式將爲/*

最後,我想你也想改變

<http-method>*</http-method> 

<http-method>POST</http-method> 

,讓管理的bean可以通過GET請求訪問WSDL(根據您的@WebServiceRef註釋)無認證。

+0

優秀,非常感謝!現在將行註釋掉了,我得到了「com.sun.xml.ws.client.ClientTransportException:服務器發送了HTTP狀態碼401:未經授權」。但是,當添加的行,然後我得到「com.sun.xml.ws.client.ClientTransportException:服務器發送HTTP狀態代碼403:禁止」 - 這是否意味着我的用戶名/密碼不正確?我試圖用命令行「asadmin create-file-user」添加用戶。當試圖再次添加同一用戶時,它會報告「將用戶2ain131添加到文件領域文件失敗,用戶2ain131已存在。」還有什麼東西丟失? – Palo

+0

更新您的文章幷包含完整的堆棧跟蹤。不清楚的是,WSDL檢索是獲取403還是實際的服務調用。你是否遵循了僅有安全HTTP POST的最後一條建議? –

+0

是的,我更新了* POST並遵循了所有的建議,這讓我感動,但現在仍然停留在禁止之列。打開鏈接時可以看到堆棧跟蹤,輸入數字並單擊按鈕:http://kempelen.ii.fmph.uniba.sk:8080/FibApp/ – Palo