struts2自定义拦截器与cookie整合实现用户免重复登入
目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用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整合实现用户免重复登入的更多相关文章
- Struts2自定义拦截器Interceptor以及拦截器登录实例
1.在Struts2自定义拦截器有三种方式: -->实现Interceptor接口 public class QLInterceptorAction implements Interceptor ...
- struts2自定义拦截器 设置session并跳转
实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆. 为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟 修改web.xml view plainco ...
- Struts2自定义拦截器处理全局异常
今天在整理之前的项目的时候想着有的action层没有做异常处理,于是想着自定义拦截器处理一下未拦截的异常. 代码: package cn.xm.exam.action.safeHat; import ...
- 12.Struts2自定义拦截器
12.自定义拦截器 拦截器是Struts2的一个重要特性.因为Struts2的大多数核心功能都是通过拦截器实现的. 拦截器之所以称之为“拦截器”,是因为它可以拦截Action方法的执行, ...
- 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】
一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...
- Struts2 自定义拦截器
自定义拦截器(权限管理),包含了对ajax和表单请求的拦截 package com.interceptor; import java.io.IOException; import java.io.Pr ...
- Struts2自定义拦截器
1. 需求 自定义拦截器实现,用户登录的访问控制. 2. 定义拦截器类 public class LoginInterceptor extends AbstractInterceptor { @Ove ...
- 5、Struts2自定义拦截器
一.拦截器相关知识 1.Struts2框架剖析 Holly版本生活案例: 影视公司(拍电影) ActionMapper 传媒公司(包装明星) ActionMapping 明星 ...
- Struts2自定义拦截器——完整实例代码
比如一个网上论坛过滤系统,将网友发表的不文明.不和谐的语言,通过拦截器对这些文字进行自动替代. 该项目包含: 1.自定义拦截器(MyInterceptor.java) 2.发表评论的页面(news.j ...
随机推荐
- git 快速入门(二)
一.引子 git代码托管的优秀工具之一, 其工作原理和svn截然不同.一旦拥有主干master分支权限, 只要在本地拉取主干分支, 可以随时随地切换分支. 它拥有众多优点,eg :支持在断网的情况下, ...
- ajax请求,请求头是provisional are shown。请求未发送出去
问题: ajax请求,请求没成功.ajax请求没有发送出去. 查看network,看到请求头处:Provisional headers are shown. 原因: 搜索了一下,网上说了几个原因. 1 ...
- Asp.Net Core- 配置组件详解
我们之前写的配置都是放置在配置文件Web.config或者app.config中,.net core提供了全新的配置方式,可以直接写在内存中或者写在文件中. .Net Core的配置API主要体现在3 ...
- Css3 display用法
display 属性规定元素应该生成的框的类型. display:none 此元素不会被显示 display:block 此元素将显示为块级元素,此元素前后会带有换行符 display:inl ...
- hdu1754线段树维护区间最大值
#include <iostream> #include <cstdio> using namespace std; #define MAXN 200005 int N,M; ...
- 文件控制列表命令setfacl和getfacl的使用
一 需求 有以下需求,通过setfacl命令实现 一组用户可写可读可执行,一组用户可写可执行,另一组用户只可读 linux rwx oracle wx uplook r 二 解决 第一步 添加六个用户 ...
- android内核读取file文件
内核读取file文件的方法: char* file_read(const char * file_path) { struct file *file = NULL; //保存打开文件的文件指针变量 s ...
- [Bootstrap] 7. Working Herader
<header class="navbar navbar-default navbar-static-top"> <div class="contain ...
- Linux性能优化--CPU[备忘]
http://kumu-linux.github.io/blog/2014/04/21/performance-cpu/
- Linux内核学习笔记2
http://www.cnblogs.com/bastard/category/412387.html