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>

案例47-crm练习登录校验拦截器的更多相关文章

  1. SpringBoot日记——登录与拦截器篇

    之前的文章我们把登录页写了出来,但是想要让登录现实他的基本功能,要如何做呢?本篇文章就来帮你实现第一步,让登录页对账号密码做校验,并且完成登录跳转. LoginController 1. 要实现登录, ...

  2. SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置

    接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...

  3. JAVAEE——SSH项目实战05:用户注册、登陆校验拦截器、员工拜访客户功能和MD5加密

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7170519.html 一.用户注册   显示错误信息到页面上的另一种方法: public ...

  4. taotao用户登录springMVC拦截器的实现

    在springMVC中写拦截器,只需要两步: 一.写 java 拦截器类,实现 interceptor 拦截器接口. 二.在 springMVC 的xml配置文件中,配置我们创建的拦截器对象及其拦截目 ...

  5. SpringMVC拦截器(实现登录验证拦截器)

    本例实现登陆时的验证拦截,采用SpringMVC拦截器来实现 当用户点击到网站主页时要进行拦截,用户登录了才能进入网站主页,否则进入登陆页面 核心代码 首先是index.jsp,显示链接 <%@ ...

  6. 分布式系统登录功能拦截器的实现以及cookie的共享问题(利用cookie实现session在分布式系统的共享)

    当我们的网站采用分布式部署系统时,每个子系统拥有自己独立的session,如果不实现session共享,当用户切换系统访问的时候,会不停的提示登录,这对于用户体验是非常不好的.因此对于多个子系统的的访 ...

  7. 【struts2】自定义登录检查拦截器

    在实际开发中,一个常见的功能要求是:有很多操作都需要登录后才能操作,如果操作的时候还没有登录,那么通常情况下会要求跳转回到登录页面. 1)如何实现这样的功能呢? 在具体实现之前,先来考虑几个问题: ( ...

  8. spring登录验证拦截器和根据用户角色登录

    大家都知道spring的用户登录拦截器,确实省去了程序员不少的精力,下面说说我在项目中使用的感受. 德安微信管理后台是管理多个微信帐号的平台,登录到平台的用户有三个角色,游客和微信帐号管理员.超级管理 ...

  9. struts2学习(6)自定义拦截器-登录验证拦截器

    需求:对登录进行验证,用户名cy 密码123456才能登录进去:  登录进去后,将用户存在session中: 其他链接要来访问(除了登录链接),首先验证是否登录,对这个进行拦截: com.cy.mod ...

随机推荐

  1. Async异步委托

    /// <summary> /// 异步委托 /// </summary> public delegate void AsyncHandler(); public static ...

  2. System.Net.Http

    System.Net.Http DotNet菜园 占个位置^-^ 2018-11-10 09:55:00修改 这个HttpClient的学习笔记一直迟迟未记录,只引用了其他博主的博客链接占个位置,但被 ...

  3. Win7 WPF程序无法接受外部拖拽

    最近在WPF项目中遇到一个问题.虽然选择了AllowDrop = True,但是还是无法支持从外部拖拽文件到程序,倒是内部拖拽(如从一个列表拖拽到树)和从程序拖拽到外部可以. 解决过程 1.考虑是程序 ...

  4. Android Camera相机功能实现 拍照并保存图片

    AndroidManifest.xml <uses-feature android:name="android.hardware.camera"/> <uses- ...

  5. StackOverflow: 你没见过的七个最好的Java答案

    StackOverflow发展到目前,已经成为了全球开发者的金矿.它能够帮助我们找到在各个领域遇到的问题的最有用的解决方案,同时我们也会从中学习到很多新的东西.这篇文章是在我们审阅了StackOver ...

  6. 题解 CF948A 【Protect Sheep】

    题目链接 额..这道题亮点在: $you$ $do$ $not$ $need$ $to$ $minimize$ $their$ $number.$ 所以说嘛... 直接判断狼的四周有没有紧挨着的羊,没 ...

  7. cf555e

    cf555e(缩点) 给一个 n 个点 m 条边的图,以及 q 对点 (s,t),让你给 m 条边定向.问是否存在一种方案,使每对点的 s 能走到 t. \(n,m,q≤ 2×10^5\). 首先,在 ...

  8. 蓝牙4.0BLE抓包(三) – 扫描请求和扫描响应

    版权声明:本文为博主原创文章,转载请注明作者和出处.    作者:强光手电[艾克姆科技-无线事业部] 1. 扫描请求和扫描响应 广播包含扫描请求SCAN_REQ和扫描响应SCAN_RSP. 扫描请求: ...

  9. ThinkCMF Foreach标签

    foreach标签类似与volist标签,只是更加简单,没有太多额外的属性,例如: <foreach name="list" item="vo"> ...

  10. /bin/bash: sshpass: command not found

    按照如下命令进行安装即可: apt-get install sshpass