6、Struts2拦截器实现权限控制

1、创建如下项目结果

6、Struts2拦截器实现权限控制

2、在com.entity包下创建

 package com.entity;

 public class User {
private String name;
private String pwd; public User() {
} public User(String name, String pwd) {
this.name = name;
this.pwd = pwd;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} @Override
public String toString() {
return "User [name=" + name + ", pwd=" + pwd + "]";
} }

User.java

3、在com.action包下创建LoginAction.java

 package com.action;

 import javax.servlet.http.HttpSession;

 import org.apache.struts2.ServletActionContext;

 import com.entity.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 登录的Action
* @author pc
*
*/
public class LoginAction extends ActionSupport {
private User user;
public String login(){
HttpSession session=ServletActionContext.getRequest().getSession(); //登录成功,将用户放入session作用域中
if(user!=null){
System.out.println("user:"+user);
//保存用户信息到Session中
session.setAttribute("user", user);
return SUCCESS;
}else{
return ERROR;
} }
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
} }

LoginAction.java

4、在com.interceptor包下创建LoginInteceptor.java

 package com.interceptor;

 import java.util.Map;

 import javax.servlet.http.HttpSession;

 import org.apache.struts2.ServletActionContext;

 import com.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* 权限验证检查拦截器
* @author pc
*
*/
public class LoginInteceptor extends AbstractInterceptor { @Override
public String intercept(ActionInvocation invocation) throws Exception {
//Map<String, Object> session=invocation.getInvocationContext().getSession();
HttpSession session=ServletActionContext.getRequest().getSession();
User user=(User)session.getAttribute("user");
System.out.println("loginInte:"+user); if(user==null){
//请求的Action
return invocation.invoke();
}else{
return Action.LOGIN; }
} }

LoginInteceptor.java

5、在src下创建struts.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" >
<struts>
<!--1. 中文乱码处理 -->
<constant name="struts.i18n.encoding" value="UTF-8"/> <package name="default" namespace="/" extends="struts-default">
<!--2. 配置所有拦截器的节点 -->
<interceptors>
<!-- 定义权限验证拦截器 -->
<interceptor name="myLogin" class="com.interceptor.LoginInteceptor"></interceptor> <!-- 定义拦截器栈 -->
<interceptor-stack name="myStack"> <!-- 引用自定义拦截器 -->
<interceptor-ref name="myLogin"/> <!-- 引用系统默认拦截器 -->
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors> <!-- 3.定义默认拦截器 -->
<default-interceptor-ref name="myStack"/> <!-- 4.定义全局结果 -->
<global-results>
<result name="login" type="redirect">/login.jsp</result>
</global-results>
<!-- 5.配置Action=明星 -->
<action name="login" class="com.action.LoginAction" method="login">
<result name="success">/index.jsp</result>
<result name="error">/error.jsp</result>
<!-- 引用拦截器==小工 -->
<interceptor-ref name="myStack"/>
</action>
</package>
</struts>

struts.xml

6、在WebRoot下创建login.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<center>
<fieldset style="width:300px;">
<legend>登录</legend>
<!-- 在浏览器直接请求页面,拦截器不会发生任何作用,
拦截器只针对于Action的请求发生作用
也就是login.action走拦截器,直接请求index.jsp不会走拦截器-->
<form action="login.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user.name"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user.pwd"/></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</fieldset>
</center>
</body>
</html>

login.jsp

7、在WebRoot下创建index.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<h1>操作成功</h1>
<s:if test="user.name eq 'holly'">
holly你来啦?
</s:if>
<s:else>
我不认识你?你是谁?
</s:else>
</body>
</html>

index.jsp

8、在WebRoot下创建error.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
操作失败!
</body>
</html>

error.jsp

9、编辑WebRoot下WEB-INF下web.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> </web-app>

web.xml

10、运行

6、Struts2拦截器实现权限控制

6、Struts2拦截器实现权限控制

当把login.jsp页面的form表单的action值改为index.jsp后,再看登录后的效果

6、Struts2拦截器实现权限控制

上一篇:VMware下ubuntu与Windows实现文件共享的方法


下一篇:C#依赖注入控制反转IOC实现详解