[英]Spring No bean defined of type AuthenticationManagerBuilder
Lately in my Spring REST API I've encountered a strange exception. 最近在我的Spring REST API中,我遇到了一个奇怪的异常。 Spring tells me that it doesn't find any bean of type
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
Spring告诉我它找不到任何类型为
org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
bean
It doesn't show up everytime and one first fix was to restart IntelliJ IDEA a few minutes and then everything would work fine. 它并非每次都显示,并且第一个解决方法是重启IntelliJ IDEA几分钟,然后一切正常。
But now, it wouldn't deploy on my remote tomcat server because of that and I can't figure out what to do with it. 但是现在,由于这个原因,它不会部署在我的远程tomcat服务器上,而且我不知道该怎么做。
The error message : 错误消息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method authenticationManager in eu.side.thot.Application required a bean of type 'org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder' in your configuration.
Process finished with exit code 1
Here is my configuration 这是我的配置
Application.java Application.java
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
@Autowired
private CustomUserDetailsService userDetailsService;
/**
* Set AuthenticationManager his UserDetailsService and PasswordEncoder so he can authenticate an user with a given username/password
* */
@Autowired
public void authenticationManager(AuthenticationManagerBuilder builder) throws Exception{
builder.userDetailsService(userDetailsService).passwordEncoder(new MD5PasswordEncoder());
}
}
And the AuthorizationServerConfig.java 还有AuthorizationServerConfig.java
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
private Logger log = Logger.getLogger(AuthorizationServerConfig.class);
private static final int FIVE_HOURS_IN_S = 5*3600;
private static final int MONTH_IN_S = 31*24*3600;
private final AuthenticationManager authenticationManager;
@Autowired
public AuthorizationServerConfig(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@Override
public void configure(AuthorizationServerSecurityConfigurer serverSecurityConfigurer){
serverSecurityConfigurer
.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()")
.allowFormAuthenticationForClients();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception{
clients.inMemory().withClient("{client}")
.authorizedGrantTypes("client-credentials", "password", "refresh_token")
.authorities("ROLE_CLIENT", "ROLE_ANDROID_CLIENT")
.scopes("read", "write", "trust")
.resourceIds("oauth2resource")
.accessTokenValiditySeconds(FIVE_HOURS_IN_S)
.secret("{secret}").refreshTokenValiditySeconds(MONTH_IN_S);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints){
endpoints.authenticationManager(authenticationManager)
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
.exceptionTranslator(loggingExceptionTranslator());
}
@Bean
public WebResponseExceptionTranslator loggingExceptionTranslator() {
return new DefaultWebResponseExceptionTranslator() {
@Override
public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
// This is the line that prints the stack trace to the log. You can customise this to format the trace etc if you like
e.printStackTrace();
// Carry on handling the exception
ResponseEntity<OAuth2Exception> responseEntity = super.translate(e);
HttpHeaders headers = new HttpHeaders();
headers.setAll(responseEntity.getHeaders().toSingleValueMap());
OAuth2Exception excBody = responseEntity.getBody();
return new ResponseEntity<>(excBody, headers, responseEntity.getStatusCode());
}
};
}
}
Thanks for your help 谢谢你的帮助
UPDATE : My API works, I didn't change anything... If anyone have an idea of where the problem can come from I would love to hear about it to have a better understanding of spring :) 更新:我的API可以正常工作,我什么也没做...如果任何人对问题的根源有一个了解,我希望听到它以更好地了解spring :)
Seems like you missed 好像你错过了
compile group: 'org.springframework.security', name: 'spring-security-config', version: '5.0.3.RELEASE'
Also, users should utilize the Global AuthenticationManagerBuilder that is available when using @EnableWebSecurity
or @EnableGlobalMethodSecurity
此外,用户应使用在使用
@EnableWebSecurity
或@EnableGlobalMethodSecurity
时可用的Global AuthenticationManagerBuilder。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.