社区!
这是我的情况。我有 Spring 启动应用程序。它使用maven构建并打包在jar文件中。问题是当我在我的开发 PC 上构建它并运行时一切正常。如果我在我们的临时环境(Linux)上运行这个 jar 文件,它也运行良好。但是,如果我在临时环境中构建它(当然,这是由我们的 Jenkins 服务器完成的),它会在启动时失败,但有异常:
<小时/>2016-11-28 16:11:47.777 WARN 9443 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authenticationController': Unsatisfied dependency expressed through field 'passwordEncoder'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2016-11-28 16:11:47.781 INFO 9443 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat 2016-11-28 16:11:47.804 WARN 9443 --- [ main] o.s.boot.SpringApplication : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available) 2016-11-28 16:11:47.943 ERROR 9443 --- [ main] 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 机器上构建起来,它就无法启动。
提前致谢
最佳答案
以防万一有人经历过类似的情况......
Bean 定义是passwordEncoder,但是我尝试将其连接为编码器。一旦我指定了全名,例如passwordEncoder,它就按预期工作了。
但不确定为什么 Autowiring 对我的 IDE 有效。
关于java - 在不同环境下构建spring boot jar失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40847983/