PDA

View Full Version : Unable to use user-service-ref for authentication-provider in Spring Security.



mgervais@agaetis.fr
Mar 30th, 2012, 02:38 AM
Hi all,

I've a problem with Spring Security (3.1.0-RELEASE), maybe it's due to something else.

Here is my configuration:

web.xml (There is no xml configuration for the Dispatcher Servlet to be sure that all bean are in the parent context)


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/all.xml</param-value>
</context-param>

<servlet>
<servlet-name>all</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>



all.xml Include all configuration files.


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="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">
<import resource="classpath:spring/all-data-commons.xml" />
<import resource="./all-config.xml"/>
<import resource="./all-data.xml"/
<import resource="./all-crypto.xml"/
<import resource="./all-security.xml"/>
<import resource="./all-social.xml"/>
<import resource="./all-task.xml"/>
<import resource="./all-web.xml"/>
<import resource="./all-webflow.xml"/>
</beans>


This UserDetailService is located in a jar and should be discovered with component scan when I include the file classpath:spring/all-data-commons.xml with this content:


<context:annotation-config />
<context:component-scan base-package="com.xxx.xxx.data.commons" />


Here is my UserDetailService:


@Service("authUserService")
public class AuthUserServiceImpl implements AuthUserService {
private final AuthUserDao dao;

@Autowired
public AuthUserServiceImpl(AuthDao dao) {
this.dao = dao;
new Exception("instanciation").printStackTrace();
}

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
AuthUser user = dao.findByUsername(username);
if (user == null)
throw new UsernameNotFoundException("");
return user;
}
}


Finally here is my all-security.xml


<sec:http use-expressions="true" authentication-manager-ref="authenticationManager" >
<!-- Authentication policy -->
<sec:form-login login-page="/signin" login-processing-url="/signin/authenticate" authentication-failure-url="/signin?param.error=bad_credentials" />

<sec:logout logout-url="/signout" delete-cookies="JSESSIONID" invalidate-session="true" />

<sec:intercept-url pattern="/favicon.ico" access="permitAll" />
<sec:intercept-url pattern="/resources/**" access="permitAll" />
<sec:intercept-url pattern="/signin/**" access="permitAll" />
<sec:intercept-url pattern="/signup/**" access="permitAll" />
<sec:intercept-url pattern="/**" access="isAuthenticated()" />

<sec:remember-me data-source-ref="ds" />

<sec:session-management invalid-session-url="/signin" >
<sec:concurrency-control max-sessions="1" />
</sec:session-management>
</sec:http>
<sec:debug />
<sec:authentication-manager id="authenticationManager" alias="authenticationManager" erase-credentials="true" >
<sec:authentication-provider user-service-ref="authUserService">
<sec:password-encoder ref="passwordEncoder"/>
</sec:authentication-provider>
</sec:authentication-manager>


Last my exception:


Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'authUserService' defined in URL jar:file:/home/mgervais/Work/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/all-web/WEB-INF/lib/all-data-commons-1.0.0-SNAPSHOT.jar!/com/agaetis/xxxxx/data/commons/service/impl/AuthUserServiceImpl.class: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class com.agaetis.xxx.xxx.data.commons.service.impl.Auth UserServiceImpl: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.agaetis.xxx.xxx.data.commons.service.impl.Auth UserServiceImpl.<init>()
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:997)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:943)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 94)
at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:291 )
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeansOfType(DefaultListableB eanFactory.java:400)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeansOfType(DefaultListableB eanFactory.java:390)
at org.springframework.context.support.AbstractApplic ationContext.getBeansOfType(AbstractApplicationCon text.java:1158)
at org.springframework.security.config.http.UserDetai lsServiceFactoryBean.getBeansOfType(UserDetailsSer viceFactoryBean.java:114)
at org.springframework.security.config.http.UserDetai lsServiceFactoryBean.getUserDetailsService(UserDet ailsServiceFactoryBean.java:95)
at org.springframework.security.config.http.UserDetai lsServiceFactoryBean.cachingUserDetailsService(Use rDetailsServiceFactoryBean.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:149)
... 85 more
Caused by: org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class com.agaetis.xxx.xxx.data.commons.service.impl.Auth UserServiceImpl: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.agaetis.xxx.xxx.data.commons.service.impl.Auth UserServiceImpl.<init>()
at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:72)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:990)
... 103 more
Caused by: java.lang.NoSuchMethodException: com.agaetis.xxx.xxx.data.commons.service.impl.Auth UserServiceImpl.<init>()
at java.lang.Class.getConstructor0(Class.java:2706)
at java.lang.Class.getDeclaredConstructor(Class.java: 1985)
at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:67)
... 104 more


In some thread it's explained that's we should take care of the different context taht's why I've not declared a spring configuration file for the DispatcherServlet....

I don't understand why the AuthService is not autowired...

I've work on it several hours, any help would be appreciated...

Thanks

Mickael