我正在研究由纯jsps(scriptlets)编写的项目,而不使用任何框架。
jboss版本:jboss-as-7.1.0.Final
我现在正尝试在其上添加简单身份验证。因此,当用户尝试浏览jsps时,例如http://localhost/myContextPath/hello.jsp
,它需要先登录。
的web.xml
<security-constraint>
<web-resource-collection>
<web-resource-name>All Access</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>PUT</http-method>
<http-method>HEAD</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
的JBoss-web.xml中
<jboss-web>
<security-domain>other</security-domain>
</jboss-web>
standalone.xml([jboss_home] \ standalone \ configuration folder)
<subsystem xmlns="urn:jboss:domain:security:1.1">
<security-domains>
<security-domain name="other" cache-type="default">
<authentication>
<login-module code="UsersRoles" flag="required">
<module-option name="usersProperties" value="users.properties"/>
<module-option name="rolesProperties" value="roles.properties"/>
</login-module>
</authentication>
</security-domain>
<security-domain name="form-auth">
<authentication>
<login-module code="UsersRoles" flag="required">
<module-option name="usersProperties" value="users.properties"/>
<module-option name="rolesProperties" value="roles.properties"/>
</login-module>
</authentication>
</security-domain>
</security-domains>
</subsystem>
users.properties(置于webapp classes文件夹下)
user1=jboss7
roles.properties(置于webapp类文件夹下)
user1=Admin
经过所有这些修改后,我尝试浏览我的hello jsp。我像往常一样工作。没有身份验证,也没有例外。
我不确定我是否朝着正确的方向前进,或者安全约束是完全不同的事情。 请帮忙,谢谢!
答案 0 :(得分:10)
只需按照此article的步骤设置7.1的BASIC身份验证。
试试这个。
<security-constraint>
<web-resource-collection>
<web-resource-name>All Access</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>PUT</http-method>
<http-method>HEAD</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>ApplicationRealm</realm-name>
</login-config>
<security-role>
<role-name>user</role-name>
</security-role>
<jboss-web>
<security-domain>java:/jaas/other</security-domain>
</jboss-web>
如果您使用的是 ApplicationRealm ,则无需执行任何操作。
您可以使用jboss提供的工具将用户添加到 ApplicationRealm 。
从%JBOSS_HOME%/ bin 中。使用 add-user.bat(或)add-user.sh 工具。
C:\dev\jboss-eap-6.2\bin>add-user
What type of user do you wish to add?
a) Management User (mgmt-users.properties)
b) Application User (application-users.properties)
(a): b
Enter the details of the new user to add.
Using realm 'ApplicationRealm' as discovered from the existing property files.
Username : johngalt
Password :
Re-enter Password :
What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[ ]: user
About to add user 'johngalt' for realm 'ApplicationRealm'
Is this correct yes/no? yes
Added user 'johngalt' to file 'C:\dev\jboss-eap-6.2\standalone\configuration\application-users.properties'
Added user 'johngalt' to file 'C:\dev\jboss-eap-6.2\domain\configuration\application-users.properties'
Added user 'johngalt' with groups user to file 'C:\dev\jboss-eap-6.2\standalone\configuration\application-roles.properties'
Added user 'johngalt' with groups user to file 'C:\dev\jboss-eap-6.2\domain\configuration\application-roles.properties'
Is this new user going to be used for one AS process to connect to another AS process?
e.g. for a slave host controller connecting to the master or for a Remoting connection for server to server EJB calls.
yes/no? no
Press any key to continue . . .
C:\dev\jboss-eap-6.2\bin>
这对我有用
答案 1 :(得分:1)
这对我有用。 将其添加到web.xml
<security-constraint>
<web-resource-collection>
<web-resource-name>HtmlAuth</web-resource-name>
<description>application security constraints
</description>
<url-pattern>/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>PUT</http-method>
<http-method>HEAD</http-method>
<http-method>OPTIONS</http-method>
<http-method>TRACE</http-method>
<http-method>GET</http-method>
<http-method>POST</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>ApplicationRealm</realm-name>
</login-config>
<security-role>
<role-name>user</role-name>
</security-role>
然后添加或创建jboss-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<security-domain>java:/jaas/other</security-domain>
</jboss-web>
将用户名添加到jboss: C:\ JBoss的\的JBoss-EAP-6.2 \箱&gt;添加-user.bat
What type of user do you wish to add?
a) Management User (mgmt-users.properties)
b) Application User (application-users.properties)
(a): b
Enter the details of the new user to add.
Using realm 'ApplicationRealm' as discovered from the existing property files.
Username : testid
User 'testid' already exits, would you like to update the existing user password
and roles
Is this correct yes/no? yes
Password :
Re-enter Password :
What groups do you want this user to belong to? (Please enter a comma separated
list, or leave blank for none)[user]: user,Manager
Updated user 'testid' to file 'C:\jboss\jboss-eap-6.2\standalone\configuration\a
pplication-users.properties'
Updated user 'testid' to file 'C:\jboss\jboss-eap-6.2\domain\configuration\appli
cation-users.properties'
Updated user 'testid' with groups user,Manager to file 'C:\jboss\jboss-eap-6.2\s
tandalone\configuration\application-roles.properties'
Updated user 'testid' with groups user,Manager to file 'C:\jboss\jboss-eap-6.2\d
omain\configuration\application-roles.properties'
Is this new user going to be used for one AS process to connect to another AS pr
ocess?
e.g. for a slave host controller connecting to the master or for a Remoting conn
ection for server to server EJB calls.
yes/no? yes
To represent the user add the following to the server-identities definition <sec
ret value="TWF2ZXJpY2sjMDE=" />
Press any key to continue . . .
答案 2 :(得分:0)
我没有看到更改JBoss配置的要求,就好像您以后想要更改服务器容器一样,还需要更改配置。而不是使用会话对象来检查会话是否已存在。如果它没有将页面重定向到登录页面。 这将返回当前会话。
request.getSession();
在您的登录页面中,将当前会话设置为属性
session.setAttribute('sess',request.getSession());
使用此比较,用户将无法直接访问任何其他页面。
答案 3 :(得分:-1)