NoSuchBeanDefinitionException: нет подходящего бина типа 'org.springframework.security.authentication.AuthenticationManager', доступного для gradle - PullRequest
1 голос
/ 11 марта 2019

Я разрабатываю Rest API с Gradle, Spring Boot и Spring Security. API будет защищен с помощью токена аутентификации.

Однако, когда я запускаю приложение Spring Boot, я получаю следующую ошибку:

NoSuchBeanDefinitionException: нет подходящего компонента типа org.springframework.security.authentication.AuthenticationManager 'доступен

Вот WebSecurityConfig.java, где определен bean-компонент AuthenticationManager:

@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, в котором bean-компонент AuthenticationManager подключен автоматически.

@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/'
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...