Struts2:自定义登陆拦截器

自定义登陆拦截器

第一步:创建拦截器类,继承 MethodFilterInterceptor类

第二步:在struts.xml配置自定义拦截器关联action

 

问题:使用自定义拦截器后默认的拦截器不会执行了

解决:把默认拦截器手动使用一次

 

配置自定义拦截器,会对action里面所有的方法都进行拦截

问题:在action里面有UserLogin的登录的方法,这个方法不需要拦截,如果这个方法都拦截,永远登录不进去了

解决:让UserLogin方法不进行拦截,直接通过配置方式让action里面某些方法不进行拦截

Controller.UserAction.java

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.username);
		System.out.println(this.pass);
		if (this.username.equals("admin") && this.pass.equals("admin")) {
			ActionContext act = ActionContext.getContext();
			Map<String, Object> sessionMap = 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 = act.getSession();
		// 清空session(注销)
		sessionMap.clear();
		return "success";
	}
}

Controller.UserInterceptor.java

package Controller;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class UserInterceptor extends MethodFilterInterceptor {

	// 自定义拦截器,拦截没有登录的用户
	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		ActionContext act = ActionContext.getContext();
		Map<String, Object> sessionMap = 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">
				<!-- 不拦截的方法 UserLog用于登录 -->
				<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> 

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>

index.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">
  </head>
  
  <body>
    This is WebApp Directory:<%=request.getContextPath() %><br/>
    <a href="User/UserLogin.action">登录用户</a>
  </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">
  </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">
  </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">
  </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">
  </head>
  
  <body>
      注销成功欢迎下次回来<br/>
      <a href="<%=request.getContextPath() %>">返回首页</a>
  </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值