Struts2安全性保证了服务器端指定资源不会轻易通过用户访问得到。
在tomcat中的conf文件中的tomcat-users.xml中添加角色,以及与角色相对应的用户及密码:
<span style="font-size:14px;"><tomcat-users>
<role rolename="tomcat"/>
<role rolename="manager"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="kit" password="kit" roles="tomcat,manager"/>
<user username="cat" password="cat" roles="manager"/>
</tomcat-users></span>
上面的这些角色例如"manager"可以通过部署描述文件来访问指定资源。元素security-constraint来保护相应资源,其子元素web-resource-collection用于列出受保护的资源,它的子元素有web-resource-name:一个资源标识符,必须要有的元素
url-pattern:与之URL模式相匹配的资源将受到保护
description:对资源进行描述的元素
http-method:指定哪些http方法收到限制,如http-method的值是GET,则get方法将受到限制
security-constraint零一子元素auth-constraint用于列出哪些角色有权访问指定资源,它的子元素role-name列出有权访问的角色,另一个子元素transport-guarantee有三个取值:NONE,INTEGRAL,CONFIDENTIAL。NONE表示传输不需要任何保护措施,INTEGRAL表示传输过程中数据不被篡改,CONFIDENTIAL表示传输过程必须进行加密。
security-role元素注册访问受保护资源的所有角色,其子元素role-name用法与上面的同名元素相同
login-config元素指定登陆方式,其子元素auth-method的取值有BASIC(基本方式),DIGEST(基于摘要方式),FORM(表单方式),CLIENT-CERT(基于客户证书方式),另一子元素realm-name提供一条提示消息。
<span style="font-size:14px;"><security-constraint>
<web-resource-collection>
<web-resource-name>Manager</web-resource-name>
<url-pattern>/manager.action</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>manager</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Text</realm-name>
</login-config>
<security-role>
<role-name>manager</role-name>
</security-role></span>
最后在Struts.xml中添加相应action
<!-- 基本方式用户验证(basic) -->
<action name="manager">
<!-- 安全验证成功后的页面 -->
<result>/WEB-INF/jsp/managerDetails.jsp</result>
</action>