→詳細設計・アルゴリズムなど
→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();
}
}
}