我遵循了这里提到的一些建议,但它对我不起作用。因此,在这里提出问题
谁能指导我是什么问题以及如何解决这个问题?
错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field authenticationManager in com.techprimers.security.springsecurityauthserver.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.
authorization server config.Java
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("ClientId")
.secret("secret")
.authorizedGrantTypes("authorization_code")
.scopes("user_info")
.autoApprove(true);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
}
resource server config.Java
@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService customUserDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatchers()
.antMatchers("/login", "/oauth/authorize")
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.parentAuthenticationManager(authenticationManager)
.userDetailsService(customUserDetailsService);
}
}
从https://github.com/TechPrimers/spring-security-oauth-mysql-example获取的代码引用,只更新了Spring Boot Parent Version到2.0.4.RELEASE
,事情开始破坏。
这似乎是Spring Boot 2.0引入的“突破性变化”之一。我相信你的情况在Spring Boot 2.0 Migration Guide中有描述。
在你的WebSecurityConfigurerAdapter
类中,你需要覆盖authenticationManagerBean
方法并用@Bean
注释它,即:
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
此外,在你的WebSecurityConfigurerAdapter
而不是用AuthenticationManager
注入@Autowired
实例,你可以使用authenticationManagerBean()
方法,即:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.parentAuthenticationManager(authenticationManagerBean());
.userDetailsService(customUserDetailsService);
}