I'm having a huge problem with my custom interceptor.
The problem is not the code inside of it, the problem is the interceptor is just being called when I open my website for the first time, I mean when the welcome page is shown.
But if I do something which must execute an action, like login or write an url directly on the navigation bar to open another page (which should redirect to the login page if the interceptor was working, but it's redirecting to the page I wroted in the url) the interceptor is not being called because the first line in it is a println and the log doesn't shows it.
My struts.xml configurations is this:
<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>
The interceptor code:
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);
}
}
Some lines of the log:
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: new code
I modified the package definition in the struts.xml like this:
<package name="basicstruts2" extends="struts-default" namespace="/">
Now it fires when I try to go to a url directly and I'm not authenticated. But I'm getting an error if I try to authenticate:
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
I had this some time ago with another issue. I'll try to solve it. But the thing is, this question is solved. The problem seemed to be the package namespace.
The context path is /ltr (IDE log)
Start is in progress...
start?path=/ltr
OK - Arrancada aplicaciĆ³n en trayectoria de contexto /ltr
Are you sure the others Actions are in the same package of the custom Interceptors Stack ?
Your Interceptor is not Thread Safe, while it should be; I don't know who and where sets authenticationSessionField
field, but declaring it class-scoped is the wrong way to go.
I wrote an little article about creating a custom Login Interceptor, feel free to read it :)
The code (and the English) aren't the best but it works fine and at least it can communicate the idea...