一款简单实用的请求过滤以及session超时处理
来源:互联网 发布:金蝶erp软件 编辑:程序博客网 时间:2024/06/07 06:10
代码源于网络共享起初只是针对请求的拦截后来我给加上了session验证
package com.ifan.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionMapping;
import org.json.simple.JSONObject;
/**
* 权限验证过滤器,副业是乱码纠
*/
public class VerificationFilter implements Filter{
/**
* HttpServletRequest对象
*/
private HttpServletRequest thisRequest;
/**
* HttpServletResponse对象
*/
private HttpServletResponse thisResponse;
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
/**
* init
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null){
this.ignore = true;
}else if (value.equalsIgnoreCase("true")){
this.ignore = true;
}else if (value.equalsIgnoreCase("yes")){
this.ignore = true;
}else{
this.ignore = false;
}
}
/**
* destroy
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* doFilter
*/
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null){
request.setCharacterEncoding(encoding);
}
}
thisRequest = (HttpServletRequest)request;
thisResponse = (HttpServletResponse)response;
String loginType=loginType=thisRequest.getParameter("loginType")==null?"":thisRequest.getParameter("loginType");
if(loginType.equals("2")){
//这是获取action请求的
String path = thisRequest.getRequestURI()+"?"+thisRequest.getQueryString();
//这里拦截是不是登录或者注销方法 除了这2个方法其余的都要验证
if(!path.contains("user.php?users=loginByAcount") && !path.contains("user.php?users=logout")&& !path.contains("user.php?users=loginTimeOut")){
//下面这段很简单了 就是判断如果session不存在了 那就是登录超时 由于配合试用dwz框架 所以反悔的数据格式是json 大家可以改成返回一个页面
HttpSession session=thisRequest.getSession(true);
String admin=(String)session.getAttribute("uacount");
if(admin==null||admin==""){
String relId=thisRequest.getParameter("relId");
try {
String resultStr="{\"statusCode\":\"301\",\"message\":\"当前会话已超时,请重新登录!\",\"navTabId\":\" \",\"callbackType\":\" \",\"forwardUrl\":\" \"}";
thisResponse.setContentType("text/json;charset=utf-8");
thisResponse.getWriter().print(resultStr);
return ;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
chain.doFilter(thisRequest,thisResponse);
}
/**
* return this.encoding
* @param request
* @return
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
- 一款简单实用的请求过滤以及session超时处理
- Ajax请求Session超时的处理
- httpClient请求超时的设置以及处理
- mvc下ajax请求遇到session超时简单处理方式
- session超时,处理ajax请求
- session超时,处理ajax请求
- session 超时,处理ajax请求
- ajax请求session 超时处理
- Ajax请求处理Session超时
- session超时,处理ajax请求
- 在Ajax请求中处理登录session超时的方法
- spring security:ajax请求的session超时处理
- Ajax请求session超时处理流程
- Session超时后,Ajax请求处理方式
- ajax异步请求,session超时处理
- ajax异步请求,session超时处理
- Session超时或无效,处理ajax请求
- Session超时后,Ajax请求处理方式
- 不借助中间变量交换数值
- JAVA——基础知识加强
- ISS错误处理
- 黑马程序员-程序控制语句
- 设计模式---创建型设计模式总结
- 一款简单实用的请求过滤以及session超时处理
- 模拟strstr()函数
- 表单中Readonly和Disabled的区别
- HTML语言基础
- 实用hiero 制作自己喜欢的位图字体
- R 语言 模拟处理信令数据
- Ubuntu amd64 running Android SDKs and Android-NDK
- 整理代码,开头定义此类需要用的方法
- 判断数组中的数值是否连续