2011-12-20 40 views
2

我正在開發一個帶有Java EE的Web項目,我希望某些JSP只能由某種類型的用戶訪問。我讀過使用web.xml描述符,我可以將一些資源的可見性設置爲「角色名稱」。但是如何在http會話中設置這個角色名?
例如,我的描述有:很熱定義web項目中的web.xml角色名稱

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>Access to Student pages</web-resource-name> 
     <url-pattern>/Student/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>Student</role-name> 
    </auth-constraint> 
    </security-constraint> 

在哪裏/我如何界定「學生」角色的名字嗎?

回答

2

這是您的應用程序服務器的工作。服務器將在認證之後將角色存儲在會話中(如果認證由服務器完成)。


web.xml - 在您的應用程序

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

<login-config> 
    <auth-method>BASIC</auth-method> 
</login-config> 

如何分配用戶/登錄到羅爾斯是服務器相關的,在這裏爲Tomcat一個非常簡單的例子:

tomcat-users.xml - 該文件是在你的服務器中,你必須擴展它!

<?xml version='1.0' encoding='utf-8'?> 
<tomcat-users> 
    <role rolename="tomcat"/> 

    <role rolename="Student"/> <!-- you have to define all roles --> 

    <user username="tomcat" password="tomcat" roles="tomcat"/> 

    <user username="myname" password="mypassword" roles="Student"/> <!-- you have to assign login and roles --> 
</tomcat-users> 
+0

你是如何得出結論OP是使用Tomcat?根據OP的問題歷史記錄,我可以發現,OP使用的是JBoss AS,它絕對不使用'tomcat-users.xml'文件。 – BalusC 2012-08-24 17:31:34

+0

@BalusC:你說得對,我沒有想到他使用的服務器。無論如何,所有Server/Servlet-Container的一般聲明都是真實的。 - 或者我錯了? – Ralph 2012-08-25 07:18:48

相關問題