java - Spring Security - No bean named 'springSecurityFilterChain' is defined -
the following configuration gives me no bean named 'springsecurityfilterchain' defined
error.my question why happening , not how can resolve this. able resolve using org.springframework.web.context.support.annotationconfigwebapplicationcontext
web.xml
<web-app id="webapp_id" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>spring mvc application</display-name> <!-- spring mvc --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value> /web-inf/applicationcontext.xml </param-value> </context-param> <!-- spring security --> <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> </web-app>
applicationcontext.xml
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd"> <beans:bean class="com.mkyong.web.controller.securityconfig"></beans:bean> </beans:beans>
securityconfig.java
@configuration @enablewebmvcsecurity public class securityconfig extends websecurityconfigureradapter { @autowired public void registerglobalauthentication(authenticationmanagerbuilder auth) throws exception { auth.inmemoryauthentication().withuser("arun").password("arun") .roles("admin"); } @override protected void configure(httpsecurity http) throws exception { http.authorizerequests().antmatchers("/admin/**") .access("hasrole('role_admin')").antmatchers("/dba/**") .access("hasrole('role_admin') or hasrole('role_dba')").and() .formlogin(); } }
you attempting bootstrap java configuration xml, need include <context:annotation-config/>
in applicationcontext.xml. example:
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <beans:bean class="com.mkyong.web.controller.securityconfig"> <context:annotation-config/> </beans:beans>
alternatively, can switch using [annotationconfigwebapplicationcontext] updating web.xml 2:
<web-app id="webapp_id" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>spring mvc application</display-name> <!-- spring mvc --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- start updates --> <context-param> <param-name>contextclass</param-name> <param-value> org.springframework.web.context.support.annotationconfigwebapplicationcontext </param-value> </context-param> <context-param> <param-name>contextconfiglocation</param-name> <param-value>com.mkyong.web.controller.securityconfig</param-value> </context-param> <!-- end updates --> <!-- spring security --> <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> </web-app>
Comments
Post a Comment