2016-06-22 7 views
2

Ich versuche, OAuth2 Server Provider mit diesem Lernprogramm Spring OAuth2 zu erstellen. Der Hauptunterschied zwischen Beispiel und meinem Projekt - Ich benutze Spring Boot nicht. Ich versuche, diese Klassen zu spalten (GitHub example link) Ich habe zwei Klassen:Autowire-Feld konnte nicht erstellt werden: private org.springframework.security.authentication.AuthenticationManager

@Configuration 
@Order(-20) 
@EnableResourceServer 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
    http.formLogin().loginPage("/login").permitAll() 
      .and() 
      .requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access") 
      .and() 
      .authorizeRequests().anyRequest().authenticated(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.parentAuthenticationManager(authenticationManager); 
    } 

}

Zweite Klasse:

@Configuration 
@EnableAuthorizationServer 
@EnableResourceServer 
public class OAuth2AuthorizationConfiguration extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Bean 
    public JwtAccessTokenConverter jwtAccessTokenConverter() { 
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 
    KeyPair keyPair = new KeyStoreKeyFactory(
      new ClassPathResource("keystore.jks"), "foobar".toCharArray()) 
      .getKeyPair("test"); 
    converter.setKeyPair(keyPair); 
    return converter; 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
    clients.inMemory() 
      .withClient("acme") 
      .secret("acmesecret") 
      .authorizedGrantTypes("authorization_code", "refresh_token", 
        "password").scopes("openid").autoApprove(true).accessTokenValiditySeconds(30); 
} 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
    endpoints.authenticationManager(authenticationManager).accessTokenConverter(
      jwtAccessTokenConverter()); 
    } 

    @Override 
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
    oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess(
      "isAuthenticated()"); 
    } 

} 

Aber wenn ich versuche, diesen Code zu starten, erhalte ich:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OAuth2AuthorizationConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.authentication.AuthenticationManager com.example.configuration.security.OAuth2AuthorizationConfiguration.authenticationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.authentication.AuthenticationManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) 
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) 
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) 
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5066) 
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) 
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899) 
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875) 
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) 
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1863) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) 
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618) 
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) 
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) 
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) 
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) 
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) 
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324) 
at sun.rmi.transport.Transport$1.run(Transport.java:200) 
at sun.rmi.transport.Transport$1.run(Transport.java:197) 
at java.security.AccessController.doPrivileged(Native Method) 
at sun.rmi.transport.Transport.serviceCall(Transport.java:196) 
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) 
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) 
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) 
at java.security.AccessController.doPrivileged(Native Method) 
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.authentication.AuthenticationManager com.example.configuration.security.OAuth2AuthorizationConfiguration.authenticationManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.authentication.AuthenticationManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) 
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 
... 58 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.authentication.AuthenticationManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) 
... 60 more 

Ich denke, das Problem ist in Spring Boot, dass ich nicht in meiner App verwenden. Ich habe folgende Lösungen versucht: First, Second Ich verwende nur Code-basierte Konfiguration. Zusatzinfo: Java 8 feder-sicherheits-oauth2 - 2.0.7.RELEASE, feder-sicherheits-version - 4.0.1.RELEASE, feder-version - 4.0.6.RELEASE.

Wie es zu beheben?

UPDATE

habe ich

@Configuration 
public class AuthManager extends WebSecurityConfigurerAdapter { 

    @Bean 
    public AuthenticationManager customAuthenticationManager() throws Exception { 
     return authenticationManager(); 
    } 

} 

Und ich habe jetzt Ausnahme:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.authentication.AuthenticationManager org.com.example.configuration.security.WebSecurityConfig.authenticationManager; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'customAuthenticationManager': Requested bean is currently in creation: Is there an unresolvable circular reference? 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 
    ... 100 more 
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'customAuthenticationManager': Requested bean is currently in creation: Is there an unresolvable circular reference? 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:335) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1017) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) 

Diese Ausnahme in OAuth2AuthorizationConfiguration Klasse.

Antwort

2

Erstellen Sie eine andere WebSecurityConfigurerAdapter Klasse, die die „main“ Konfiguration und fügen Sie den folgenden Code, um eine AuthenticationManager Bohne aussetzen wird:

@Bean 
public AuthenticationManager customAuthenticationManager() throws Exception { 
    return authenticationManager(); 
} 
+0

Bitte überprüfen Sie mein Update in Frage –

+0

Ich habe dies hinzugefügt, weil ich den gleichen Fehler in meiner Testumgebung habe. Dies löste meine Testumgebung, aber ich kann mich nicht einloggen Ich habe 401 Fehler. – BigDong

0

Lösung: Nur protected void configure(AuthenticationManagerBuilder auth) Methode entfernen. Laut Javadoc brauche ich diese Methode nicht.