LoginSignup
9
9

More than 5 years have passed since last update.

目的

ログインしていないユーザーに、WEBサイトを見られないようにする。

行ったこと

アクセスをされた際に、ログイン情報がセッションに入っていない場合、
welcomeページにリダイレクトをする。

ソースコード

インターセプターの実装

LoginConfirmInterceptor.java
public class LoginConfirmInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID = 1L;

    // ===================================================================================
    //          When you inherit the AbstractInterceptor, this methods must be implemented
    //                                                                            ========
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // Failure to meet both conditions , skip to the Login page.
        return (!isExecuteMethod(invocation) || isLoggedIn()) ? invocation.proceed() : "/飛ばしたいアクション/";
    }

    /**
     * Whether or not with a Execute to the executed Action.
     * 
     * @param invocation
     * @return If with annotation true
     */
    private boolean isExecuteMethod(MethodInvocation invocation) {
        return invocation.getMethod().isAnnotationPresent(Execute.class);
    }

    /**
     * There is Dto on the session , and the userID in Dto
     * 
     * @return if satisfies both the above conditions true
     */
    private boolean isLoggedIn() {
        UserDto userDto = (UserDto) RequestUtil.getRequest().getSession().getAttribute("userDto");
        return (userDto != null && userDto.userId != null);
    }
}

設定の変更

  • 処理対象にするAction
  • 処理対象にしないAction を設定する。
customizer.dicon
<initMethod name="addCustomizer">
    <arg>
        <component class="org.seasar.framework.container.customizer.AspectCustomizer">
            <property name="interceptorName">"loginConfirmInterceptor"</property>

            <!-- 処理の対象外ににしたいAction -->
            <initMethod name="addIgnoreClassPattern">
            <arg>"パッケージ名"</arg>
                <arg>"Action名"</arg>
            </initMethod>
            <initMethod name="addIgnoreClassPattern">
                <arg>"パッケージ名"</arg>
                <arg>"Action名"</arg>
            </initMethod>
            <initMethod name="addIgnoreClassPattern">
                <arg>"パッケージ名"</arg>
                <arg>"Action名"</arg>
            </initMethod>

            <!-- 対象にしたいクラス -->
            <initMethod name="addClassPattern">
                <arg>"パッケージ名"</arg>
                <arg>"クラス名"</arg>
            </initMethod>
        </component>
    </arg>
</initMethod>

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9