Announcement Announcement Module
Collapse
No announcement yet.
Custom authentication provider. Why it can't be instantiated as a regular bean? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom authentication provider. Why it can't be instantiated as a regular bean?

    I spent two days trying to figure out why this doesn't work. And still no chance. So, if anyone, has any idea (wrong or right) on this, please, share it. I will appreciate that much.

    This is really a duplicate question I asked here http://forum.springsource.org/showth...e-instantiated, I need someone help me. Please.
    ================================================== ==========
    I'm building my own web application based on the Greenhouse as a great sample.
    The only differences are:
    1. I use XML for application configuration
    2. And I split separate part of the projects (persistence, services, social) into the separate modules/sub-projects.

    I've spent 2 days trying to figure out where is the difference between my application and the Greenhouse (Greenhouse runs fine from my IDE, and everything works as expected).
    The problem I have - container is not able to instantiate custom usernamePasswordAuthenticationProvider.
    There is the error:
    Could not instantiate bean class [com.myappl.service.UsernamePasswordAuthenticationP rovider]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.myappl.service.UsernamePasswordAuthenticationP rovider.<init>()
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:106)
    at org.springframework.beans.factory.support.Construc torResolver.resolveConstructorArguments(Constructo rResolver.java:616)
    at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:148)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:1035)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:939)
    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:193)
    at org.springframework.security.config.debug.Security DebugBeanFactoryPostProcessor.postProcessBeanFacto ry(SecurityDebugBeanFactoryPostProcessor.java:21)
    at org.springframework.context.support.AbstractApplic ationContext.invokeBeanFactoryPostProcessors(Abstr actApplicationContext.java:681)
    at org.springframework.context.support.AbstractApplic ationContext.invokeBeanFactoryPostProcessors(Abstr actApplicationContext.java:671)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:446)
    at org.springframework.web.context.ContextLoader.conf igureAndRefreshWebApplicationContext(ContextLoader .java:384)
    at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.
    ...
    I tried all imaginary possible ways to fix it. The reason for this, that I @Inject AuthenticationService through constructor injection for UsernamePasswordAuthenticationProvider, which is declared as @Service. Everything is very similar to the Greenhouse application, so it makes me crazy. Why do I need default no arg constructor? I tried to change configuration files order I use in web contextConfigLocation, but no luck.
    Is this specific for @Configuration configuration? I don't want to switch my application into @Configuration instead of XML, at least, if I have to, I would like to know how to fix this error.

    Please, also look into http://forum.springsource.org/showth...ing-form-login (the last answer) and http://forum.springsource.org/showth...ation-provider.
    I asked them for this issue, I describe here, but got no answers.
    Please, if anyone, has any idea or thoughts what can be wrong, share it with me, I will appreciate that much.
    Let me know, if you additional information for this issue.

  • #2
    Did you try adding a default constructor to your class?

    Comment


    • #3
      Yes, I think I can add a default constructor, but it is not the solution I like. And if in this case authentication provider will be instantiated authenticationService field will not be initialized (it is not initialized via field or method @Inject-ion too). The only solution I see (but I like very much that the current implementation works, because it works fine for the Greenhouse) is to implement ApplicationContextAware or @Inject application context, and get the beans from application context when authentication method is called (again if implements ApplicationContextAware in the method setApplicationContext() ref to the authenticationService is null, why? Actually not null, but the exception is thrown if attempts to create either AuthenticationSerive (no default constructor exception) or AccountRepository (expected at least one but found 0), I use Spring Data JPA for repository creation; but in the authenticate() method when using the same applicationContext everything is accessible).
      Everything works fine from JUnit/Integration test, AccountRepository is @Injected (constructor injection) into AuthenticationService, and AuthenticationService is @Injected (constructor injection) into UsernamePasswordAuthenticationProvider (see attached screenshot below).
      But this doesn't work from the real web application. I suppose the reason is that UsernamePasswordAuthenticationProvider can't be constructed using one arg constructor, because at the construction time AuthenticationService is not available (if I'm right, so I am interested why AuthenticationService is not available?).
      And again, the same UsernamePasswordAuthenticationProvider without default constructor works fine for the Greenhouse application. Is @Configuration configuration very different internally to the classical XML+namespaces configuration?

      Thank you.
      Attachment
      Attached Files
      Last edited by Aliaksandr; Jan 4th, 2012, 05:31 AM.

      Comment


      • #4
        If I remember right, the greenhouse application is using aspectj to weave the application during the build. Are you doing the same?

        Comment


        • #5
          If it is enough to include the following snippet into parent pom.xml:
          <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>aspectj-maven-plugin</artifactId>
          <version>1.3.1</version>
          <dependencies>
          <!-- Note: Maven 2.0.9 or > required or these are ignored (MNG-2972) -->
          <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>${org.aspectj-version}</version>
          </dependency>
          <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjtools</artifactId>
          <version>${org.aspectj-version}</version>
          </dependency>
          </dependencies>
          <executions>
          <execution>
          <goals>
          <goal>compile</goal>
          <goal>test-compile</goal>
          </goals>
          </execution>
          </executions>
          <configuration>
          <outxml>true</outxml>
          <aspectLibraries>
          <aspectLibrary>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aspects</artifactId>
          </aspectLibrary>
          </aspectLibraries>
          <source>1.6</source>
          <target>1.6</target>
          </configuration>
          </plugin>
          then yes, I use aspectj for weaving too. I also use
          <tx:annotation-driven mode="aspectj"/>
          the same as in the Greenhouse.

          Comment


          • #6
            Also, you can read http://forum.springsource.org/showth...order-creation, where I described my research about beans instantiation in my case.

            Comment


            • #7
              You might see if removing <debug/> from your configuration fixes the issue. I have noticed that under some circumstances adding <debug/> with a custom AuthenticationProvider that does not have a default constructor can cause issues. If you would like to track progress on the issue you can follow SEC-1885. Let me know if that resolves your issue.

              Comment


              • #8
                Thank you very very much. I think you are right. I haven't expected that this tag <debug/> can cause this error. Right now, I can report that adding <debug/> into the Greenhouse application results that the Greenhouse application fails with the same error I have.
                ...
                Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [com.springsource.greenhouse.account.UsernamePasswo rdAuthenticationProvider]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.springsource.greenhouse.account.UsernamePasswo rdAuthenticationProvider.<init>()
                ...
                My code is broken now (need to get used with Git's branches), so I surely report whether removing <debug/> fixes my application later.
                Someone needs to report this behavior/issue on the official Spring Security 3.1 reference guide.
                Thank you, again.

                Comment


                • #9
                  Yes, removing <debug/> from my security config resolved this issue.
                  Thank you.

                  Comment

                  Working...
                  X