我试图以java配置方式加载webapp。在RAFTWebAppInitializer中,我首先在本地框架中注册了配置类。但春天的安全过滤链正在抛出错误。
public class RAFTWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
public static final String SPRING_DISPATCHER_SERVLET_NAME = "raft";
public static final String URL_PATTERN = "/";
private static final String SPRING_FILTER_CHAIN_NAME = "springSecurityFilterChain";
private static final String UNABLE_TO_REGISTER_FILTER_CHAIN = "Unable to register filter chain";
private Logger logger = LogManager.getLogger(RAFTWebAppInitializer.class);
@Override
protected WebApplicationContext createRootApplicationContext() {
//initialize and register context
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
logger.info("Entering in root context to register chassis library");
/*Home grown framework */
ABCInitializer abcInitializer = new ABCInitializer();
abcInitializer .initialize(rootAppContext);
logger.info("Exiting after registering chassis library");
rootAppContext.register(getRootConfigClasses());
logger.info("Exiting after registering App config classes");
//registerSpringFilterChain(rootAppContext.getServletContext(), URL_PATTERN + "*");
return rootAppContext;
}
@Override
protected Class<?>[] getRootConfigClasses() {
logger.debug("Calling getRootConfigClasses...");
return new Class[]{AppConfig.class,WebAppSecurityConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
logger.debug("Calling getServletConfigClasses...");
return new Class[]{DispatcherConfiguration.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{URL_PATTERN};
}
@Override
protected String getServletName() {
return SPRING_DISPATCHER_SERVLET_NAME;
}
}
@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = { "com.capitalone.raft" })
@Order(2)
public class WebAppSecurityConfig extends WebSecurityConfigurerAdapter {
private Logger logger = LogManager.getLogger(this.getClass());
// Own implementation of different configs
}
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
@Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
logger.info("Before spring security");
// super.beforeSpringSecurityFilterChain(servletContext);
}
@Override
protected void afterSpringSecurityFilterChain(ServletContext servletContext) {
logger.info("After spring security");
// super.afterSpringSecurityFilterChain(servletContext);
}
private Logger logger = LogManager.getLogger(this.getClass());
private static final String FILTER_ENCODING_TYPE = "UTF-8";
/* @Override
protected void afterSpringSecurityFilterChain(ServletContext servletContext) {
logger.info("afterSpringSecurityFilterChain gets called to register other filter");
//super.afterSpringSecurityFilterChain(servletContext);
servletContext.addFilter("HttpMethodFilter", HiddenHttpMethodFilter.class);
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(FILTER_ENCODING_TYPE, true);
servletContext.addFilter("CharacterEncodingFilter", characterEncodingFilter);
logger.info("afterSpringSecurityFilterChain completed to register other filters");
}*/
}
除了这些之外,我们还有servlet DispatcherConfiguration.class的配置类。但我总是得到错误 2016-04-01 14:05:37,237 INFO [localhost-startStop-1] context.ContextLoader(ContextLoader.java:347) - 根WebApplicationContext:初始化在0毫秒内完成 2016-04-01 14:05:37,237 DEBUG [localhost-startStop-1] filter.GenericFilterBean(GenericFilterBean.java:177) - 初始化过滤器&#39; springSecurityFilterChain&#39; 2016年4月1日14:05:37.252严重[localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal一个或多个过滤器无法启动。完整的详细信息将在相应的容器日志文件中找到 2016-04-01 14:05:37,252 INFO [localhost-startStop-1] support.AbstractApplicationContext(AbstractApplicationContext.java:957) - 关闭Root WebApplicationContext:启动日期[Fri Apr 01 14:05:35 EDT 2016];上下文层次结构的根
在容器日志中, 2016年4月1日12:06:17.359 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例的异常 org.springframework.beans.factory.BeanCreationException:创建名称为&#39; chassisConfig&#39;的bean时出错:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.capitalone.raft.refapps.api.mvc.config.ChassisConfig $$ EnhancerBySpringCGLIB $$ 7e8af6fb]:找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException:com.capitalone.raft.refapps.api.mvc.config.ChassisConfig $$ EnhancerBySpringCGLIB $$ 7e8af6fb。() 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
无法理解,因为当我调试服务器初始化时,实际上会调用这两个方法:
@覆盖 public final void onStartup(ServletContext servletContext) 抛出ServletException { if(enableHttpSessionEventPublisher()){ servletContext.addListener(HttpSessionEventPublisher.class); } insertSpringSecurityFilterChain(参数servletContext); afterSpringSecurityFilterChain(参数servletContext); } 然后
private void insertSpringSecurityFilterChain(ServletContext servletContext){ String filterName =&#34; springSecurityFilterChain&#34 ;; DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy(filterName); String contextAttribute = getWebApplicationContextAttribute(); if(contextAttribute!= null){ springSecurityFilterChain.setContextAttribute(contextAttribute); } registerFilter(servletContext,true,filterName,springSecurityFilterChain); }
答案 0 :(得分:0)
因为我已经使用我们自己开发的框架引导了上下文类,所以我不得不手动刷新上下文。刷新后,所有问题都得到了解决。但有一件事需要记住,使用组件扫描并尝试在根级别设置它。但是,我们可能需要了解后果。有时在root包上进行扫描时,它可能会尝试注册一些可能导致冲突的附加(或与给定上下文无关)的类。 更新的代码:
public class RAFTWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
public static final String SPRING_DISPATCHER_SERVLET_NAME = "raft";
public static final String URL_PATTERN = "/";
private static final String SPRING_FILTER_CHAIN_NAME = "springSecurityFilterChain";
private static final String UNABLE_TO_REGISTER_FILTER_CHAIN = "Unable to register filter chain";
private Logger logger = LogManager.getLogger(RAFTWebAppInitializer.class);
@Override
protected WebApplicationContext createRootApplicationContext() {
//initialize and register context
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
logger.info("Entering in root context to register abc library");
/*Home grown framework */
ABCInitializer abcInitializer = new ABCInitializer();
abcInitializer .initialize(rootAppContext);
logger.info("Exiting after registering chassis library");
rootAppContext.register(getRootConfigClasses());
logger.info("Exiting after registering App config classes");
rootAppContext.refresh();
return rootAppContext;
}