- 配置保护资源
1.<web-resource-collection>元素中的子标签为
<web-resource-name>用来标示一个资源
<description>用来对资源进行描述
<url-pattern> 用来定义一个URL模式,所有与这个URL模式相匹配的URL地址的资源都将受到保护
<http-method>用来定义受限制的HTTP请求方法。例如设置其值为POST,则POST请求方法将受到限制。
2.<auth-constraint>
<role-name> 允许访问受保护资源的角色
<description>描述角色
3.<user-data-constraint>包含<transport-guarantee>元素,用来定义数据传输的保护形式
INTEGRAL: 保证数据在传输过程中不被修改
CONFIDENTIAL:必须对传输数据进行加密
NONE:对数据不做任何额外保护
在web.xml中使用<security-constraint>标签将一些资源进行保护,将WebContent/admin/text.jsp资源进行保护。
<security-constraint>
<web-resource-collection>
<!-- 资源标示 -->
<web-resource-name>Admin</web-resource-name>
<!-- 描述 -->
<description>nobody but admin</description>
<!-- URL模式 -->
<url-pattern>/admin/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- 允许访问受保护资源的角色 -->
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<!-- 允许访问受保护资源的角色 -->
<security-role>
<role-name>admin</role-name>
</security-role>
定义用户登录方法是通过web.xml文件中使用<login-config>标签元素进行配置,其包含的元素子元素如下
<auth-method>:用来指定用户身份验收的方法。基本方式(BASIC)、基于摘要方式(DIGEST)、基于表单方式(FORM)、SSL方式和基于客户证书方式
<realm-name>:一条提示信息
<form-login-config>:用来指定一个登陆页面以及一个身份验证失败时的错误页面。该元素在<auth-method>元素值为FORM时使用才有意义。
- 采用BASIC基本身份验证方法
struts.xml配置如下
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8" />
<constant name="struts.custom.i18n.resources" value="TokenInterceptor" />
<package name="default" extends="struts-default">
<action name="test">
<result name="success">/admin/index.jsp</result>
</action>
</package>
</struts>
web.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<security-constraint>
<web-resource-collection>
<!-- 资源标示 -->
<web-resource-name>Admin</web-resource-name>
<!-- 描述 -->
<description>nobody but admin</description>
<!-- URL模式 -->
<url-pattern>/admin/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- 允许访问受保护资源的角色 -->
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin</web-resource-name>
<!-- <url-pattern>/test.action</url-pattern> <url-pattern>/login.action</url-pattern> -->
<url-pattern>*.action</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<!-- 允许访问受保护资源的角色 -->
<security-role>
<role-name>admin</role-name>
</security-role>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>HUIZHI</realm-name>
</login-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- 使用FORM基于表单方式进行身份验证
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>使用FORM验证</title>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<center>
<div>
<%@ include file="top.html" %>
</div>
<div>
<h3>用户登录</h3>
<!--action属性名必须为j_security_check
用户名的name必须是j_username
密码的name必须是j_password
-->
<form action="j_security_check" method="post" name="myForm">
用户名:<input type="text" name="j_username"/><br/>
密 码:<input type="password" name="j_password"/><br/>
<input type="submit" name="mySub" value="登录"/>
</form>
</div>
</center>
</body>
</html>
struts.xml中配置Action
<action name="login">
<result name="success">/success.html</result>
</action>
web.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<security-constraint>
<web-resource-collection>
<!-- 资源标示 -->
<web-resource-name>Admin</web-resource-name>
<!-- 描述 -->
<description>nobody but admin</description>
<!-- URL模式 -->
<url-pattern>/admin/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- 允许访问受保护资源的角色 -->
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin</web-resource-name>
<!-- <url-pattern>/test.action</url-pattern> <url-pattern>/login.action</url-pattern> -->
<url-pattern>*.action</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/fail.html</form-error-page>
</form-login-config>
</login-config>
<!-- 允许访问受保护资源的角色 -->
<security-role>
<role-name>admin</role-name>
</security-role>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
运行:http://localhost:8080/Demo13/login.action