目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码

结构(两部份)=struts2.xml+自定义拦截器对象

配置文件

<!-- 自定义拦截栈与拦截器 -->
<interceptors>
<interceptor name="visitInterceptor" class="cn.kjkj.web.ema.view.interceptor.VisitInterceptor" />
<interceptor name="cookieInterceptor" class="cn.kjkj.web.ema.view.interceptor.CookieInterceptor" />
<interceptor-stack name="MyStack">
<interceptor-ref name="cookieInterceptor" />
<interceptor-ref name="visitInterceptor" />
<interceptor-ref name="paramsPrepareParamsStack" />
</interceptor-stack>
</interceptors> <!-- 设置默认拦截栈 -->
<default-interceptor-ref name="MyStack" />

自定义拦截器具体实现

import java.util.List;
import java.util.Map; import javax.servlet.http.Cookie;
import org.springframework.beans.factory.annotation.Autowired; import cn.kjkj.web.ema.domain.User;
import cn.kjkj.web.ema.service.UserService;
import cn.kjkj.web.ema.view.action.BaseAction;
import cn.kjkj.web.ema.view.action.LoginAction; import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**登入时间缓存拦截器
* */
@SuppressWarnings("serial")
public class CookieInterceptor extends AbstractInterceptor {
@Autowired
private UserService userService; @Override
public String intercept(ActionInvocation invocation) throws Exception {
/**登入拦截*/
Object action=invocation.getAction();
Map<String, Object> session=invocation.getInvocationContext().getSession();
Cookie [] cookies= BaseAction.getRequest().getCookies();
//System.out.println(cookies!=null);
String account=null;
String password=null;
if(!(action instanceof LoginAction)){ //判断是否正在登入
if(session.get("usermessage")!=null){
return invocation.invoke();
}else{
if(cookies!=null){ for(Cookie c:cookies){
if(c.getName().equalsIgnoreCase("username")){
if(c!=null){
account=c.getValue();
}
}else if(c.getName().equalsIgnoreCase("password")){
if(c!=null){
password=c.getValue();
}
}
}
if(account!=null&&password!=null){
User u=new User(account, password);
List<User> list=userService.checkMessage(u);
if(list.size()==1){
session.put("usermessage", list.get(0));
return BaseAction.MAIN;
}
}
}
}
} return invocation.invoke();
}

struts2自定义拦截器与cookie整合实现用户免重复登入的更多相关文章

  1. Struts2自定义拦截器Interceptor以及拦截器登录实例

    1.在Struts2自定义拦截器有三种方式: -->实现Interceptor接口 public class QLInterceptorAction implements Interceptor ...

  2. struts2自定义拦截器 设置session并跳转

    实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆. 为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟 修改web.xml view plainco ...

  3. Struts2自定义拦截器处理全局异常

    今天在整理之前的项目的时候想着有的action层没有做异常处理,于是想着自定义拦截器处理一下未拦截的异常. 代码: package cn.xm.exam.action.safeHat; import ...

  4. 12.Struts2自定义拦截器

    12.自定义拦截器        拦截器是Struts2的一个重要特性.因为Struts2的大多数核心功能都是通过拦截器实现的. 拦截器之所以称之为“拦截器”,是因为它可以拦截Action方法的执行, ...

  5. 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】

    一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...

  6. Struts2 自定义拦截器

    自定义拦截器(权限管理),包含了对ajax和表单请求的拦截 package com.interceptor; import java.io.IOException; import java.io.Pr ...

  7. Struts2自定义拦截器

    1. 需求 自定义拦截器实现,用户登录的访问控制. 2. 定义拦截器类 public class LoginInterceptor extends AbstractInterceptor { @Ove ...

  8. 5、Struts2自定义拦截器

    一.拦截器相关知识 1.Struts2框架剖析 Holly版本生活案例: 影视公司(拍电影)    ActionMapper 传媒公司(包装明星) ActionMapping 明星           ...

  9. Struts2自定义拦截器——完整实例代码

    比如一个网上论坛过滤系统,将网友发表的不文明.不和谐的语言,通过拦截器对这些文字进行自动替代. 该项目包含: 1.自定义拦截器(MyInterceptor.java) 2.发表评论的页面(news.j ...

随机推荐

  1. 【转】安装Ubuntu 15.10后要做的事

    Ubuntu 15.10发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...

  2. centos上安装jdk环境

    老沙采用的环境是centos 6.5 64位服务器.在linux上安装jdk环境都很多中方式,这里讲解下手工进行安装并进行环境变量配置. 首先需要下载一个64位版本的linux,可以去oracle官网 ...

  3. ProgressSeekBar

    ProgressSeekBar.rar

  4. iOS开发——网络编程OC篇&Socket编程

    Socket编程 一.网络各个协议:TCP/IP.SOCKET.HTTP等 网络七层由下往上分别为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 其中物理层.数据链路层和网络层通常被称作 ...

  5. Python 将文本转换成html的简单示例

    实例txt文件test_input.txt: Welcome to World Wide Spam. Inc. These are the corporate web pages of *World ...

  6. javascript关闭浏览器窗口

    var opened=window.open('about:blank','_self'); opened.close(); window.open 不被阻止 .open('_blank') 然后 t ...

  7. 出栈入栈动画demo

    项目做了一个切换界面动画的功能,用到了出栈入栈的,写了一个demo package com.myron.stackview; import java.util.Stack; import androi ...

  8. 原 Debian设置开机自动启动与关闭

    发表于1年前(2013-01-08 13:01)   阅读(2380) | 评论(0) 2人收藏此文章, 我要收藏 赞0 开机自动启动 update-rc.d chkconfig 熟悉debian系统 ...

  9. Controllers

    Controllers Controllers are the bread and butter of the framework they control when a model is used ...

  10. C++ (P103—P154)

    1 任一指针变量本身的数据值得类型都是unsigned long int 2 指针值为0的叫做空指针,为了安全起见,声明指针时最好初始化,哪怕是初始化为空指针 3 一般不能使用不同类型变量的地址来给指 ...