1

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

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

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

在此处输入图像描述

4

1 回答 1

1

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

您的拦截器不是线程安全的,但它应该是;我不知道谁和在哪里设置authenticationSessionField字段,但是将其声明为类范围是错误的方法。

我写了一篇关于创建自定义登录拦截器的小文章,请随意阅读:)

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

于 2012-12-14T17:41:08.603 回答