import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
聽* 鐢ㄤ簬鐨勪嬌 Browser 涓嶇紦瀛橀〉闈㈢殑榪囨護(hù)鍣?br />聽*/
public class ForceNoCacheFilter聽implements Filter {
聽public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
聽{
聽聽((HttpServletResponse) response).setHeader("Cache-Control","no-cache");
聽聽((HttpServletResponse) response).setHeader("Pragma","no-cache");
聽聽((HttpServletResponse) response).setDateHeader ("Expires", -1);
聽聽filterChain.doFilter(request, response);
聽}
聽public void destroy()
聽{
聽}
聽聽聽 public void init(FilterConfig filterConfig) throws ServletException
聽{
聽}
}
浜屻佹嫻嬬敤鎴鋒槸鍚︾櫥闄嗙殑榪囨護(hù)鍣?/p>
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.IOException;
/**
聽* 鐢ㄤ簬媯嫻嬬敤鎴鋒槸鍚︾櫥闄嗙殑榪囨護(hù)鍣紝濡傛灉鏈櫥褰曪紝鍒欓噸瀹氬悜鍒版寚鐨勭櫥褰曢〉闈?lt;p>
聽* 閰嶇疆鍙傛暟<p>
聽* checkSessionKey 闇媯鏌ョ殑鍦?Session 涓繚瀛樼殑鍏抽敭瀛?lt;br/>
聽* redirectURL 濡傛灉鐢ㄦ埛鏈櫥褰曪紝鍒欓噸瀹氬悜鍒版寚瀹氱殑欏甸潰錛孶RL涓嶅寘鎷?ContextPath<br/>
聽* notCheckURLList 涓嶅仛媯鏌ョ殑URL鍒楄〃錛屼互鍒嗗彿鍒嗗紑錛屽茍涓?URL 涓笉鍖呮嫭 ContextPath<br/>
聽*/
public class CheckLoginFilter
聽implements Filter
{
聽聽聽聽protected FilterConfig filterConfig = null;
聽聽聽 private String redirectURL = null;
聽聽聽聽private List notCheckURLList = new ArrayList();
聽聽聽聽private String sessionKey = null;
聽public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
聽{
聽聽HttpServletRequest request = (HttpServletRequest) servletRequest;
聽聽HttpServletResponse response = (HttpServletResponse) servletResponse;
聽聽 HttpSession session = request.getSession();
聽聽if(sessionKey == null)
聽聽{
聽聽聽filterChain.doFilter(request, response);
聽聽聽return;
聽聽}
聽聽if((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null)
聽聽{
聽聽聽response.sendRedirect(request.getContextPath() + redirectURL);
聽聽聽return;
聽聽}
聽聽filterChain.doFilter(servletRequest, servletResponse);
聽}
聽public void destroy()
聽{
聽聽notCheckURLList.clear();
聽}
聽private boolean checkRequestURIIntNotFilterList(HttpServletRequest request)
聽{
聽聽String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());
聽聽return notCheckURLList.contains(uri);
聽}
聽public void init(FilterConfig filterConfig) throws ServletException
聽{
聽聽this.filterConfig = filterConfig;
聽聽redirectURL = filterConfig.getInitParameter("redirectURL");
聽 sessionKey = filterConfig.getInitParameter("checkSessionKey");
聽聽String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");
聽聽if(notCheckURLListStr != null)
聽聽{
聽聽聽StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");
聽聽聽notCheckURLList.clear();
聽聽聽while(st.hasMoreTokens())
聽聽聽{
聽聽聽聽notCheckURLList.add(st.nextToken());
聽聽聽}
聽聽}
聽}
}
涓夈佸瓧絎︾紪鐮佺殑榪囨護(hù)鍣?/p>
import javax.servlet.*;
import java.io.IOException;
/**
聽* 鐢ㄤ簬璁劇疆 HTTP 璇鋒眰瀛楃緙栫爜鐨勮繃婊ゅ櫒錛岄氳繃榪囨護(hù)鍣ㄥ弬鏁癳ncoding鎸囨槑浣跨敤浣曠瀛楃緙栫爜,鐢ㄤ簬澶勭悊Html Form璇鋒眰鍙傛暟鐨勪腑鏂囬棶棰?br />聽*/
public class CharacterEncodingFilter
聽implements Filter
{
聽protected FilterConfig filterConfig = null;
聽protected String encoding = "";
聽public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
聽{
聽聽聽聽聽聽聽 if(encoding != null)
聽聽聽聽聽聽聽聽 servletRequest.setCharacterEncoding(encoding);
聽聽聽聽聽聽聽聽filterChain.doFilter(servletRequest, servletResponse);
聽}
聽public void destroy()
聽{
聽聽filterConfig = null;
聽聽encoding = null;
聽}
聽聽聽 public void init(FilterConfig filterConfig) throws ServletException
聽{
聽聽聽聽聽聽聽聽聽this.filterConfig = filterConfig;
聽聽聽聽聽聽聽 this.encoding = filterConfig.getInitParameter("encoding");
聽}
}
鍥涖佽祫婧愪繚鎶よ繃婊ゅ櫒
package catalog.view.util;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
//
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This Filter class handle the security of the application.
*
* It should be configured inside the web.xml.
*
* @author Derek Y. Shen
*/
public class SecurityFilter implements Filter {
//the login page uri
private static final String LOGIN_PAGE_URI = "login.jsf";
//the logger object
private Log logger = LogFactory.getLog(this.getClass());
//a set of restricted resources
private Set restrictedResources;
/**
* Initializes the Filter.
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.restrictedResources = new HashSet();
this.restrictedResources.add("/createProduct.jsf");
this.restrictedResources.add("/editProduct.jsf");
this.restrictedResources.add("/productList.jsf");
}
/**
* Standard doFilter object.
*/
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
this.logger.debug("doFilter");
String contextPath = ((HttpServletRequest)req).getContextPath();
String requestUri = ((HttpServletRequest)req).getRequestURI();
this.logger.debug("contextPath = " + contextPath);
this.logger.debug("requestUri = " + requestUri);
if (this.contains(requestUri, contextPath) && !this.authorize((HttpServletRequest)req)) {
this.logger.debug("authorization failed");
((HttpServletRequest)req).getRequestDispatcher(LOGIN_PAGE_URI).forward(req, res);
}
else {
this.logger.debug("authorization succeeded");
chain.doFilter(req, res);
}
}
public void destroy() {}
private boolean contains(String value, String contextPath) {
Iterator ite = this.restrictedResources.iterator();
while (ite.hasNext()) {
String restrictedResource = (String)ite.next();
if ((contextPath + restrictedResource).equalsIgnoreCase(value)) {
return true;
}
}
return false;
}
private boolean authorize(HttpServletRequest req) {
//澶勭悊鐢ㄦ埛鐧誨綍
/* UserBean user = (UserBean)req.getSession().getAttribute(BeanNames.USER_BEAN);
if (user != null && user.getLoggedIn()) {
//user logged in
return true;
}
else {
return false;
}*/
}
}