基本アクション クラスのコンストラクターをチェックインしたいようですが、間違っています。コンストラクターは、アクション インスタンスをインスタンス化するためにオブジェクト ファクトリによって使用されます。この段階では、いくつかのことが利用可能です。あなたの場合、それは間違っています。もう 1 つのアプローチは、メソッド呼び出しが機能する前にロジックをメソッド say execute()
and callに移動するsuper.execute()
ことですが、スーパー呼び出しをアクションに入れるのを忘れると、アクション コードが認証されずに実行される可能性があります。これを防ぐには、アクションが実行される前にコードを実行し、アクション インスタンスまたはアクション コンテキストにアクセスして、より Struts2 になるようにする必要があります。Struts 2 in Actionという本を読んだことがないと思うので、私自身の考えをいくつか述べます。それは、作成AuthenticationInterceptor
と実装するアクションについてですUserAware
ログインしたユーザーを、このインターフェースを実装するアクションに挿入します。インターセプターは次のようになります
public class AuthenticationInterceptor implements Interceptor {
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation actionInvocation) throws Exception {
Map session = actionInvocation.getInvocationContext().getSession();
User user = (User) session.get(Struts2MyConstants.USER);
if (user == null) {
return Action.LOGIN; //login required result
}
else {
Action action = (Action)actionInvocation.getAction();
if (action instanceof UserAware) {
User freshUser = myService.getUser(user.getId());
((UserAware)action).setUser(freshUser);
}
System.out.println("Logged in: interceptor");
return actionInvocation.invoke();
}
}
のUserAware
ように見えます
public interface UserAware {
public void setUser( User user );
}
任意のアクションを参照する安全なデフォルト スタックを作成します
<interceptors>
<interceptor name="authenticationInterceptor" class="org.yourapp.struts.interceptor.AuthenticationInterceptor"/>
<interceptor-stack name="secureStack">
<interceptor-ref name="authenticationInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="secureStack"/>
ベースアクションを実装するUserAware
場合、ログインしているユーザーオブジェクトは、セッションからだけでなく、ユーザーのゲッターを定義するか保護すると、アクションでも利用できます。User
セキュリティ機能を損なわないように、オブジェクトを不変にする必要があります。