自定义拦截器(权限管理),包含了对ajax和表单请求的拦截

package com.interceptor;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class AuthorityInterceptor extends AbstractInterceptor{ /**
*
*/
private static final long serialVersionUID = 1L; @Override
/* session过期及操作的权限验证拦截器 */
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
// 取得请求相关的ActionContext实例
ActionContext context=invocation.getInvocationContext();
Map session=context.getSession();
String employ=(String)session.get("employ"); if(!ServletActionContext.getRequest().isRequestedSessionIdValid()){
// session 过期
//return Action.LOGIN;
//对ajax请求的拦截
return isAjax();
}else if (employ==null) {
//没有登陆,将服务器提示设置成一个HttpServletRequest属性
context.put("tip","您还没有登录,请登陆系统");
return isAjax(); }else {
return invocation.invoke();
} }// end function private String isAjax() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("text/html;charset=utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter pw = null;
try {
pw = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String flag = "";
if (request.getHeader("X-Requested-With") != null
&& request.getHeader("X-Requested-With").equalsIgnoreCase(
"XMLHttpRequest")) { flag = "sessionfalse";
pw.write(flag);
return null;
}else{
return Action.LOGIN;
}
} }

Struts2 自定义拦截器的更多相关文章

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

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

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

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

  3. 12.Struts2自定义拦截器

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

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

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

  5. Struts2自定义拦截器

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

  6. struts2自定义拦截器与cookie整合实现用户免重复登入

    目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码 结构(两部份)=struts2.xml+自定义拦截器 ...

  7. 5、Struts2自定义拦截器

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

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

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

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

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

随机推荐

  1. 关于 UINavigationController 的一些知识

    1.在 UINavigationController 中,添加一个UITextView,虽然设置self.frame = textView.bounds(从0.0开始),但是系统会自动设置一个cont ...

  2. Oracle触发器反向调用Java程序

    导入jar包 在oracle中导入需要的jar包,我们把编辑好的java类打成jar包,直接在oarcle里面写简单的调用就可以了, 1.操作系统需要拥有支持loadjava命令的jdk. 2.加载j ...

  3. Unable to load native-hadoop library for your platform... using builtin-Java classes where applicable

    以前hadoop也有这个问题,没注意,用spark也提示,解决一下. 首先找到对应自己hadoop版本的的lib包,编译好后,名字类似hadoop-native-64-2.4.1.tar ,放到lib ...

  4. VisualSVN Server和Subversion的联系

    VisualSVN Server是只能在Windows平台上搭建的SVN服务器,内核使用的是Subversion,做了整合:apache+subversion+WMI(实现操作界面等). 用这个的好处 ...

  5. Process Kill Technology && Process Protection Against In Linux

    目录 . 引言 . Kill Process By Kill Command && SIGNAL . Kill Process By Resource Limits . Kill Pr ...

  6. C++ essentials 之 static 关键字

    extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...

  7. 同样有缓冲区,为什么bufferedReader输入流不需要清空缓冲区?而bufferedWriter需要清空缓冲区呢?

    当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并置入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取, 如果缓冲区数据不足,才会再从文件中读取.清不清空Buf ...

  8. p:commandButton vs h:commandButton

    同样的语句,换成primefaces就不刷新页面了.原来p的支持ajax.需要显示指定刷新全部页面.郁闷了一天了. <p:commandButton id="SearchTop&quo ...

  9. maven工程直接部署在tomcat上

    http://www.cnblogs.com/guodefu909/p/4874549.html (现在用的是第三点.)

  10. Yocto开发笔记之《根文件系统裁剪》(QQ交流群:519230208)

    开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...