最近在我的Spring REST API中,我遇到了一个奇怪的异常。 Spring告诉我它没有找到org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
它并不是每次出现而且第一次修复是在几分钟后重新启动IntelliJ IDEA然后一切都会正常工作。
但是现在,它不会在我的远程tomcat服务器上部署,因此我无法弄清楚如何处理它。
错误消息:
***************************
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
这是我的配置
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());
}
}
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());
}
};
}
}
感谢您的帮助
更新:我的API有效,我没有改变任何东西......如果有人知道问题可能来自哪里,我很乐意听到它有更好的理解春天:)
答案 0 :(得分:0)
好像你错过了
compile group: 'org.springframework.security', name: 'spring-security-config', version: '5.0.3.RELEASE'
此外,用户应使用使用@EnableWebSecurity
或@EnableGlobalMethodSecurity