2010-05-19 95 views
2

如果用戶未登錄,如何阻止訪問該網站?基於Web的基於Java登錄

在web.xml>安全性我檢查了表單身份驗證,然後我選擇了登錄和錯誤頁面,但我不知道如何阻止訪問並將用戶重定向到登錄頁面。

我需要過濾器嗎?如果是這樣,我怎麼能得到我指定的登錄網址?

我該如何調用驗證方法?我在一些例子中看到這個代碼

<form method=post action="j_security_check"> 
    <input type="text" name="j_username" /> 
    <input type="password" name="j_password" /> 
    </form> 

它是做什麼的?

回答

3

要防止未登錄的用戶查看資源,請使用安全約束。這樣的事情:

<security-constraint> 
    <display-name>Constraint</display-name> 
    <web-resource-collection> 
     <web-resource-name>all-resources</web-resource-name> 
     <description/> 
     <url-pattern>/pages/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <description/> 
     <role-name>User</role-name> 
    </auth-constraint> 
</security-constraint> 
+0

它的工作,謝謝!如果你知道你可以編輯答案,給我一個例子,我應該怎麼稱呼我的用戶驗證方法,重定向和東西......? – BrunoLM 2010-05-19 23:18:32

+0

實際上,j_security_check的作用取決於您使用的應用程序服務器。如果你想自己創建,你需要閱讀JAAS: http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html#JAASRealm 注意有很多web那些已經準備好迎接你的框架,而不是一大堆非學術的理由來重新詮釋JAAS。 – Affe 2010-05-19 23:40:12