Spring Controller method executed twice -
i have integrated spring security spring mvc , seeing strange behaviour. every method in controller executed twice same request. googled quite bit didn't much. closest find http://forum.springsource.org/archive/index.php/t-83158.html tried suggestions without success.
here web.xml :
<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>
here relevant part of 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/" />
the servlet-context.xml has mapping internalresourceviewresolver
the security-context.xml follows :
<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>
and further mapping filters. doing wrong in configuration lead controller being called twice. have checked logs , beans instantiated once. please help.
thanks in advance.
just in case stumbles on post , solution posted here thinking fix duplicate requests, please first check if browser/rest client making additional call.
i wasted many hours trying configure app , servlet contexts "after seeing first suggested solution", until figured rest client on browsers (chrome) creating additional request "get requests on image resources". curl showed 1 method execution.
however, agree practice separate controller specific beans , required spring beans servlet-context , keep common beans in app-context. important if have multiple spring dispatcher servlet- in web.xml.
elaborating previous answer:
you can either define both component-scan , mvc in same context file keep simple.
app-context.xml:
<context:component-scan base-package="..." /> <mvc:annotation-driven /> ...
or separate them follows:
app-context.xml:
<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> ...
servlet-context.xml:
<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 /> ...
Comments
Post a Comment