Thursday 15 August 2013

java - Spring Security cannot find custom UserDetailsService -



java - Spring Security cannot find custom UserDetailsService -

i'm trying utilize spring security on app engine. implemented own userdetailsservice.

package com.example.mymodule.app2; import com.example.mymodule.repoobject.mutibouser; import com.example.mymodule.repository.mutibouserrepo; import org.springframework.beans.factory.annotation.autowired; import org.springframework.security.core.grantedauthority; import org.springframework.security.core.authority.authorityutils; import org.springframework.security.core.userdetails.user; import org.springframework.security.core.userdetails.userdetails; import org.springframework.security.core.userdetails.userdetailsservice; import org.springframework.security.core.userdetails.usernamenotfoundexception; import org.springframework.stereotype.component; import java.util.collection; import java.util.list; @component public class mutibouserdetailsservice implements userdetailsservice { private final mutibouserrepo mutibouserrepo; @autowired public mutibouserdetailsservice(mutibouserrepo mutibouserrepo) { if (mutibouserrepo == null) { throw new illegalargumentexception("mutibouserrepo cannot null"); } this.mutibouserrepo = mutibouserrepo; } @override public userdetails loaduserbyusername(string username) throws usernamenotfoundexception { mutibouser mutibouser = mutibouserrepo.findbyname(username); if (mutibouser == null) { throw new usernamenotfoundexception("invalid username/password."); } list<grantedauthority> authoritylist = authorityutils .createauthoritylist("role_user"); homecoming new user(mutibouser.getemail(), mutibouser.getpassword(), authoritylist); } }

here web.xml

<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextconfiglocation</param-name> <param-value> /web-inf/spring/services.xml /web-inf/spring/security.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.contextloaderlistener </listener-class> </listener> <!-- intercept requests --> <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> <dispatcher>request</dispatcher> <dispatcher>error</dispatcher> </filter-mapping> <!-- srping mvc dispatcherservelt --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextclass</param-name> <param-value> org.springframework.web.context.support.annotationconfigwebapplicationcontext </param-value> </init-param> <init-param> <param-name>contextconfiglocation</param-name> <param-value> com.example.mymodule.app2.application </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <!-- mora preko https (app engine)--> <security-constraint> <web-resource-collection> <web-resource-name>question</web-resource-name> <url-pattern>/question/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>confidential</transport-guarantee> </user-data-constraint> </security-constraint> </web-app>

services.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <context:component-scan base-package="com.example.mymodule"> <context:exclude-filter type="regex" expression="com.example.mymodule.*"/> </context:component-scan> </beans>

and security.xml

<?xml version="1.0" encoding="utf-8"?> <b:beans xmlns="http://www.springframework.org/schema/security" xmlns:b="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.xsd"> <http auto-config="true"> <intercept-url access="role_user" pattern="/*" /> </http> <authentication-manager> <authentication-provider ref="mutibouserdetailsservice"/> </authentication-manager> </b:beans>

but every time tired test app error.

'org.springframework.security.authentication.dao.daoauthenticationprovider#0': cannot resolve reference bean 'mutibouserdetailsservice' while setting bean property 'userdetailsservice'; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no bean named 'mutibouserdetailsservice' defined.

can help me? thanks.

your problem has nil app engine fact spring container not able find bean named mutibouserdetailsservice.

i root cause component scanning configuration.

<context:component-scan base-package="com.example.mymodule"> <context:exclude-filter type="regex" expression="com.example.mymodule.*"/> </context:component-scan>

the class mutibouserdetailsservice define in bundle com.example.mymodule.app2, however, bundle excluded (see exclude-filter) component scanning , hence spring not register corresponding bean.

furthermore, there problem authentication manager configuration. follow excerpt below.

<authentication-manager> <authentication-provider user-service-ref='mutibouserdetailsservice'/> </authentication-manager>

java spring spring-security

No comments:

Post a Comment