熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Web App struts框架裡實現Filter

2013-11-23 20:22:49  來源: Java開源技術 

   webxml裡添加filter配置信息
  <filter>
  <filtername>Authentication</filtername>
  <filterclass>
  limaswebfiltersAuthenticationFilter
  </filterclass>
  <initparam>
  <paramname>onError</paramname>
  <paramvalue>/pages/indexjsp</paramvalue>
  </initparam>
  </filter>
  <filtermapping>
  <filtername>Authentication</filtername>
  <urlpattern>/protect/*</urlpattern>
  </filtermapping>
  <filterclass>定義使用limaswebfiltersAuthenticationFilter類執行過濾Action
  <initparam>當驗證失敗後Forward到/pages/indexjsp
  <filtermapping>只有當地址包括/protect/時運行filter類
  
   實現limaswebfiltersAuthenticationFilter
  package limaswebfilters;
  import javaioIOException;
  import javaxservletRequestDispatcher;
  import javaxservletServletException;
  import javaxservlethttp*;
  import javaxservletFilter;import javaxservletFilterChain;
  import javaxservletFilterConfig;
  import limaswebbeanUserBean;
  import javaxservletServletRequest;
  import javaxservletServletResponse;import orgapachestrutsGlobals;
  import orgapachestrutsaction*;
  import nstantsConstants;
  /**
  * Filter implementation for user authentication必須實現Filter接口
  * @author tyrone * @version $Id: AuthenticationFilterjavav
  */public class AuthenticationFilter implements Filter{
  /**
  * filterConfig will store the reference of FilterConfig
  */  private FilterConfig filterConfig;
  private String onErrorUrl;
  /**
  * store the reference of the FilterConfig
  * @param config FilterConfig object
  * @throws ServletException
  */  public void init(FilterConfig config) throws ServletException
  {//獲得驗證失敗forward地址
  filterConfig = config;
  onErrorUrl=filterConfiggetInitParameter(onError);
  if (onErrorUrl==null || equals(onErrorUrl)){
  onErrorUrl=/pages/indexjsp;
  }
  }
  /**
  * User Authentication is done If User is authenticated successful then
  * control is transferred to logon URI
  * @param ServletRequest Request
  * @param ServletRequest Response
  * @param FilterChain Filter Chain
  * @throws ServletExceptionIOException
  */  public void doFilter(ServletRequest request ServletResponse response FilterChain next)
  throws IOException ServletException
  {
  HttpServletRequest httpRequest = (HttpServletRequest)request;
  HttpServletResponse httpResponse=(HttpServletResponse)response;
  // Current session
  HttpSession httpSession = ();
  //Session裡是否有用戶信息
  if ((ConstantsUSER_KEY) == null)
  {
  ActionErrors errors=new ActionErrors();
  errorsadd(ActionErrorsGLOBAL_ERROR
  new ActionError(error authentication));
  (GlobalsERROR_KEYerrors);
  //沒有驗證失敗forward到/pages/indexjsp
  (onErrorUrl)forward(httpRequesthttpResponse);
  }else//成果過濾Action結束
  nextdoFilter(requestresponse);
  }
  /**
  * destroy() method is called by the servlet container
  */  public void destroy()
  {
  }
  }

From:http://tw.wingwit.com/Article/program/Java/ky/201311/28400.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.