出于某种原因(我真的不记得为什么 :))我决定只使用 Java 来配置 Spring 应用程序。另外我会尽量避免使用 web.xml
我从以下两个 java 配置文件开始。应用程序引导程序.java
public class ApplicationBootstrap implements WebApplicationInitializer {
//public class Initializer
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(ApplicationConfig.class);
rootContext.refresh();
// Manage the lifecycle of the root appcontext
servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.setInitParameter("defaultHtmlEscape", "true");
servletContext.setInitParameter("spring.profiles.active", "Production");
// now the config for the Dispatcher servlet
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
mvcContext.register(ApplicationConfig.class);
mvcContext.getEnvironment().setActiveProfiles("Production");
mvcContext.getEnvironment().setDefaultProfiles("Production");
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/api/*");
}
和 ApplicationConfig.java
@Configuration()
@Profile({"Production", "ControllerUnitTest"})
@EnableWebMvc
@ComponentScan( basePackages = "com.consius.activework.server" )
@EnableAspectJAutoProxy
public class ApplicationConfig extends WebMvcConfigurerAdapter {
}
这按预期工作。没有我的问题开始了。我的想法是使用 spring-security,我寻找一种使用 Java 配置 spring-security 的方法。一段时间后我放弃了,我发现无法使用 Java 配置 spring-security。我决定回到 XML 进行安全配置。
我创建了一个包含以下内容的 web.xml:
<filter>
<filter-name>filterChainProxy</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>filterChainProxy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我在 ApplicationConfig.java 中添加了:
@ImportResource( { "classpath:/spring-security.xml" } )
并创建了一个名为 spring-security.xml 的新 xml 文件
<security:http auto-config='true' create-session="never" realm="Restricted Service" use-expressions="true">
<security:intercept-url pattern="/rest/**" access="permitAll()" />
</security:http>
根据文档,这是最低配置。
尝试运行它会出现以下错误(我不明白为什么)
SEVERE: Exception starting filter filterChainProxy
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'filterChainProxy' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:549)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1096)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:236)
at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:194)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
谁能帮我?我想我做了一些明显的错误,但我看不到它。
//lg