2012-08-01 114 views
2

我想限制只有GET/Request/path/rest/*的域。所以我宣佈它在我的web.xml:在web.xml中聲明http-method不會阻止其他http方法

<security-constraint> 
    <web-resource-collection> 
    <web-resource-name>All</web-resource-name> 
    <url-pattern>/rest/*</url-pattern> 
    <http-method>GET</http-method> 
    </web-resource-collection> 
</security-constraint> 

<security-constraint> 
    <web-resource-collection> 
    <web-resource-name>Auth</web-resource-name> 
    <url-pattern>/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
    <role-name>Admin</role-name> 
    </auth-constraint> 
</security-constraint> 

然而,做一個POST請求到(例如)當「/ REST /添加」,那麼Web容器接受並提交POST請求。爲什麼?

回答

0

這是因爲第一個安全約束被第二個(順序問題)覆蓋,它沒有http方法(如果Web資源集合中沒有http方法元素,它會意味着所有的HTTP方法是允許的。)

這意味着塔這個代碼

<security-constraint> 
    <web-resource-collection> 
    <web-resource-name>All</web-resource-name> 
    <url-pattern>/rest/*</url-pattern> 
    <http-method>GET</http-method> 
    </web-resource-collection> 
</security-constraint> 

是沒用的

+0

當瀏覽到:localhost/section/a.html時,我不允許通過'/ *'在web.xml中聲明資源。但是,我可以通過'/ rest/*'聲明訪問'localhost/rest/myresource'而不進行身份驗證。所以代碼並不是完全無用的。 – nimo23 2012-08-01 10:42:13

+0

我沒有第二個http方法,但我限制了對已認證角色的訪問。 – nimo23 2012-08-01 10:44:14

+0

這是否意味着,我應該只改變兩個安全約束的順序? – nimo23 2012-08-01 10:44:57

0

安全約束不覆蓋。如果不同約束的url模式是相同的,則它們是可加的。在上面的例子中,url模式是不同的,所以第一個獲勝並且允許發佈帖子。

相關問題