zoukankan      html  css  js  c++  java
  • struts2登陆拦截器 (FIX)

    struts2.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    	"http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    	<!-- 覆盖掉Struts2一些常量的默认值 -->
        <constant name="struts.enable.DynamicMethodInvocation" value="true" />
        <constant name="struts.devMode" value="true" />
        <constant name="struts.action.extension" value="action,,do"></constant>
        <constant name="struts.configuration.xml.reload" value="true"></constant>
        <constant name="struts.locale" value="zh_CN"></constant>
        <package name="mydefaultpackage" extends="struts-default">
        	<interceptors><!-- 只是定义拦截器 -->
    			<interceptor name="timeCost" class="com.itheima.interceptor.TimeCostInterceptor"></interceptor>
    			<interceptor name="loginCheck" class="com.itheima.interceptor.LoginCheckInterceptor">
    				<param name="excludeMethods">sayHello1</param><!-- 排除在拦截之外 -->
    			</interceptor>
    			<!-- 定义小组 -->
    			<interceptor-stack name="mydefaultStack">
    				<interceptor-ref name="annotationWorkflow"></interceptor-ref>
    				<!-- <interceptor-ref name="loginCheck"></interceptor-ref> -->
    				<interceptor-ref name="timeCost"></interceptor-ref>
    				<interceptor-ref name="defaultStack"></interceptor-ref>
    			</interceptor-stack>
    		</interceptors>
    		<default-interceptor-ref name="mydefaultStack"></default-interceptor-ref>
        </package>
    	<package name="p1" extends="mydefaultpackage">
    		<action name="test1" class="com.itheima.actions.ActionDemo1">
    			<result>/index.jsp</result>
    			<result name="loginCheckFailure">/failure.jsp</result>
    		</action>
    		<action name="test2" class="com.itheima.actions.ActionDemo2">
    			<result>/index.jsp</result>
    		</action>
    	</package>
    </struts>
    

    TimeCostInterceptor

    package com.itheima.interceptor;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    //统计动作方法执行效率的拦截器
    public class TimeCostInterceptor extends AbstractInterceptor {
    	//核心拦截方法
    	public String intercept(ActionInvocation invocation) throws Exception {
    		long startTime = System.nanoTime();//纳秒: 1s=1000000000纳秒
    		String result = invocation.invoke();//让下一个执行
    		long endTime = System.nanoTime();
    		System.out.println(invocation.getInvocationContext().getName()+" cost time :"+(endTime-startTime)+" ns");
    		return result;
    	}
    
    }
    

    LoginCheckInterceptor

    package com.itheima.interceptor;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
    
    public class LoginCheckInterceptor extends MethodFilterInterceptor {
    
    	protected String doIntercept(ActionInvocation invocation) throws Exception {
    		String user = (String)ServletActionContext.getRequest().getSession().getAttribute("user");
    		String result = "loginCheckFailure";
    		if(user!=null){
    			result = invocation.invoke();
    		}
    		return result;
    	}
    
    }
    

    failure.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        
        <title></title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	
    
      </head>
      
      <body>
        对不起!您没有登陆<a href="${pageContext.request.contextPath}/login.jsp?username=wzhting">登陆</a>
      </body>
    </html>
    

    login.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        
        <title></title>
        
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	
    
      </head>
      
      <body>
        <%
        String username = request.getParameter("username");
        if(username!=null){
        	session.setAttribute("user", username);
        }else{
        	session.invalidate();//相当于注销
        }
        %>
      </body>
    </html>
    

      

  • 相关阅读:
    Codeforces 1491 D. Zookeeper and The Infinite Zoo (二进制处理)
    Codeforces 1479A. Searching Local Minimum(注意输入+二分)
    Codeforces 1480B. The Great Hero(阅读模拟题,注意数据范围和攻击顺序)
    Codeforces 1480A. Yet Another String Game (阅读理解题)
    windows 10 开启全盘瞬间索引功能
    JetBrains CLion C++ IDE连接wsl2(Ubuntu)时,报错"Unable to establish SSL connection"解决方案
    WorkFlowy 的 MarkDown 预览方案解决
    git 学习 完全学懂
    jeecgboot <j-popup
    面试之类加载器
  • 原文地址:https://www.cnblogs.com/vaer/p/4691677.html
Copyright © 2011-2022 走看看