NoSuchBeanDefinitionException:类型为“ org.springframework.security.authentication.AuthenticationManager”的合格Bean不可用

时间:2019-03-11 15:17:24

标签: spring spring-boot gradle spring-security spring-security-oauth2

我正在使用Gradle,Spring Boot和Spring Security开发Rest API。该API将使用令牌身份验证进行保护。

但是,当我运行Spring Boot应用程序时,出现以下错误:

NoSuchBeanDefinitionException:没有类型为'org.springframework.security.authentication.AuthenticationManager'的合格Bean

这是WebSecurityConfig.java,其中定义了AuthenticationManager bean:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)  
@Import({SkyscannerBeanConfiguration.class})
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private MyUserDetailsService myUserDetailsService;

@Bean
@Override
public AuthenticationManager authenticationManager() throws Exception{
    return super.authenticationManager();
}

@Bean
public BCryptPasswordEncoder passwordEncoder(){
    BCryptPasswordEncoder bCryptPasswordEncoder=new 
BCryptPasswordEncoder(11);
    return bCryptPasswordEncoder;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws 
Exception{
}

@Override
protected void configure(HttpSecurity http) throws Exception{

        http
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .httpBasic()
                .realmName(securityRealm)
                .and()
                .csrf()
                .disable();
}

@Bean
public JwtAccessTokenConverter accessTokenConverter(){
    JwtAccessTokenConverter converter=new JwtAccessTokenConverter();
    converter.setSigningKey(signingKey);
    return converter;
}

@Bean
public TokenStore tokenStore(){
    return new JwtTokenStore(accessTokenConverter());
}

@Bean
@Primary
public DefaultTokenServices tokenServices(){
    DefaultTokenServices defaultTokenServices=new 
DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    return defaultTokenServices;
 }
} 

这是AuthorizationServerConfig.java,其中AuthenticationManager bean是自动接线的。

@Configuration
@EnableAuthorizationServer
@Import({WebSecurityConfig.class,SkyscannerBeanConfiguration.class})
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private TokenStore tokenStore;

@Autowired
private JwtAccessTokenConverter 
accessTokenConverter;

@Autowired
private AuthenticationManager 
authenticationManager;

@Autowired
private PasswordEncoder passwordEncoder;

@Override
public void  
configure(ClientDetailsServiceConfigurer 
configurer) throws Exception{
    configurer
            .inMemory()
            .withClient(clientId)

.secret(passwordEncoder.encode(clientSecret))
            .authorizedGrantTypes(grantType)
            .scopes(scopeRead,scopeWrite)
            .resourceIds(resourceIds);
}

@Override
public void 
configure(AuthorizationServerEndpointsConfigurer 
endpoints) throws Exception{
    TokenEnhancerChain enhancerChain=new 
TokenEnhancerChain();


enhancerChain.setTokenEnhancers(Arrays.asList(accessTokenConverter));
        endpoints.tokenStore(tokenStore)
                .accessTokenConverter(accessTokenConverter)
                .tokenEnhancer(enhancerChain)
                .authenticationManager(authenticationManager);


 }
}

这是SpringBoot应用程序文件:

@ComponentScan("com.skyscanner.config")
@Import({WebConfig.class, SkyscannerBeanConfiguration.class,WebSecurityConfig.class, WebDatabaseConfig.class, AuthorizationServerConfig.class, AdditionalWebConfig.class})
@SpringBootApplication
public class DataServiceApplication {
    public static void main(String[] args){
        SpringApplication.run(DataServiceApplication.class,args);
    }
}

这是build.gradle文件:

 buildscript {
    repositories {
        mavenCentral()
    }
    ext {
        springBootVersion = '2.1.3.RELEASE'
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
        classpath "io.spring.gradle:dependency-management-plugin:0.5.3.RELEASE"
    }
}



plugins {
    id 'java'
}
group 'com.skyscanner'
version '1.0-SNAPSHOT'


apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


bootJar {
    baseName = 'skyscanner-data-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile "antlr:antlr:2.7.7"
    compile "cglib:cglib-nodep:2.2"
    compile "log4j:log4j:1.2.17"
    compile "javax.transaction:jta:1.1"
    compile "com.fasterxml.jackson.core:jackson-core:2.7.4"
    compile "org.codehaus.jackson:jackson-mapper-asl:1.9.10"

    compile("org.modelmapper:modelmapper:2.3.2");
    compile "org.apache.xmlbeans:xmlbeans:2.4.0"
    compile "commons-fileupload:commons-fileupload:1.2.1"
    compile "commons-pool:commons-pool:1.5.4"
    compile "commons-dbcp:commons-dbcp:1.3"
    compile "commons-collections:commons-collections:3.2.2"
    runtime('mysql:mysql-connector-java')
    //  providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    compile "org.springframework.security:spring-security-jwt:1.0.7.RELEASE"
    compile "org.springframework.security.oauth:spring-security-oauth2:2.1.0.RELEASE"
    //compile group: 'org.springframework.boot', name: 'spring-boot', version: '2.1.3.RELEASE'
    // compile "org.springframework.boot:spring-boot-starter-parent:2.0.3.RELEASE"


    compile "commons-dbcp:commons-dbcp:1.3"
    compile "javax.servlet:jstl:1.2"
    compile group: 'org.springframework.security', name: 'spring-security-taglibs', version: '4.1.1.RELEASE'
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

task getDeps(type: Copy) {
    from configurations.compile
    into 'lib/'
}

0 个答案:

没有答案