Jef*_*ook 12 spring spring-security spring-boot
我遵循了这里提到的一些建议,但它对我不起作用.因此,在这里提出问题
谁能指导我是什么问题以及如何解决这个问题?
错误:
***************************
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.
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
代码参考来自https://github.com/TechPrimers/spring-security-oauth-mysql-example,只更新了Spring Boot Parent Version 2.0.4.RELEASE
,开始了.
Pog*_*ger 33
这似乎是Spring Boot 2.0引入的"突破性变化"之一.我相信您的情况在Spring Boot 2.0迁移指南中有所描述.
在你的WebSecurityConfigurerAdapter
类中,你需要覆盖authenticationManagerBean
方法并用它注释@Bean
,即:
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
Run Code Online (Sandbox Code Playgroud)
而且,在您使用该方法WebSecurityConfigurerAdapter
而不是注入AuthenticationManager
实例时,即:@Autowired
authenticationManagerBean()
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.parentAuthenticationManager(authenticationManagerBean());
.userDetailsService(customUserDetailsService);
}
Run Code Online (Sandbox Code Playgroud)
YCF*_*F_L 13
由于WebSecurityConfigurerAdapter
它已被弃用,您现在可以使用:
@Bean
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
return http.getSharedObject(AuthenticationManagerBuilder.class)
.build();
}
Run Code Online (Sandbox Code Playgroud)
小智 5
只需将其添加到 AuthenticationManagerBuilder
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
Run Code Online (Sandbox Code Playgroud)
在需要使用它的控制器中添加以下内容:
@Autowired
private AuthenticationManager authenticationManager;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16811 次 |
最近记录: |