gpt4 book ai didi

java - spring mvc 未定义名为 'springSecurityFilterChain' 的 bean

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:10 28 4
gpt4 key购买 nike

我正在使用 spring 4.2.1.RELEASE 和 jdk1.8.0_65。我无法将我的 spring mvc 项目部署到 tomcat8。(“服务器”是带有 Apache Tomcat/8.0.14 (Debian) 的树莓派 3)

在我的带有 Windows 和 Spring Boot 的开发机器上,一切正常。我可以从命令行启动我的 warfile。我不需要任何用户登录。每个人都应该无需任何身份验证即可访问所有页面。我进行了很多搜索,但无法让该项目运行。

部署到 tomcat8 后,出现以下异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined

我的 WebSecurityConfigureAdapter:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity

public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

protected static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(new Log4j2DynamicBinder().getCurrentClass());

static {
log.debug("WebSecurityConfiguration initialized.");
}

@Override
protected void configure(HttpSecurity security) throws Exception {
security.csrf().disable()
.authorizeRequests()
.antMatchers("/*").permitAll();
}

@Override
public void configure(WebSecurity security) throws Exception {
security.ignoring()
.antMatchers(WebMVCConfig.RESOURCE_HANDLERS.keySet().toArray(new String[0]));
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("timmy").password("timmy").authorities("USER");
}

@Bean
public FilterChainProxy springSecurityFilterChain() throws Exception {
log.debug("springSecurityFilterChain called!");
return new FilterChainProxy(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**")));
}
}

当我在 ubuntu 机器上从命令行启动项目时,一切正常。我确信此类已初始化:

2016-10-25 14:46:13.001 DEBUG 53128 --- [main] (  c.o.s.s.WebSecurityConfiguration:35 ) : WebSecurityConfiguration initialized.
....
2016-10-25 14:46:15.008 DEBUG --- [ost-startStop-1] ( c.o.s.s.WebSecurityConfiguration:65 ) : springSecurityFilterChain called!
2016-10-25 14:46:15.015 INFO 53128 --- [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/**'], []
2016-10-25 14:46:15.194 INFO 53128 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'metricFilter' to: [/*]
2016-10-25 14:46:15.194 INFO 53128 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-10-25 14:46:15.196 INFO 53128 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2016-10-25 14:46:15.197 INFO 53128 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2016-10-25 14:46:15.201 INFO 53128 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'applicationContextIdFilter' to: [/*]

我将不胜感激任何帮助:)谢谢!

@编辑:堆栈跟踪:

   org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1174)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:283)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:201)
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1051)
org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:255)

@Edit2:来自日志文件的堆栈跟踪:

    25-Oct-2016 13:14:16.380 SEVERE [http-nio-8080-exec-64] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [SpringMVCDispatcher] in context with path [/server-1.0] threw exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1174)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:283)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:201)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1051)
at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:255)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

@Edit3:将 Bean 移至新类并添加 @Configuration 注释:生产系统上出现同样的错误...当我从命令行启动应用程序时,我收到替换消息..

@Configuration
public class DedicatedConfigs {
@Bean
public FilterChainProxy springSecurityFilterChain() throws Exception {
return new FilterChainProxy(new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**")));
}
}

最佳答案

错误一:我的 BootApplication 类没有扩展 SpringBootServerletInitializer

错误 2:我的 WebApplicationInitializer 实现没有被调用。因此,将 protected SpringApplicationBuilder 配置(SpringApplicationBuilder builder)添加到我的引导类中...

在我的 SpringMVVInitializer 类中,我有注释 @Configuration @ComponentScan

我的启动应用程序类:

@SpringBootApplication
@EnableScheduling
public class BootApplication extends SpringBootServletInitializer {

public static final String processID;

static {
processID = ManagementFactory.getRuntimeMXBean().getName().replaceAll("@.*", "");
ThreadContext.put("pid", processID);
}

private static ApplicationContext springBootContext = null;

public static void main(String[] args) {
springBootContext = SpringApplication.run(new Class[]{BootApplication.class, SpringMVCInitializer.class}, args);
}

public static synchronized void shutdownSpringBoot() {
if (springBootContext != null) {
SpringApplication.exit(springBootContext, () -> 1);
springBootContext = null;
}
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(new Class[]{BootApplication.class, SpringMVCInitializer.class});
}

}

另外我在这里找到了这个视频:https://www.youtube.com/watch?v=kDZ5KkVQDxE我已将教程的 Spring Boot 代码与我的进行了比较。

谢谢你帮我解决问题

关于java - spring mvc 未定义名为 'springSecurityFilterChain' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40240726/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com