Struts2自定义登录验证拦截器
2016-06-11 00:00
716 查看
Controller.UserAction.java
Controller.UserInterceptor.java
struts.xml
index.jsp
User/UserLog.jsp用户登录结果页面
User/UserCenter.jsp用户中心页面
User/UserLogOut.jsp用户注销页面
package Controller; import java.util.Map; import com.opensymphony.xwork2.ActionContext; public class UserAction { private String username; private String pass; //|POST 外部注入 UserLog方法! public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } //|登录页 public String UserLogin() { return "success"; } //|登录结果页 public String UserLog() { System.out.println(this.pass); System.out.println(this.username); if(this.username.equals("admin")&&this.pass.equals("admin")) { ActionContext act=ActionContext.getContext(); Map<String,Object> sessionMap=(Map<String,Object>)act.getSession(); sessionMap.put("username", "admin"); return "success"; } return "failure"; } //|用户中心页面 public String UserCenter() { return "success"; } //|用户注销 public String UserLogOut() { ActionContext act=ActionContext.getContext(); Map<String,Object> sessionMap=(Map<String,Object>)act.getSession(); //|清空session(注销) sessionMap.clear(); return "success"; } }
Controller.UserInterceptor.java
package Controller; /******* * 自定义拦截器 拦截 没有登录的用户 Author:邱于涵 * ******/ import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; /*AbstractClass Use extends ,Interface Use implements *AbstractInterceptor继承于MethodFilterInterceptor *使用MethodFilterInterceptor才能设置<param name="excludeMethods" >method1,...</param> *实现doIntercept方法 */ public class UserInterceptor extends MethodFilterInterceptor{ @Override protected String doIntercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub ActionContext act=ActionContext.getContext(); Map<String,Object> sessionMap=(Map<String,Object>)act.getSession(); System.out.println("test拦截器 正在过滤..."); System.out.println(sessionMap.get("username")); if(sessionMap.get("username")!=null) //|session有值 就 执行下一步 invocation.invoke(); //|否则就返回 failure作为 result 根据struts.xml返回<result name="failure">/User/UserLogin.jsp</result> return "failure"; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="User" extends="struts-default" namespace="/User"> <!-- 注册拦截器 --> <interceptors> <interceptor name="test" class="Controller.UserInterceptor"> </interceptor> </interceptors> <!-- -通配符映射 --> <action name="User*" class="Controller.UserAction" method="User{1}"> <!-- 引用拦截器 --> <interceptor-ref name="test"> <!-- 不拦截的方法 UserLogi用于登录 --> <param name="excludeMethods">UserLogin,UserLog</param> </interceptor-ref> <!-- 没有默认的拦截器栈 Action无法接收POST GET --> <interceptor-ref name="defaultStack"/> <result name="success">/User/User{1}.jsp</result> <result name="failure">/User/UserLogin.jsp</result> </action> </package> </struts>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContex 3ff0 tPath(); 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> This is my JSP page. <br> This is WebApp Directory:<br/> <%=request.getContextPath() %><br/> <a href="User/UserLogin.action">登录用户</a> <br/> Powered By 涵涵 </body> </html>User/UserLogin.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>用户登录</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> 这是通配符映射<a href="<%=request.getContextPath() %>">返回到首页</a><br/> <form action="<%=request.getContextPath() %>/User/UserLog.action" method="POST" > <input type="text" name="username"/> <input type="password" name="pass"/> <input type="submit" value="登录"/> </form> </body> </html>
User/UserLog.jsp用户登录结果页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <% 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>用户登录结果页</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> <s:debug></s:debug> 登录成功!<br/> <s:property value="#session.username"/><br/> <a href="<%=request.getContextPath() %>/User/UserCenter.action">进入用户中心</a> </body> </html>
User/UserCenter.jsp用户中心页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <% 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>用户中心</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> 这是用户中心<br/> 用户名:<s:property value="#session.username"/><br/> <a href="<%=request.getContextPath()%>/User/UserLogOut.action">注销</a> </body> </html>
User/UserLogOut.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>注销成功</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> 注销成功欢迎下次回来<br/> <a href="<%=request.getContextPath() %>">返回首页</a> </body> </html>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"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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></web-app>
相关文章推荐
- Java多线程之Thread继承的用法
- SpringMVC helloworld (xml版本)
- 基于MyBatis和Spring泛型DAO的三层架构
- springMVC的视图解析器
- java web开发 高并发处理
- MyEclipse支持mac的Retina屏解决字体模糊的问题
- 解决Mac下MyEclipse启动服务器之后控制台中的中文乱码问题
- MyMyEclipse中比较常用的快捷键整理
- Leet Code 28 Implement strStr() - Java
- Leet Code 29 Divide Two Integers - 两个整数相除 - Java
- Leet Code 30 由所有单词连接成的子串 - Java
- Leet Code 31 Next Permutation - 下一个排列 - Java
- Leet Code 33 搜索旋转后的有序数组 - Java
- 如何清除eclipse浏览器中的地址
- Spring
- spring jar 下载地址
- java中用字节流读取文本时中文乱码
- java正则实例
- JAVA正则表达式,matcher.find()和 matcher.matches()的区别
- Java Web 之 Servlet - Listener、Filter、Decorator 精辟