java - No qualifying bean of type found for dependency in AuthenticationProvider -
hi i'm trying custom authenticationprovider i'm, having torubles autowire annotation. have class find users in database , authenticate them ldap server, have logic cant manage pass autowire error.
this class implements websecurityconfigureradapter
package com.abc.config; @configuration @enablewebmvcsecurity @componentscan(basepackages = {"com.abc.auth"}) public class configsecurity extends websecurityconfigureradapter { @autowired private authprovider authent; <--- autowire gives me troubles @override protected void configure( httpsecurity http ) throws exception { http .authenticationprovider(authent) .authorizerequests() .anyrequest().authenticated() .and() .formlogin() .loginpage("/prueba") .permitall() .and() .logout() .permitall(); } }
this jpa configuration class
package com.abc.config; @configuration @enablespringconfigured @componentscan( basepackages = {"com.abc.dom", "com.abc.repo", "com.abc.auth"}) @enablejparepositories(basepackages="com.abc.repo") public class configjpa { @bean public localcontainerentitymanagerfactorybean entitymanagerfactory() throws classnotfoundexception { localcontainerentitymanagerfactorybean em = new localcontainerentitymanagerfactorybean(); em.setdatasource( datasource() ); em.setpackagestoscan("com.abc.dom"); em.setpersistenceproviderclass(hibernatepersistence.class); em.setjpaproperties( asignproperties() ); return em; } //propiedades hibernate properties asignproperties() { properties jpaproperties = new properties(); jpaproperties.put("hibernate.dialect", "org.hibernate.dialect.oracle10gdialect"); jpaproperties.put("hibernate.format_sql", true); jpaproperties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.improvednamingstrategy"); jpaproperties.put("hibernate.show_sql", false); return jpaproperties; } @bean public datasource datasource(){ drivermanagerdatasource datasource = new drivermanagerdatasource(); datasource.setdriverclassname("oracle.jdbc.driver.oracledriver"); datasource.seturl("jdbc:oracle:thin:@127.0.0.1:1521:xe"); datasource.setusername("theuser"); datasource.setpassword("mypasswordtoaskquestions"); return datasource; } @bean public jpatransactionmanager transactionmanager() throws classnotfoundexception { jpatransactionmanager transactionmanager = new jpatransactionmanager(); transactionmanager.setentitymanagerfactory(entitymanagerfactory().getobject()); return transactionmanager; } }
and class have authenticate methods
package com.abc.auth; public class authprovider implements authenticationprovider { @autowired private userrepo repository_user; public authentication authenticate(authentication authentication) throws authenticationexception { userclass user = null; authentication auth = null; string name = null; string password = null; try { name = authentication.getname(); password = authentication.getcredentials().tostring(); if( name != null && !name.trim().equals("") && password != null && !password.trim().equals("") ) { user = this.finduserinthedb(name); //this method returns user if(user != null) { if( authuserldap(user .getldapserver().getdirection(), user .getldapserver().getdom(), name, password, true) ) <-- method authenticate user ldap { list<grantedauthority> grantedauths = new arraylist(); grantedauths.add(new simplegrantedauthority(usuario.getrole().getname())); auth = new usernamepasswordauthenticationtoken(name, password, grantedauths); } else { throw new badcredentialsexception("invalid credentials"); } } else { throw new usernamenotfoundexception("the user dont exist"); } } else { throw new badcredentialsexception("invalid credentials"); } } catch (authenticationexception e) { throw e; } catch (exception ex) { throw new authenticationserviceexception("", ex.getcause()); } return auth; } private userclass finduserinthedb(string login) { userclass user = null; if(login != null && !login.trim().equalsignorecase("")) { user = repository_user.findbylogin(login); } return user ; } public boolean supports(class<?> arg0) { return arg0.equals(usernamepasswordauthenticationtoken.class); } private boolean authuserldap(string server, string dom, string login, string password, boolean dev) { string initctx = "com.sun.jndi.ldap.ldapctxfactory"; hashtable<string, string> env = new hashtable<string, string>(); //para propositos de prueba en desarrollo if(dev)return true; try { env.put(context.initial_context_factory, initctx); env.put(context.provider_url, "ldap://" + server); env.put(context.security_authentication, "simple"); env.put(context.security_credentials, password); env.put(context.security_principal, login + dominio); dircontext ctx = new initialdircontext(env); } catch (exception e) { return false; } return true; } public void setrepositoryuser(userrepo userrepo ) { this.repository_user= userrepo ; } }
the userrepo class
package com.abc.repo; public interface userrepo extends jparepository <userclass, long> { public userclass findbylogin(string login); }
this console output
info : org.springframework.web.context.contextloader - root webapplicationcontext: initialization started info : org.springframework.web.context.support.annotationconfigwebapplicationcontext - refreshing root webapplicationcontext: startup date [wed sep 10 08:09:19 vet 2014]; root of context hierarchy info : org.springframework.web.context.support.annotationconfigwebapplicationcontext - registering annotated classes: [class com.abc.config.configsecurity] info : org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor - jsr-330 'javax.inject.inject' annotation found , supported autowiring error: org.springframework.web.context.contextloader - context initialization failed org.springframework.beans.factory.beancreationexception: error creating bean name 'configsecurity': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.abc.auth.authprovider com.abc.config.configsecurity.authent; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.abc.auth.authprovider] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:292) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1185) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:537) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:475) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:228) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:298) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:193) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:703) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:760) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:482) @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:403) @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:306) @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:106) @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4971) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5467) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:150) @ org.apache.catalina.core.containerbase.addchildinternal(containerbase.java:901) @ org.apache.catalina.core.containerbase.addchild(containerbase.java:877) @ org.apache.catalina.core.standardhost.addchild(standardhost.java:632) @ org.apache.catalina.startup.hostconfig.deploydescriptor(hostconfig.java:670) @ org.apache.catalina.startup.hostconfig$deploydescriptor.run(hostconfig.java:1839) @ java.util.concurrent.executors$runnableadapter.call(unknown source) @ java.util.concurrent.futuretask.run(unknown source) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ java.lang.thread.run(unknown source) caused by: org.springframework.beans.factory.beancreationexception: not autowire field: private com.abc.auth.authprovider com.abc.config.configsecurity.authent; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.abc.auth.authprovider] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:508) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:87) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:289) ... 26 more caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.abc.auth.authprovider] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:1103) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:963) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:858) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:480) ... 28 more
Comments
Post a Comment