Interceptor刚刚在欢迎页面之前调用过

时间:2012-12-14 17:20:36

标签: java authentication struts2 interceptor interceptorstack

我的自定义拦截器存在很大问题。

问题不在于它内部的代码,问题是我第一次打开我的网站时只是调用拦截器,我的意思是显示欢迎页面。

但是如果我做了一些必须执行动作的事情,比如登录或直接在导航栏上写一个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="/">

现在当我尝试直接访问网址并且我没有通过身份验证时它就会触发。但是如果我尝试进行身份验证,我会收到错误:

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

enter image description here

1 个答案:

答案 0 :(得分:1)

您确定其他操作是否在自定义拦截器堆栈的同一个包中?

你的拦截器不是线程安全的,而应该是;我不知道设置authenticationSessionField字段的位置和位置,但声明它是类范围的是错误的方法。

我写了little article about creating a custom Login Interceptor,随时可以阅读:)

代码(和英文)不是最好的,但它工作正常,至少它可以传达这个想法......