web.xml中 filter解析
AdminFilter.java:
web.xml中对应filter文件:
<filter>
<filter-name>adminFilter</filter-name>
<filter-class>org.bluechant.mvc.filter.AdminFilter</filter-class>
<init-param>
<param-name>loginAction</param-name>
<param-value>/account!login.do</param-value>
</init-param>
<init-param>
<param-name>loginPage</param-name>
<param-value>/login.html</param-value>
</init-param>
<init-param>
<param-name>userLoginTag</param-name>
<param-value>account_login_check</param-value>
</init-param>
</filter>
<filter>
<filter-name>userSourceFilter</filter-name>
<filter-class>org.bluechant.mvc.filter.AccountRoleFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>adminFilter</filter-name>
<url-pattern>*.do</url-pattern><!-- 表明凡是访问跳转*.do形式的跳转,都要运行名字为adminFilter的过滤器 -->
</filter-mapping>
<filter-mapping>
<filter-name>userSourceFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
package
org.bluechant.mvc.filter;
import java.io.IOException;
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.bluechant.mvc.core.GlobalSources;
/** */ /**
* @author CHAOS
* @date 2012-02-28 12:49
* filter for check wheather user already login in ,
* if not , send redirect to the login page
* if already login , forward user's request to the target request URL
*/
public class AdminFilter implements Filter {
private String loginPage ;
private String loginAction ;
private String userLoginTag ;
//FilterConfig可用于访问Filter的配置信息
private FilterConfig config;
//执行过滤的核心方法
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse )res;
HttpSession session = request.getSession(true);
//获取客户请求的页面
String urlPath = request.getServletPath();
//如果session范围的user为null,即表明没有登录
//且用户请求的既不是登录页面,也不是处理登录的页面if( session.getAttribute("user") == null
boolean flag = needNotFilter(urlPath);
//不需要进行拦截的页面
if(!flag){
Object obj = session.getAttribute(userLoginTag);
System.out.println("the session userLoginTag value is :"+obj);
if(obj!=null){
flag = true ;
}
}
if(flag){
chain.doFilter(req, res);
}else{
//拦截不通过的时候跳转到的目标//forward到登录页面
response.sendRedirect(request.getContextPath()+loginPage);
}
}
//只有在”/login.html“ || “/account!login.do” 情况下才进行拦截通过,否则拦截不通过跳到登陆页面
private boolean needNotFilter(String urlPath){
if(urlPath.endsWith(loginPage) || urlPath.endsWith(loginAction)){
return true ;
}
return false ;
}
/** *//**
* init params
*/
public void init(FilterConfig config) throws ServletException {
loginPage = config.getInitParameter("loginPage");
loginAction = config.getInitParameter("loginAction");
userLoginTag = config.getInitParameter("userLoginTag");
System.out.println("the loginPage value is :"+loginPage);
System.out.println("the loginAction value is :"+loginAction);
System.out.println("the userLoginTag value is :"+userLoginTag);
this.config = config;
//以前页面登陆的时候session进行设置HttpSession session = request.getSession(true);
//session.setAttribute(GlobalSources.user_login_tag, user);
GlobalSources.user_login_tag = userLoginTag ;//session放到GlobalSources,bean里,
用GlobalSources.user_login_tag获得
}
public void destroy(){
this.config = null;
}
}
import java.io.IOException;
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.bluechant.mvc.core.GlobalSources;
/** */ /**
* @author CHAOS
* @date 2012-02-28 12:49
* filter for check wheather user already login in ,
* if not , send redirect to the login page
* if already login , forward user's request to the target request URL
*/
public class AdminFilter implements Filter {
private String loginPage ;
private String loginAction ;
private String userLoginTag ;
//FilterConfig可用于访问Filter的配置信息
private FilterConfig config;
//执行过滤的核心方法
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse )res;
HttpSession session = request.getSession(true);
//获取客户请求的页面
String urlPath = request.getServletPath();
//如果session范围的user为null,即表明没有登录
//且用户请求的既不是登录页面,也不是处理登录的页面if( session.getAttribute("user") == null
boolean flag = needNotFilter(urlPath);
//不需要进行拦截的页面
if(!flag){
Object obj = session.getAttribute(userLoginTag);
System.out.println("the session userLoginTag value is :"+obj);
if(obj!=null){
flag = true ;
}
}
if(flag){
chain.doFilter(req, res);
}else{
//拦截不通过的时候跳转到的目标//forward到登录页面
response.sendRedirect(request.getContextPath()+loginPage);
}
}
//只有在”/login.html“ || “/account!login.do” 情况下才进行拦截通过,否则拦截不通过跳到登陆页面
private boolean needNotFilter(String urlPath){
if(urlPath.endsWith(loginPage) || urlPath.endsWith(loginAction)){
return true ;
}
return false ;
}
/** *//**
* init params
*/
public void init(FilterConfig config) throws ServletException {
loginPage = config.getInitParameter("loginPage");
loginAction = config.getInitParameter("loginAction");
userLoginTag = config.getInitParameter("userLoginTag");
System.out.println("the loginPage value is :"+loginPage);
System.out.println("the loginAction value is :"+loginAction);
System.out.println("the userLoginTag value is :"+userLoginTag);
this.config = config;
//以前页面登陆的时候session进行设置HttpSession session = request.getSession(true);
//session.setAttribute(GlobalSources.user_login_tag, user);
GlobalSources.user_login_tag = userLoginTag ;//session放到GlobalSources,bean里,
用GlobalSources.user_login_tag获得
}
public void destroy(){
this.config = null;
}
}
web.xml中对应filter文件:
<filter>
<filter-name>adminFilter</filter-name>
<filter-class>org.bluechant.mvc.filter.AdminFilter</filter-class>
<init-param>
<param-name>loginAction</param-name>
<param-value>/account!login.do</param-value>
</init-param>
<init-param>
<param-name>loginPage</param-name>
<param-value>/login.html</param-value>
</init-param>
<init-param>
<param-name>userLoginTag</param-name>
<param-value>account_login_check</param-value>
</init-param>
</filter>
<filter>
<filter-name>userSourceFilter</filter-name>
<filter-class>org.bluechant.mvc.filter.AccountRoleFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>adminFilter</filter-name>
<url-pattern>*.do</url-pattern><!-- 表明凡是访问跳转*.do形式的跳转,都要运行名字为adminFilter的过滤器 -->
</filter-mapping>
<filter-mapping>
<filter-name>userSourceFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>