Java 拦截器刚刚在欢迎页面前调用

Java 拦截器刚刚在欢迎页面前调用,java,authentication,struts2,interceptor,interceptorstack,Java,Authentication,Struts2,Interceptor,Interceptorstack,我的自定义拦截器有一个很大的问题 问题不在于它里面的代码,问题在于当我第一次打开我的网站时,拦截器正在被调用,我的意思是当欢迎页面显示时 但是如果我做了一些必须执行操作的事情,比如登录或者直接在导航栏上写一个url来打开另一个页面(如果拦截器正在工作,它应该重定向到登录页面,但是它重定向到我在url中创建的页面)侦听器没有被调用,因为它的第一行是println,而日志没有显示它 我的struts.xml配置如下: 真的 错误 索引、登录、privacypolicy、登录输入 /WEB-INF/

我的自定义拦截器有一个很大的问题

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

但是如果我做了一些必须执行操作的事情,比如登录或者直接在导航栏上写一个url来打开另一个页面(如果拦截器正在工作,它应该重定向到登录页面,但是它重定向到我在url中创建的页面)侦听器没有被调用,因为它的第一行是println,而日志没有显示它

我的struts.xml配置如下:


真的
错误
索引、登录、privacypolicy、登录输入
/WEB-INF/content/index.jsp
index.jsp
拦截器代码:

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中修改了包定义,如下所示:


现在,当我试图直接转到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日志)


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

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

我写了一篇文章,请随意阅读:)


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

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

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

我写了一篇文章,请随意阅读:)


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

你是说拦截器不是为你排除在拦截器之外的方法而发射的???不,我尝试用必须发射拦截器的方法来发射拦截器。那些需要认证的。但事实并非如此。我现在将用新的结果编辑这个问题。我刚刚添加了我的文件夹结构,因为我的名称空间有问题。如果我在struts.xml中的包定义中使用namespace=“/”,将执行拦截器,但找不到操作。如果我在名称空间中使用任何其他东西(然后调用defaultStack,而不是“myStack”),则会找到操作,但在我第一次调用欢迎页面时,拦截器只是执行了,而不是与任何其他操作一起执行。在每个操作中的类声明之前使用@namespace(“/”)并不能解决问题。在struts.xml中,使用这个名称空间和包namespace=“/”,拦截器不会被执行,框架直接进入操作。你是说拦截器没有为你从拦截器中排除的方法触发???不,我尝试用必须触发拦截器的方法触发拦截器。那些需要认证的。但事实并非如此。我现在将用新的结果编辑这个问题。我刚刚添加了我的文件夹结构,因为我的名称空间有问题。如果我在struts.xml中的包定义中使用namespace=“/”,将执行拦截器,但找不到操作。如果我在名称空间中使用任何其他东西(然后调用defaultStack,而不是“myStack”),则会找到操作,但在我第一次调用欢迎页面时,拦截器只是执行了,而不是与任何其他操作一起执行。在每个操作中的类声明之前使用@namespace(“/”)并不能解决问题。有了它和struts.xml中的package namespace=“/”,拦截器将不会执行,框架将直接转到操作。1。我知道我的拦截器不是线程安全的,我必须处理int,但这不是我现在的问题(我从中的教程中复制了它)。//2.关于包…,我不知道该回答什么,我的操作都在同一个文件夹中,我的jsp都在“WEB-INF/content/”下,我在包定义中添加了“namespace=“/”,现在当我直接在导航栏中写入url并将我发送到登录页面时,会调用拦截器,但是如果我登录,服务器会给我一个错误。我将把这个添加到问题中。@IsabelPM,我说的包是struts包,在struts.xml中,不是java包。当您有多个包时,添加一个名称空间是一件好事……现在我有另一个问题,因为同样的事情,我的意思是,名称空间。如果我在struts.xml中的包定义中使用namespace=“/”,将执行拦截器,但找不到操作。如果我没有指定名称空间,或者如果我使用namespace=“”(或其他名称,但不是“/”),则会找到操作,但不会执行拦截器。Isabel,将类名添加到操作声明中:
1。我知道我的拦截器不是线程安全的,我必须处理int,但这不是我现在的问题(我从中的教程中复制了它)。//2.关于包…,我不知道该回答什么,我的操作都在同一个文件夹中,我的jsp都在“WEB-INF/content/”下,我在包定义中添加了“namespace=“/”,现在当我直接在导航栏中写入url并将我发送到登录页面时,会调用拦截器,但是如果我登录,服务器会给我一个错误。我将添加t
Start is in progress...
start?path=/ltr
OK - Arrancada aplicación en trayectoria de contexto /ltr