詳細設計アルゴリズムなど

DI(依存性注入)

Java関連

SAStrutsのアクション全実行メソッドでログイン済みチェックをする

public class LoginCheckInterceptor extends AbstractInterceptor {

  private static final long serialVersionUID = 7073242636557446465L;

  @Override
  public Object invoke(MethodInvocation arg0) throws Throwable {
    Log.debug("LoginCheckInterceptor.invoke");
    
    if(isExecuteMethod(arg0)){
      if (isLoggedIn()){
        Log.debug("ログインチェック OK.");
        return arg0.proceed();
      }else{
        Log.error("正しくログイン認証されていません。");
        return "/?redirect=true";
      }
    } else 
      return arg0.proceed();
  }

  /**
   * @Executeアノテーションがついているか
   * @param invocation
   * @return Executeがついていたらtrue
   */
  boolean isExecuteMethod(MethodInvocation invocation) {
        return invocation.getMethod().
              isAnnotationPresent(Execute.class);
    }
  
  /**
   * ログイン済みかチェック
   * @return ログイン認証されていて、タイムアウトしてなければ true
   */
  private boolean isLoggedIn() {
        HttpSession session =
            (HttpSession) SingletonS2ContainerFactory
                .getContainer()
                .getExternalContext()
                .getSession();
        
        SessionDto dto = (SessionDto) session.getAttribute("sessionDto");

        //セッションタイムアウトしてたらnullになるはず
        if(dto==null)
          return false;
        else{
          return dto.isLoggedIn();
        }

    }
}

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2009-10-09 (金) 05:37:54