spring boot 启动报错:No qualifying bean of type org.springframework.security.oauth2.provider.token.Toke

spring boot启动报错: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘authorizationServerConfig’: Unsatisfied dependency expressed through field ‘tokenStore’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘org.springframework.security.oauth2.provider.token.TokenStore’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

具体错误:

Field tokenStore in com.mall.pro.security.config.AuthorizationServerConfig required a bean of type 'org.springframework.security.oauth2.provider.token.TokenStore' that could not be found.
The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
	- Bean method 'tokenStore' in 'AuthorizationServerTokenServicesConfiguration.JwtKeyStoreConfiguration' not loaded because OAuth JWT KeyStore Condition did not find provided key store location
	- Bean method 'jwtTokenStore' in 'AuthorizationServerTokenServicesConfiguration.JwtTokenServicesConfiguration' not loaded because OAuth JWT Condition did not find provided private or symmetric key
	- Bean method 'jwkTokenStore' in 'ResourceServerTokenServicesConfiguration.JwkTokenStoreConfiguration' not loaded because OAuth JWK Condition did not find key jwk set URI not provided
	- Bean method 'jwkTokenStore' in 'ResourceServerTokenServicesConfiguration.JwkTokenStoreConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration did not match
	- Bean method 'tokenStore' in 'ResourceServerTokenServicesConfiguration.JwtKeyStoreConfiguration' not loaded because OAuth JWT KeyStore Condition did not find key store location not provided
	- Bean method 'tokenStore' in 'ResourceServerTokenServicesConfiguration.JwtKeyStoreConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration did not match
	- Bean method 'jwtTokenStore' in 'ResourceServerTokenServicesConfiguration.JwtTokenServicesConfiguration' not loaded because OAuth JWT Condition did not find provided public key
	- Bean method 'jwtTokenStore' in 'ResourceServerTokenServicesConfiguration.JwtTokenServicesConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration did not match


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.security.oauth2.provider.token.TokenStore' in your configuration.

通过反复对比学习的项目框架,发现自己在搭建的时候,漏加了一个配置文件 TokenConfig.java:

package com.mall.pro.security.config;

import com.mall.pro.security.constants.SecurityConstants;
import com.mall.pro.security.util.SecurityTokenServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider;

import java.util.Collections;

/**
 * @author LGH
 */
@Configuration
public class TokenConfig {


    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Autowired
    private TokenEnhancer tokenEnhancer;

    @Bean
    public TokenStore tokenStore() {
        RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
        tokenStore.setPrefix(SecurityConstants.YAMI_OAUTH_PREFIX);
        return tokenStore;
    }


    @Primary
    @Bean
    @Lazy
    public AuthorizationServerTokenServices yamiTokenServices() {
    	SecurityTokenServices tokenServices = new SecurityTokenServices();
        tokenServices.setTokenStore(tokenStore());
        //支持刷新token
        tokenServices.setSupportRefreshToken(true);
        tokenServices.setReuseRefreshToken(true);
        tokenServices.setTokenEnhancer(tokenEnhancer);
        addUserDetailsService(tokenServices);
        return tokenServices;
    }

    private void addUserDetailsService(SecurityTokenServices tokenServices) {
        PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();
        provider.setPreAuthenticatedUserDetailsService(new UserDetailsByNameServiceWrapper<>(userDetailsService));
        tokenServices.setAuthenticationManager(new ProviderManager(Collections.singletonList(provider)));
    }

}

总结,遇到此类Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
请认真检查自己的配置文件是否存在,这可能是造成错误的主要原因之一!


版权声明:本文为lzf2284466原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。