目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用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. android图片的压缩和水印

    学习了一下压缩和水印,以后要用到的时候可以直接来这里拷贝 activity_main.xml <LinearLayout xmlns:android="http://schemas.a ...

  2. Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 44 bytes) in

    最近莫名出现这个错误. 研究一下原因很奇葩呢. 原因:sql获取数据库中数据,取出数据赋给变量,数据太多,超过memory_limit内存设置了. 解决方法:设置memory_limit不建议.优化代 ...

  3. NET中間語言(IL) 图解

    转载地址是:http://msdn.microsoft.com/zh-tw/library/dd229210.aspx 想查看IL指令,请看中英文对照表: CN-http://www.cnblogs. ...

  4. 第一章 JavaScript概述

    JavaScript诞生于1995年.它当时的目的是为了验证表单输入的验证.因为在JavaScript 问世之前,表单的验证都是通过服务器端验证.而当时都是电话拨号上网的年代,服务器验证数据是一件非常 ...

  5. C#_MVC_Repository_CURD_Controller

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. Android屏幕分辨率详解(VGA、HVGA、QVGA、WVGA、WQVGA)

    这些术语都是指屏幕的分辨率. VGA:Video Graphics Array,即:显示绘图矩阵,相当于640×480 像素: HVGA:Half-size VGA:即:VGA的一半,分辨率为480× ...

  7. 从你的u盘启动:30天自制操作系统第四天u盘启动学习笔记

    暑假学习小日本的那本书:30天自制操作系统 qq交流群:122358078    ,更多学习中的问题.资料,群里分享 developing environment:ubuntu 关于u盘启动自己做的操 ...

  8. 将字符串写进txt中方式

    try { File file = new File(filePath); PrintStream ps = new PrintStream(new FileOutputStream(file)); ...

  9. 两个字符串,若为数字则都相加,若有一个不为数字则,输出error

    import java.util.*; /*请设计一个算法能够完成两个用字符串存储的整数进行相加操作,对非法输入则返回“error”: * 用例:123 234 * 输出:357 * 用例123 as ...

  10. android开发之路04(初级android工程师必会,你懂得!)

    Android初级Android工程师重点掌握内容如下: 1.Android开发基础: ①UI界面设计: ②SQLite数据库: ③android四大组件: ④android网络编程: ⑤androi ...