java - cannot convert to required type [org.springframework.security.core.userdetails.UserDetailsService] for property 'userDetailsService': -
hello s first time using spring security. can please me! here console :
grave: exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.contextloaderlistener org.springframework.beans.factory.beancreationexception: error creating bean name 'authenticationctrl': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private org.springframework.security.authentication.authenticationmanager tn.talan.project003.authenticationctrl.authmanager; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'authenticationmanager': cannot resolve reference bean 'org.springframework.security.authentication.dao.daoauthenticationprovider#0' while setting constructor argument key [0]; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.security.authentication.dao.daoauthenticationprovider#0': initialization of bean failed; nested exception org.springframework.beans.conversionnotsupportedexception: failed convert property value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice'; nested exception java.lang.illegalstateexception: cannot convert value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice': no matching editors or conversion strategy found @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:334) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1214) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:543) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:772) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:839) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:538) @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:444) @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:326) @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:107) @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4812) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5255) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:147) @ org.apache.catalina.core.containerbase$startchild.call(containerbase.java:1408) @ org.apache.catalina.core.containerbase$startchild.call(containerbase.java:1398) @ java.util.concurrent.futuretask.run(futuretask.java:266) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ java.lang.thread.run(thread.java:745) caused by: org.springframework.beans.factory.beancreationexception: not autowire field: private org.springframework.security.authentication.authenticationmanager tn.talan.project003.authenticationctrl.authmanager; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'authenticationmanager': cannot resolve reference bean 'org.springframework.security.authentication.dao.daoauthenticationprovider#0' while setting constructor argument key [0]; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.security.authentication.dao.daoauthenticationprovider#0': initialization of bean failed; nested exception org.springframework.beans.conversionnotsupportedexception: failed convert property value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice'; nested exception java.lang.illegalstateexception: cannot convert value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice': no matching editors or conversion strategy found @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:573) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:88) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:331) ... 22 more caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'authenticationmanager': cannot resolve reference bean 'org.springframework.security.authentication.dao.daoauthenticationprovider#0' while setting constructor argument key [0]; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.security.authentication.dao.daoauthenticationprovider#0': initialization of bean failed; nested exception org.springframework.beans.conversionnotsupportedexception: failed convert property value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice'; nested exception java.lang.illegalstateexception: cannot convert value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice': no matching editors or conversion strategy found @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolvereference(beandefinitionvalueresolver.java:359) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolvevalueifnecessary(beandefinitionvalueresolver.java:108) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolvemanagedlist(beandefinitionvalueresolver.java:382) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolvevalueifnecessary(beandefinitionvalueresolver.java:157) @ org.springframework.beans.factory.support.constructorresolver.resolveconstructorarguments(constructorresolver.java:634) @ org.springframework.beans.factory.support.constructorresolver.autowireconstructor(constructorresolver.java:140) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.autowireconstructor(abstractautowirecapablebeanfactory.java:1143) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1046) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:510) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.findautowirecandidates(defaultlistablebeanfactory.java:1192) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1116) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1014) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:545) ... 24 more caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.security.authentication.dao.daoauthenticationprovider#0': initialization of bean failed; nested exception org.springframework.beans.conversionnotsupportedexception: failed convert property value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice'; nested exception java.lang.illegalstateexception: cannot convert value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice': no matching editors or conversion strategy found @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:553) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) @ org.springframework.beans.factory.support.beandefinitionvalueresolver.resolvereference(beandefinitionvalueresolver.java:351) ... 41 more caused by: org.springframework.beans.conversionnotsupportedexception: failed convert property value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice'; nested exception java.lang.illegalstateexception: cannot convert value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice': no matching editors or conversion strategy found @ org.springframework.beans.abstractnestablepropertyaccessor.convertifnecessary(abstractnestablepropertyaccessor.java:591) @ org.springframework.beans.abstractnestablepropertyaccessor.convertforproperty(abstractnestablepropertyaccessor.java:603) @ org.springframework.beans.beanwrapperimpl.convertforproperty(beanwrapperimpl.java:204) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.convertforproperty(abstractautowirecapablebeanfactory.java:1527) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.applypropertyvalues(abstractautowirecapablebeanfactory.java:1486) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1226) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:543) ... 47 more caused by: java.lang.illegalstateexception: cannot convert value of type [tn.talan.springsecurity.configuration.customuserdetailsserviceimpl] required type [org.springframework.security.core.userdetails.userdetailsservice] property 'userdetailsservice': no matching editors or conversion strategy found @ org.springframework.beans.typeconverterdelegate.convertifnecessary(typeconverterdelegate.java:302) @ org.springframework.beans.abstractnestablepropertyaccessor.convertifnecessary(abstractnestablepropertyaccessor.java:576) ... 53 more
my springsecurity.xml :
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:security="http://www.springframework.org/schema/security" 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.2.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd"> <security:authentication-manager id="authenticationmanager"> <security:authentication-provider user-service-ref="userdetailsservice"> </security:authentication-provider> </security:authentication-manager> <security:http realm="protected api" use-expressions="true" auto-config="false" create-session="stateless" entry-point-ref="unauthorizedentrypoint" authentication-manager-ref="authenticationmanager"> <security:custom-filter ref="authenticationtokenprocessingfilter" position="form_login_filter" /> <security:intercept-url pattern="/services/authenticate" access="permitall" /> <security:intercept-url method="get" pattern="/services/users/**" access="isauthenticated()" /> <security:intercept-url method="post" pattern="/services/users/**" access="isauthenticated()" /> <security:intercept-url pattern="/services/users/**" access="hasrole('role_admin')" /> <security:intercept-url pattern="/services/**" access="isauthenticated()" /> </security:http> </beans>
customuserdetailsservice.java
import org.springframework.beans.factory.annotation.autowired; import org.springframework.security.core.userdetails.userdetails; import org.springframework.security.core.userdetails.usernamenotfoundexception; import tn.talan.model.utilisateur; import tn.talan.springdata.repo.userrepository; public class customuserdetailsserviceimpl implements customuserdetailsservice { @autowired userrepository userbean; @override public userdetails loaduserbyusername(string username) throws usernamenotfoundexception { utilisateur user = userbean.findbylogin(username); system.out.println("user : "+user); if(user==null){ system.out.println("user not found"); throw new usernamenotfoundexception("username not found"); } return user; } }
actually i'm getting compliation error because user of type utilisateur , not userdetails
authenticationprocessingfilter.java
import java.io.ioexception; import javax.servlet.filterchain; import javax.servlet.servletexception; import javax.servlet.servletrequest; import javax.servlet.servletresponse; import javax.servlet.http.httpservletrequest; import org.springframework.beans.factory.annotation.autowired; import org.springframework.security.authentication.usernamepasswordauthenticationtoken; import org.springframework.security.core.context.securitycontextholder; import org.springframework.security.core.userdetails.userdetails; import org.springframework.security.core.userdetails.userdetailsservice; import org.springframework.security.web.authentication.webauthenticationdetailssource; import org.springframework.stereotype.component; import org.springframework.web.filter.genericfilterbean; import tn.talan.springdata.service.tokenutils; @component("authenticationtokenprocessingfilter") public class authenticationtokenprocessingfilter extends genericfilterbean { private final userdetailsservice userservice; private tokenutils tokenutils; @autowired public authenticationtokenprocessingfilter(userdetailsservice userservice, tokenutils tokenutils) { this.userservice = userservice; this.tokenutils = tokenutils; } @override public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { if (!(request instanceof httpservletrequest)) { throw new runtimeexception("expecting http request"); } httpservletrequest httprequest = (httpservletrequest) request; string authtoken = httprequest.getheader("x-auth-token"); string username = tokenutils.getusernamefromtoken(authtoken); if (username != null) { userdetails userdetails = this.userservice.loaduserbyusername(username); if (tokenutils.validatetoken(authtoken, userdetails)) { usernamepasswordauthenticationtoken authentication = new usernamepasswordauthenticationtoken(userdetails, null, userdetails.getauthorities()); authentication.setdetails(new webauthenticationdetailssource().builddetails((httpservletrequest) request)); securitycontextholder.getcontext().setauthentication(authentication); } } chain.dofilter(request, response); } }
and springmvc controller:
import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.security.authentication.authenticationmanager; import org.springframework.security.authentication.usernamepasswordauthenticationtoken; import org.springframework.security.core.authentication; import org.springframework.security.core.context.securitycontextholder; import org.springframework.security.core.userdetails.userdetails; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.responsebody; import tn.talan.model.utilisateur; import tn.talan.springdata.service.tokenutils; import tn.talan.springsecurity.configuration.customuserdetailsservice; @controller @requestmapping("/services") public class authenticationctrl { @autowired private customuserdetailsservice userservice; @autowired private tokenutils tokenutils; @autowired @qualifier("authenticationmanager") private authenticationmanager authmanager; @responsebody @requestmapping(value = "/authenticate", method = requestmethod.post, produces = "application/json") public utilisateur authenticate(@requestparam("username") string username, @requestparam("password") string password){ usernamepasswordauthenticationtoken authenticationtoken = new usernamepasswordauthenticationtoken(username, password); authentication authentication = this.authmanager.authenticate(authenticationtoken); securitycontextholder.getcontext().setauthentication(authentication); utilisateur user = (utilisateur) this.userservice.loaduserbyusername(username); user.settoken(tokenutils.createtoken((userdetails) user)); return user; } }
utilisateur.java :my model
import java.io.serializable; import java.util.arraylist; import java.util.collection; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.manytoone; import javax.persistence.namedquery; import javax.persistence.table; import javax.persistence.transient; import org.springframework.security.core.grantedauthority; import org.springframework.security.core.authority.simplegrantedauthority; /** * persistent class users database table. * */ @entity @table(name="users") @namedquery(name="utilisateur.findall", query="select u utilisateur u") public class utilisateur implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy=generationtype.auto) private long iduser; private string firstname; private string lastname; private string login; private string password; @transient private string token; @column(name="\"role\"") private string role; public collection<? extends grantedauthority> getauthorities() { grantedauthority authority = new simplegrantedauthority("role_" + this.role); arraylist<grantedauthority> authorities = new arraylist<grantedauthority>(); authorities.add(authority); return authorities; } @manytoone(fetch=fetchtype.eager,targetentity=agence.class) private agence agence; public utilisateur() { } public long getiduser() { return this.iduser; } public void setiduser(long iduser) { this.iduser = iduser; } public string getfirstname() { return this.firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return this.lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getlogin() { return this.login; } public void setlogin(string login) { this.login = login; } public string getpassword() { return this.password; } public void setpassword(string password) { this.password = password; } public string getrole() { return this.role; } public void setrole(string role) { this.role = role; } public agence getagence() { return agence; } public void setagence(agence agence) { this.agence = agence; } public void settoken(string token){ this.token = token; } public string gettoken(){ return token; }
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>servlet 3.0 web application</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/dispatcher-servlet.xml </param-value> </context-param> <!-- <context-param> <param-name>contextconfiglocation</param-name> <param-value> classpath:applicationcontext.xml </param-value> </context-param> --> <listener> <listener-class>org.springframework.web.context.request.requestcontextlistener</listener-class> </listener> <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> <display-name>talanpfe</display-name> </web-app>
Comments
Post a Comment