一款简单实用的请求过滤以及session超时处理
2013-03-16 13:52
447 查看
代码源于网络共享起初只是针对请求的拦截后来我给加上了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);
}
}
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);
}
}
相关文章推荐
- mvc下ajax请求遇到session超时简单处理方式
- Extjs6关于Ajax和form表单提交以及store请求session超时的处理方法
- mvc下ajax请求遇到session超时简单处理方式
- Vue------第五天(Vue的Ajax请求,使用Axios,目前熟悉了一下,主要包括GET请求、POST请求、并发请求、请求参数的配置、服务器响应的结构以及对服务器响应错误的简单处理)
- Session超时或无效,处理ajax请求
- Ajax请求session超时处理流程(DWZ)
- DWZ(JUI) 教程 Ajax请求session超时处理流程
- I/O模型和原理,以及简单解析客户端请求WEB服务器内部处理过程,MPM三种模
- session超时,处理ajax请求
- zookeeper源码阅读分析笔记--客户端服务端通信机制以及session超时、过期处理
- spring security:ajax请求的session超时处理
- Nohttp网络请求数据,Post以及Get的简单实用以及设置缓存文字的的请求
- spring security:ajax请求的session超时处理
- 在Web应用中,会用到大量的Ajax请求,在Ajax请求中存在的一个问题就是如何判断session超时,在网上找到的一个java中处理此问题方法:
- Nohttp网络请求数据,Post以及Get的简单实用以及设置缓存文字的的请求
- Ajax请求Session超时的处理
- Springmvc拦截器简单使用以及session超时跳转
- session 超时,处理ajax请求
- Ajax请求Session超时的处理
- session超时,处理ajax请求