Reputation: 473
Lately in my Spring REST API I've encountered a strange exception. Spring tells me that it doesn't find any bean of type org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
It doesn't show up everytime and one first fix was to restart IntelliJ IDEA a few minutes and then everything would work fine.
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.
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
@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
@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 :)
Upvotes: 2
Views: 1168
Reputation: 1
Annotate with @EnableWebSecurity
on the Class allows me to make security-related imports
Upvotes: 0
Reputation: 793
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
Upvotes: 0