spring cloud 问题记录(十三)authenticationManager无法注入问题

在配置spring cloud security的过程中出现如下异常信息导致无法启动项目

Field authenticationManager in com.clark.online.edu.config.AuthorizationServerConfig required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.

看错误信息就很明白了,就是需要authenticationManager,但是没有在config里面注入,但是实际上在我的config配置里面已经注入了,代码如下:

	/**
	 * 认证管理
	 */
	@Autowired
	private AuthenticationManager authenticationManager;
	/**
	 * redis连接工具
	 */
	@Autowired
	private RedisConnectionFactory redisConnectionFactory;
	
	/**
	 * redis存储token
	 * @return
	 */
	@Bean
	RedisTokenStore redisTokenStore() {
		return new RedisTokenStore(redisConnectionFactory);
	}
	
	/**
	 * 配置
	 */
	@Override 
	public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
		endpoints.tokenStore(redisTokenStore()).authenticationManager(authenticationManager);
	}

其实原因我不知道,解决方法很简单就是重新实例化bean就OK

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	/**
	 * 重新实例化bean
	 */
	@Override
	@Bean
	public AuthenticationManager authenticationManagerBean() throws Exception {
		return super.authenticationManagerBean();
	}
}

 

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Spring Cloud提供了OAuth2.0的集成支持,可以帮助我们快速实现OAuth2.0的认证和授权功能。在Spring Cloud中,我们可以使用Spring Security来实现OAuth2.0的认证和授权。 下面是一个简单的Spring Cloud OAuth2.0集成示例: 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency> ``` 2. 配置认证服务器 在认证服务器中,我们需要配置一些基本信息,如授权类型、客户端信息、用户信息等。下面是一个简单的认证服务器配置: ``` @Configuration @EnableAuthorizationServer public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientid") .secret("clientsecret") .authorizedGrantTypes("password", "refresh_token") .scopes("read", "write") .accessTokenValiditySeconds(3600) .refreshTokenValiditySeconds(86400); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } } ``` 3. 配置资源服务器 在资源服务器中,我们需要配置一些基本信息,如资源ID、访问规则等。下面是一个简单的资源服务器配置: ``` @Configuration @EnableResourceServer public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/**").authenticated() .anyRequest().permitAll(); } @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception { resources.resourceId("resourceid"); } } ``` 4. 配置安全 在安全配置中,我们需要配置一些基本信息,如安全规则、用户信息等。下面是一个简单的安全配置: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } } ``` 5. 测试 完成以上配置后,我们就可以测试OAuth2.0的认证和授权功能了。可以使用Postman等工具发送请求,获取访问令牌,然后使用访问令牌访问受保护的资源。 以上就是一个简单的Spring Cloud OAuth2.0集成示例。具体实现还需要根据实际业务需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值