我遵循了这里提到的一些建议,但是对我来说不起作用。因此,将问题放在这里
任何人都可以指导我解决什么问题以及如何解决该问题吗?
错误:
***************************
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.
AuthorizationServerConfig.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);
}
}
ResourceServerConfig.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父版本更新为2.0.4.RELEASE
,事情开始崩溃。
答案 0 :(得分:16)
似乎这是Spring Boot 2.0引入的“重大更改”之一。我相信您的案件已在Spring Boot 2.0 Migration Guide中进行了描述。
在您的ResourceServerConfig
类中,您需要覆盖authenticationManagerBean
方法并用@Bean
对其进行注释,即:
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
此外,在您的ResourceServerConfig
中,无需向AuthenticationManager
注入@Autowired
实例,您可以使用authenticationManagerBean()
方法,即:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.parentAuthenticationManager(authenticationManagerBean());
.userDetailsService(customUserDetailsService);
}
答案 1 :(得分:0)
just add this to the AuthenticationManagerBuilder
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
and in your controller where you need to use it add this :
@Autowired
private AuthenticationManager authenticationManager;