2013-02-16 156 views
4

出于某种原因(我真的不能rembember为什么:))我决定只使用Java来配置Spring应用程序。此外,我会尽量避免web.xml弹簧安全的春天Java配置

我开始与follinging两个java配置文件。 ApplicationBootstrap.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 { 


} 

此担任espected。没有我的问题开始。我的想法是使用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" }) 

并创建一个新的XML文件命名为弹簧的security.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

回答

0

DeleagatingFilterProxy DOC

代理为标准的Servlet 2.3过滤器,委托给一个实现Filter接口Spring管理的bean。

默认情况下,此实现将所有调用委托给与过滤器具有相同名称的bean。 (你的情况是filterChainProxy)。所以,你需要定义一个名为filterChainProxy豆和这个bean必须实现Filter接口

但是Spring Security提供了一个名为springSecurityFilterChain实施Filter和适合春季的安全豆。因此,将您的过滤器名称更改为springSecurityFilterChain

link可以帮助你

+0

我改个名字,,但错误仍然存​​在:: - 严重:异常开始过滤springSecurityFilterChain org.springframework.beans.factory.NoSuchBeanDefinitionException:无豆命名为“springSecurityFilterChain”的定义 \t在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:549) \t在org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1096) – 2013-02-16 15:26:00

+0

@ lg.lindstrom刚添加类似问题的链接 – ben75 2013-02-16 15:49:15

6

您的过滤器的安全应该是springSecurityFilterChain的名称,它是分配由Spring安全命名空间中的Spring bean的名字。

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

而且你已经使用ApplicationConfig两个根上下文(通过的ContextLoaderListener加载),也是Web应用程序上下文(通过DispatcherServlet的加载),这将是更好地保持不同的上下文两者并加载Spring Security的一个只有通过根上下文

更新 澄清你的问题的意见 - Spring MVC的应用程序通常装载了2种应用环境中,你已经使用指定的一个ContextLoaderListener(称之为Root Web Application Context),而第二个加载usin g DispatcherServlet(称之为Servlet Web Application Context),它实际上是根上下文的子节点,并且在根环境中定义了可见的bean。根上下文包含与核心应用程序相关的bean(服务,存储库,实体管理器,安全等),而servlet应用程序上下文仅包含Spring MVC(控制器,视图解析器等)所需的东西。现在在你的情况下,你已经指定ApplicationConfig作为根上下文以及servlet上下文的一部分,这是不需要的。您可以改为使用ApplicationConfig作为根上下文,并为Web应用程序上下文使用不同的MVC特定上下文。

+0

我不太了解您的第二条评论? – 2013-02-16 15:28:22

+1

现在添加了一个更新,详细说明了我在第二条评论 – 2013-02-16 17:28:23

+0

@ lg-lindstrom中的含义。这种方式不需要包含过滤器配置。您可以在ApplicationBootstrap中配置过滤器。请参阅温室示例https://github.com/SpringSource/greenhouse/blob/213d2c742d472e602defcde801dd118e098d73c6/src/main/java/com/springsource/greenhouse/config/GreenhouseWebAppInitializer.java。您已经为调度程序servlet具有单独的上下文,但不需要在那里使用ApplicationConfig。 – Ritesh 2013-02-16 17:28:46

1

如果您使用的是Spring 3.1+,那么您可以尝试将上下文配置文件添加到web.xml中;这将把它们结合在一起。为了简单地在没有web配置的情况下工作,请尝试如下所示:

servletContext.addFilter(
      "springSecurityFilterChain", 
      new DelegatingFilterProxy("springSecurityFilterChain")) 
     .addMappingForUrlPatterns(null, false, "/*"); 
11

如何坚持使用Java配置?

@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    // ... 
} 

public class WebSecurityInitializer 
     extends AbstractSecurityWebApplicationInitializer { 
} 

便捷地,WebSecurityInitializer会为您注册Spring Security Servlet过滤器!下面是用大量细节的一个很好的解释:

http://docs.spring.io/spring-security/site/docs/3.2.x/guides/helloworld.html

顺便说一句...如果没有上述情况,但也可以手动做了登记:

public class DispatcherServletInitializer 
     extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    public void onStartup(ServletContext servletContext) 
      throws ServletException { 
     servletContext 
      .addFilter("securityFilter", 
         new DelegatingFilterProxy("springSecurityFilterChain")) 
      .addMappingForUrlPatterns(null, false, "/*"); 

     super.onStartup(servletContext); 
    } 

    // Various other required methods... 

} 

这种方式,你可以约,就是忘记恼人的web.xml。 :)

+0

我试过你的建议。我也不喜欢web.xml。但我仍然得到'NoSuchbeanException。 “你可以请看看。 http://stackoverflow.com/questions/32575242/nosuchbeandefinitionexception-during-spring-container-start-up – 2015-09-15 05:57:00