社区!
这是我的情况。我有春季启动应用程序。它使用maven构建并打包在jar文件中。问题是当我在我的开发PC上构建它并且运行良好时。如果我在我们的暂存环境(即linux)上运行此jar文件,它也运行良好。但是,如果我在暂存环境中构建它(当然这是由我们的Jenkins服务器完成的),它在启动期间失败,但例外情况是:
2016-11-28 16:11:47.777 WARN 9443 --- [main] ationConfigEmbeddedWebApplicationContext:在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'authenticationController的bean时出错':通过字段'passwordEncoder'表示不满意的依赖;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder'的限定bean可用:预计至少有1个bean有资格作为autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} 2016-11-28 16:11:47.781 INFO 9443 --- [main] o.apache.catalina.core.StandardService:停止服务Tomcat 2016-11-28 16:11:47.804 WARN 9443 --- [main] osboot.SpringApplication:错误处理失败(在类路径资源中定义名为'delegatingApplicationListener'的bean时出错[org / springframework / security / config / annotation /web/configuration/WebSecurityConfiguration.class]:bean的初始化失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration'的bean时出错:bean的初始化失败;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry'的bean可用) 2016-11-28 16:11:47.943 ERROR 9443 --- [主要] o.s.b.d.LoggingFailureAnalysisReporter:
申请失败
说明
no.sykling.rest.AuthenticationController中的字段passwordEncoder需要一个无法找到的'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder'类型的bean。
动作:
考虑在您的配置中定义类型为'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder'的bean。
以下是我的spring安全配置的样子:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private Logger logger = LoggerFactory.getLogger(SecurityConfiguration.class);
@Autowired private Environment environment;
@Autowired private UserService userService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder managerBuilder) throws Exception {
managerBuilder.userDetailsService(userService);
managerBuilder.authenticationProvider(authenticationProvider());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userService);
provider.setPasswordEncoder(passwordEncoder());
return provider;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/css/**", "/js/**", "/partials/**", "index.html");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/v1/authenticate/login", "/v1/authenticate/logout", "/").permitAll()
.anyRequest().hasRole("USER")
.and()
.logout()
.permitAll()
.logoutSuccessUrl("/")
.and()
.addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
.csrf().disable();
}
private AuthenticationSuccessHandler loginSuccessHandler() {
return ((request, response, authentication) -> response.sendRedirect("/dashboard"));
}
private AuthenticationFailureHandler loginFailureHandler() {
return ((request, response, exception) -> response.sendRedirect("/"));
}
private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName("X-XSRF-TOKEN");
return repository;
}
}
BCryptPasswordEncoder bean是明确定义的,当我在调试器中运行应用程序时,它似乎正确自动装配。但是一旦我们的linux机器上构建了应用程序,它就无法启动。
提前致谢
答案 0 :(得分:0)
以防有人遇到类似情况......
Bean定义是 passwordEncoder ,但我尝试将其连接为编码器。一旦我指定了全名,就像passwordEncoder一样按预期工作。
不确定为什么自动装配适用于我的IDE。