1

我試圖在spring啓動配置類中創建一個bean。但是當我運行該應用程序時出現錯誤。無法在彈簧啓動時初始化bean

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) 
    at com.highpeak.gbi.webservices.Application.main(Application.java:22) 
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat 
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:123) 
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:84) 
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:554) 
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:179) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) 
    ... 8 common frames omitted 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jwtAuthenticationFilter' defined in class path resource [com/highpeak/gbi/webservices/security/WebSecurity.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: authenticationManager must be specified 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:234) 
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:182) 
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:177) 
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAdaptableBeans(ServletContextInitializerBeans.java:159) 
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:80) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getServletContextInitializerBeans(EmbeddedWebApplicationContext.java:241) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:228) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:89) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:213) 
    at org.springframework.boot.context.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:55) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 
    at java.lang.Thread.run(Thread.java:748) 
Caused by: java.lang.IllegalArgumentException: authenticationManager must be specified 
    at org.springframework.util.Assert.notNull(Assert.java:134) 
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.afterPropertiesSet(AbstractAuthenticationProcessingFilter.java:164) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) 
    ... 24 common frames omitted 

Process finished with exit code 1 

我的配置類是

@Configuration 
@EnableWebSecurity 
public class WebSecurity extends WebSecurityConfigurerAdapter { 

@Autowired 
private UserDetailsService userDetailsService; 

@Autowired 
private BCryptPasswordEncoder bCryptPasswordEncoder; 

public WebSecurity(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) 
{ 
    this.userDetailsService = userDetailsService; 
    this.bCryptPasswordEncoder = bCryptPasswordEncoder; 
} 

@Bean 
public JWTAuthenticationFilter jwtAuthenticationFilter() throws Exception 
{ 
    return new JWTAuthenticationFilter(authenticationManager()); 
} 

@Bean 
public JWTAuthorizationFilter jwtAuthorizationFilter() throws Exception 
{ 
    return new JWTAuthorizationFilter(authenticationManager()); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception 
{ 
    http.cors().and().csrf().disable().authorizeRequests().antMatchers(HttpMethod.POST, "/rest/auth/**").permitAll() 
      .antMatchers("/static/*").permitAll().antMatchers("/").permitAll() 
      /* .anyRequest().authenticated() */.and().logout() 
      .logoutRequestMatcher(new AntPathRequestMatcher("/logout")).clearAuthentication(true) 
      .logoutSuccessUrl("/").and().addFilter(jwtAuthenticationFilter()) 
      .addFilter(jwtAuthorizationFilter()); 
} 

@Override 
public void configure(AuthenticationManagerBuilder auth) throws Exception 
{ 
    auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder); 
} 

@Override 
public void configure(org.springframework.security.config.annotation.web.builders.WebSecurity web) 
     throws Exception 
{ 

    web.ignoring().antMatchers("/static/**"); 
} 

@Bean 
CorsConfigurationSource corsConfigurationSource() 
{ 
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 
    source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues()); 
    return source; 
} 
} 

兩個@Bean類我試圖在@Override protected void configure(HttpSecurity http) throws Exception方法使用。但拋出上述錯誤。如何解決這個問題?

+0

你看看[這個答案](https://stackoverflow.com/questions/27690160/spring-boot-with-spring-security-error -creating-bean-with-name-securityfilter)? – user10089632

+0

'@ Bean'類正在使用參數化構造函數創建一個新實例。該構造函數的'argument'值來自父類'WebSecurityConfigurerAdapter'方法'authenticationManager()'。錯誤是告訴'應該指定authenticationManager()'。這是什麼意思? –

回答

0

我建議你添加像這樣
@Override @Autowired public void setAuthenticationManager(AuthenticationManager authenticationManager) { super.setAuthenticationManager(authenticationManager); }

+0

在哪裏添加它? –

+0

@SandeshaJ,CustomUsernamePasswordAuthenticationFilter類 – user10089632

相關問題