zoukankan      html  css  js  c++  java
  • 案例47-crm练习登录校验拦截器

    1 LoginInterceptor

    package www.test.web.interceptor;
    
    import java.util.Map;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
    
    //需求:希望除了登录功能以外,其它功能的访问都需要登录才可以操作。
    public class LoginInterceptor extends MethodFilterInterceptor {
    
        @Override
        protected String doIntercept(ActionInvocation invocation) throws Exception {
            // 1.获得session
            Map<String, Object> session = ActionContext.getContext().getSession();
            // 2.获得登陆标识
            Object object = session.get("user");
            // 3.判断登陆标识是否存在
            if (object == null) {
                // 不存在=>没登录=>重定向到登录页面
                return "toLogin";
            } else {
                // 存在=>已经登陆=>放行
                return invocation.invoke();
            }
    
        }
    }

    2 struts.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是否以开发模式运行
                1.热加载主配置.(不需要重启即可生效)
                2.提供更多错误信息输出,方便开发时的调试
         -->
        <constant name="struts.devMode" value="true"></constant>
        <package name="crm" namespace="/" extends="struts-default" >
            <interceptors>
                <!--1注册拦截器 -->
                <interceptor name="loginInterceptor" class="www.test.web.interceptor.LoginInterceptor"></interceptor>
                <!--2注册拦截器栈 -->
                <interceptor-stack name="myStack">
                    <!--引入自定义的拦截器(建议放在20的拦截器之前)  -->
                    <interceptor-ref name="loginInterceptor">
                        <!--配置不需要拦截的方法  -->
                        <param name="excludeMethods">login,exit</param>
                    </interceptor-ref>
                    <!--引入20个默认拦截器  -->
                    <interceptor-ref name="defaultStack"></interceptor-ref>
                </interceptor-stack>
            </interceptors>
            <!--3指定包中的默认拦截器  -->
            <default-interceptor-ref name="myStack"></default-interceptor-ref>
            
            <!--定义全局结果集  -->
            <global-results>
                <result name="toLogin">/login.jsp</result>
            </global-results>
            <global-exception-mappings>
                <!-- 如果出现名为java.lang.RuntimeException的异常,就跳转到名为error的结果 -->
                <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
            </global-exception-mappings>
            <action name="CustomerAction_*" class="www.test.web.action.CustomerAction" method="{1}" >
                <result name="list" >/jsp/customer/list.jsp</result>
                <result name="toList" type="redirectAction">
                     <param name="actionName">CustomerAction_list</param>
                     <param name="namespace">/</param>
                 </result>
            </action>
            <action name="UserAction_*" class="www.test.web.action.UserAction" method="{1}" >
                <result name="toHome" type="redirect">/index.htm</result>
                <result name="error" type="dispatcher">/login.jsp</result>
            </action>
        </package>
    </struts>

    3 没有登录的时候让登录页面全屏显示

    在login.jsp中加入下面的代码即可。

    <script type="text/javascript">
        window.onload=function(){
            if(window.parent!=window){//如果是在框架中
                //就让框架页面跳转到登录页面
                window.parent.location.href="${pageContext.request.contextPath}/login.jsp";
            }
        };
    </script>
  • 相关阅读:
    DAY56
    DAY55
    DAY54
    DAY53
    DAY52
    DAY51
    DAY50
    spark1.1.0部署standalone分布式集群
    Storm流分组介绍
    Storm拓扑的并行度(parallelism)介绍
  • 原文地址:https://www.cnblogs.com/jepson6669/p/8531500.html
Copyright © 2011-2022 走看看