执行两次Spring控制器方法
我已经将Spring安全性与Spring MVC集成在一起,并看到了奇怪的行为。控制器中的每个方法针对同一请求执行两次。我在谷歌上搜索了不少,但没帮上什么忙。我能找到的最接近的答案是,我尝试了这些建议,但没有成功 这是我的web.xml:执行两次Spring控制器方法,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我已经将Spring安全性与Spring MVC集成在一起,并看到了奇怪的行为。控制器中的每个方法针对同一请求执行两次。我在谷歌上搜索了不少,但没帮上什么忙。我能找到的最接近的答案是,我尝试了这些建议,但没有成功 这是我的web.xml: <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/security-app-context.xml
/WEB-INF/application-context.xml
</param-value>
</context-param>
appServlet
org.springframework.web.servlet.DispatcherServlet
0
appServlet
/
org.springframework.web.context.ContextLoaderListener
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
上下文配置位置
/WEB-INF/spring/appServlet/security-app-context.xml
/WEB-INF/application-context.xml
以下是applicationcontext.xml的相关部分:
<context:component-scan base-package="com.*" />
<context:spring-configured/>
<mvc:annotation-driven />
<context:property-placeholder location="classpath:/conf/myConfig.properties" />
<mvc:resources mapping="/resources/**" location="/resources/" />
servlet-context.xml仅具有InternalResourceViewResolver的映射
security-context.xml如下所示:
<http pattern="/resources/**" security="none"/>
<http auto-config="false" create-session="stateless" entry-point-ref="loginUrlAuthenticationEntryPoint" >
<intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/j_spring_security_check" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/accessdenied" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/logout" access="ROLE_USER"/>
<custom-filter before="SECURITY_CONTEXT_FILTER" ref="cookieSecurityContextFilter" />
<custom-filter position="LOGOUT_FILTER" ref="logoutfilter" />
<custom-filter position="FORM_LOGIN_FILTER" ref="authenticationFilter" />
<custom-filter after="EXCEPTION_TRANSLATION_FILTER" ref="customExceptionFilter" />
</http>
<beans:bean id="logoutfilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
<beans:constructor-arg value="/"/>
<beans:constructor-arg ref="customLogouthandler"/>
</beans:bean>
然后进一步映射过滤器。
我是否在配置中出错,从而导致控制器被调用两次。我检查了日志,bean只实例化了一次。请帮忙
提前感谢。我会尝试移动
,以防有人无意中发现此帖子和此处发布的解决方案,认为它将修复重复的请求,请首先检查是否是您的浏览器/rest客户端进行了额外呼叫
“在看到第一个建议的解决方案之后”,我浪费了很多时间试图配置应用程序和servlet上下文,直到我发现是某些浏览器(Chrome)上的rest客户端创建了一个额外的“获取图像资源请求”请求。Curl只显示了一个方法执行
然而,我同意将特定于控制器的bean和它们所需的springbean分离到servlet上下文中,并将所有公共bean保留在应用程序上下文中是一个很好的实践。如果web.xml中有多个Spring dispatcher servlet,这一点尤其重要
详细说明先前的答复:
您可以在同一个上下文文件中定义组件扫描和mvc,以保持简单
app-context.xml:
<context:component-scan base-package="..." />
<mvc:annotation-driven />
...
<context:component-scan base-package="....">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
<context:exclude-filter expression="org.springframework.web.bind.annotation.ControllerAdvice" type="annotation"/>
<context:exclude-filter expression="org.springframework.web.bind.annotation.ExceptionHandler" type="annotation"/>
</context:component-scan>
...
<context:component-scan base-package="...">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
<context:include-filter expression="org.springframework.web.bind.annotation.ControllerAdvice" type="annotation"/>
<context:include-filter expression="org.springframework.web.bind.annotation.ExceptionHandler" type="annotation"/>
</context:component-scan>
<mvc:annotation-driven />
...
...
或按如下方式将其分开:
app-context.xml:
<context:component-scan base-package="..." />
<mvc:annotation-driven />
...
<context:component-scan base-package="....">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
<context:exclude-filter expression="org.springframework.web.bind.annotation.ControllerAdvice" type="annotation"/>
<context:exclude-filter expression="org.springframework.web.bind.annotation.ExceptionHandler" type="annotation"/>
</context:component-scan>
...
<context:component-scan base-package="...">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
<context:include-filter expression="org.springframework.web.bind.annotation.ControllerAdvice" type="annotation"/>
<context:include-filter expression="org.springframework.web.bind.annotation.ExceptionHandler" type="annotation"/>
</context:component-scan>
<mvc:annotation-driven />
...
...
servlet-context.xml:
<context:component-scan base-package="..." />
<mvc:annotation-driven />
...
<context:component-scan base-package="....">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
<context:exclude-filter expression="org.springframework.web.bind.annotation.ControllerAdvice" type="annotation"/>
<context:exclude-filter expression="org.springframework.web.bind.annotation.ExceptionHandler" type="annotation"/>
</context:component-scan>
...
<context:component-scan base-package="...">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
<context:include-filter expression="org.springframework.web.bind.annotation.ControllerAdvice" type="annotation"/>
<context:include-filter expression="org.springframework.web.bind.annotation.ExceptionHandler" type="annotation"/>
</context:component-scan>
<mvc:annotation-driven />
...
...
我在applicationcontext.xml中添加了以下内容,并在servlet-context.xml中添加了以下内容:。还尝试了移动。请共享服务器日志。此外,spring安全拦截器URL也按声明顺序应用。最后移动全局拦截器/**(并不是说它将有助于解决控制器问题)