カスタムインターセプターに大きな問題があります。
問題はその中のコードではありません。問題は、Webサイトを初めて開いたときにインターセプターが呼び出されていることです。つまり、ウェルカムページが表示されているときです。
しかし、ログインするか、ナビゲーションバーに直接URLを書き込んで別のページを開くなどのアクションを実行する必要がある場合(インターセプターが機能している場合はログインページにリダイレクトされますが、私が書いたページにリダイレクトされます) url)インターセプターは、その最初の行がprintlnであり、ログに表示されないため、呼び出されていません。
私のstruts.xml構成は次のとおりです。
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="global" />
<constant name="struts.convention.default.parent.package" value="restful"/>
<constant name="struts.action.extension" value=","/>
<constant name="struts.ui.theme" value="simple" />
<package name="restful" extends="rest-default, struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
</package>
<package name="basicstruts2" extends="struts-default">
<interceptors>
<interceptor name="authorization" class="org.letter.ltr.scripts.AuthInterceptor" />
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
<interceptor-ref name="authorization">
<param name="excludeActions">index,login,privacypolicy,login-input</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
<default-action-ref name="index" />
<global-results>
<result name="authentication_required">/WEB-INF/content/index.jsp</result>
</global-results>
<action name="index">
<result name="login">index.jsp</result>
</action>
</package>
</struts>
インターセプターコード:
public class AuthInterceptor extends AbstractInterceptor implements{
private String authenticationSessionField = "authenticated";
private static final String authenticationRequiredResult = "authentication_required";
private Set excludeActions = Collections.EMPTY_SET;
@Override
public String intercept (ActionInvocation invocation) throws Exception {
System.out.println("---Inside interceptor.....");
Map session = invocation.getInvocationContext().getSession();
String actionName = invocation.getProxy().getActionName();
Object authenticationObject = session.get(authenticationSessionField);
if(excludeActions.contains(actionName) ||
(authenticationObject != null &&
authenticationObject instanceof Boolean &&
authenticationObject.equals(Boolean.TRUE))){
return invocation.invoke();
}
else return authenticationRequiredResult;
}
public void setAuthenticationSessionField(String authenticationSessionField){
this.authenticationSessionField = authenticationSessionField;
}
public void setExcludeActions(String values){
if(values != null)
this.excludeActions = TextParseUtil.commaDelimitedStringToSet(values);
}
}
ログのいくつかの行:
2012-12-14 18:10:33,796 DEBUG com.opensymphony.xwork2.DefaultActionProxy.debug:68 - Creating an DefaultActionProxy for namespace / and action name
2012-12-14 18:10:33,844 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - intercept '//' {
2012-12-14 18:10:33,844 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - applied invocation context locale=es_ES
2012-12-14 18:10:33,845 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - before Locale=es_ES
2012-12-14 18:10:33,898 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[com.opensymphony.xwork2.ActionSupport@7b1e8d, com.opensymphony.xwork2.DefaultTextProvider@138be8d], property=struts]
2012-12-14 18:10:33,917 DEBUG com.opensymphony.xwork2.util.LocalizedTextUtil.debug:68 - Resource bundles reloaded
2012-12-14 18:10:33,921 DEBUG org.apache.struts2.interceptor.FileUploadInterceptor.debug:68 - Bypassing //
2012-12-14 18:10:33,922 DEBUG com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.debug:68 - Setting static parameters {}
2012-12-14 18:10:33,926 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:68 - Setting params NONE
2012-12-14 18:10:33,926 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:68 - Setting params
2012-12-14 18:10:33,931 DEBUG org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.debug:68 - Validating // with method execute.
2012-12-14 18:10:33,982 DEBUG com.opensymphony.xwork2.validator.ValidationInterceptor.debug:68 - Invoking validate() on action com.opensymphony.xwork2.ActionSupport@7b1e8d
2012-12-14 18:10:33,987 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:68 - cannot find method [validateExecute] in action [com.opensymphony.xwork2.ActionSupport@7b1e8d]
2012-12-14 18:10:33,987 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:68 - cannot find method [validateDoExecute] in action [com.opensymphony.xwork2.ActionSupport@7b1e8d]
---Inside interceptor.....
2012-12-14 18:10:33,992 DEBUG org.apache.struts2.dispatcher.ServletDispatcherResult.debug:68 - Forwarding to location /WEB-INF/content/index.jsp
...
2012-12-14 18:10:46,398 DEBUG com.opensymphony.xwork2.DefaultActionProxy.debug:68 - Creating an DefaultActionProxy for namespace / and action name tablon
2012-12-14 18:10:46,399 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - intercept '//tablon' {
2012-12-14 18:10:46,399 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - applied invocation context locale=es_ES
2012-12-14 18:10:46,400 DEBUG com.opensymphony.xwork2.interceptor.I18nInterceptor.debug:68 - before Locale=es_ES
2012-12-14 18:10:46,417 DEBUG com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.debug:68 - Entering nullPropertyValue [target=[org.letter.ltr.action.TablonAction@1ea8209, com.opensymphony.xwork2.DefaultTextProvider@138be8d], property=struts]
2012-12-14 18:10:46,424 DEBUG com.opensymphony.xwork2.util.LocalizedTextUtil.debug:68 - Resource bundles reloaded
2012-12-14 18:10:46,427 DEBUG org.apache.struts2.interceptor.FileUploadInterceptor.debug:68 - Bypassing //tablon
2012-12-14 18:10:46,427 DEBUG com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.debug:68 - Setting static parameters {}
2012-12-14 18:10:46,428 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:68 - Setting params NONE
2012-12-14 18:10:46,429 DEBUG com.opensymphony.xwork2.interceptor.ParametersInterceptor.debug:68 - Setting params
2012-12-14 18:10:46,430 DEBUG org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.debug:68 - Validating //tablon with method execute.
2012-12-14 18:10:46,449 DEBUG com.opensymphony.xwork2.validator.ValidationInterceptor.debug:68 - Invoking validate() on action org.letter.ltr.action.TablonAction@1ea8209
2012-12-14 18:10:46,449 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:68 - cannot find method [validateExecute] in action [org.letter.ltr.action.TablonAction@1ea8209]
2012-12-14 18:10:46,450 DEBUG com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil.debug:68 - cannot find method [validateDoExecute] in action [org.letter.ltr.action.TablonAction@1ea8209]
2012-12-14 18:10:46,450 DEBUG com.opensymphony.xwork2.DefaultActionInvocation.debug:68 - Executing action method = execute
PS:新しいコード
struts.xmlのパッケージ定義を次のように変更しました。
<package name="basicstruts2" extends="struts-default" namespace="/">
これで、URLに直接アクセスしようとしたときに、認証されていない場合に起動します。しかし、認証しようとするとエラーが発生します。
2012-12-14 18:59:02,351 ERROR org.apache.struts2.dispatcher.Dispatcher.error:38 -Could not find action or result /ltr/login
No result defined for action com.opensymphony.xwork2.ActionSupport and result success
私はこれを少し前に別の問題で抱えていました。私はそれを解決しようとします。しかし、問題は、この質問は解決されたということです。問題はパッケージの名前空間にあるようです。
コンテキストパスは/ltr(IDEログ)です
Start is in progress...
start?path=/ltr
OK - Arrancada aplicación en trayectoria de contexto /ltr