1. 程式人生 > >JAVA 過濾器 我寫在SpirngMVC上的

JAVA 過濾器 我寫在SpirngMVC上的

java web 過濾器    這個是我 SpirngMVC 配置的過濾器

配置 web.xml


<!-- 新增Login 過濾器 -->
    <filter >
      <filter-name >loginFilter</ filter-name>
      <filter-class >yc.base.LoginFilter</ filter-class>
      <init-param >
<span style="white-space:pre">		</span><!--配置需要建議的 Session -->
                   <param-name> checkSessionKey</param-name >
                   <param-value>currentUser</param-value >
             </init-param>
      <init-param >
              <!--配置不需要過濾的 URL--->
             <param-name> notCheckURLList</param-name >
             <param-value> /access/login</param-value >
      </init-param >
    </filter >

       <filter-mapping>
             <filter-name> loginFilter</filter-name >
             <url-pattern> /*</ url-pattern>
       </filter-mapping>


LoginFilter 類


/**
* @author Administrator
* @version 1.0
* @see 登陸的過濾器
* */
public class LoginFilter implements Filter {

     private List<String> notCheckURLList = new ArrayList<String>();
     private String sessionKey = null;
     private List<String> sessionKeyList = new ArrayList<String>();
     protected FilterConfig filterConfig = null;
    
     @Override
     public void destroy() {
               notCheckURLList.clear();
               sessionKeyList.clear();
         
     }

     @Override
     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
               FilterChain filterChain) throws IOException, ServletException {
          // TODO Auto-generated method stub
         
          HttpServletRequest request = (HttpServletRequest) servletRequest;
          HttpServletResponse response = (HttpServletResponse) servletResponse;
         
          HttpSession session = request.getSession();
          if (sessionKey == null) {
               filterChain.doFilter(request, response);
               return;
          }
          String url=request.getRequestURI();
         
         
          if(!checkRequestURIIntNotFilterList(request)){
               if(!isSessionUser(session)){
                    //response.sendError(403);
                    //寫你的過濾提示
                   
               }
          }else{
               //不過濾執行URI
               filterChain.doFilter(servletRequest, servletResponse);
          }
         
         
     }

     @Override
     public void init(FilterConfig filterConfig) throws ServletException {
          // TODO Auto-generated method stub
          this.filterConfig = filterConfig;

          //for url
          String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");

          //for notCheckURLList
          if (notCheckURLListStr != null) {
               StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");
               notCheckURLList.clear();
               while (st.hasMoreTokens()) {
                    notCheckURLList.add(st.nextToken());
               }
          }
         
          //for sessionKey
          sessionKey = filterConfig.getInitParameter("checkSessionKey");
          if (sessionKey != null) {
               StringTokenizer stt = new StringTokenizer(sessionKey, ";");
               sessionKeyList.clear();
               while (stt.hasMoreTokens()) {
                    sessionKeyList.add(stt.nextToken());
               }
          }
     }
    
     /*
     * 驗證不需要過濾的url
     * 返回 boolean 型別
     * */
     private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {
          String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());
          for(String urlOne : notCheckURLList){
               if(uri.equalsIgnoreCase(urlOne)){
                    return true;
               }
          }
          return false;
     }

     /*
     * 校驗session中是否含有當前登陸的user
     * */
     private boolean isSessionUser(HttpSession session) {
               for(String keyOne: sessionKeyList){
                    if(null != keyOne && session.getAttribute(keyOne) != null){
                         return true;
                    }
               }
               return false;
     }
}


這是我的第一篇文章我以後會堅持寫下去的