首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring-security的Spring Java配置

spring-security的Spring Java配置
EN

Stack Overflow用户
提问于 2013-02-16 22:42:43
回答 4查看 19.8K关注 0票数 4

出于某些原因(我真的记不清为什么:)我决定只使用Java来配置Spring应用程序。另外,我会尽量避免使用web.xml

我从下面的两个java配置文件开始。ApplicationBootstrap.java

代码语言:javascript
复制
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

代码语言:javascript
复制
@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:

代码语言:javascript
复制
   <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中,我添加了:

代码语言:javascript
复制
 @ImportResource( { "classpath:/spring-security.xml" } )

并创建了一个名为spring-security.xml的新xml文件

代码语言:javascript
复制
<security:http auto-config='true' create-session="never" realm="Restricted Service" use-expressions="true">
    <security:intercept-url pattern="/rest/**" access="permitAll()" />               
</security:http>

根据文档,这是最小配置。

尝试运行它会出现以下错误(我不明白为什么)

代码语言:javascript
复制
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

EN

回答 4

Stack Overflow用户

发布于 2014-01-14 22:14:42

坚持使用Java配置怎么样?

代码语言:javascript
复制
@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

顺便说一句。如果没有上述步骤,也可以手动进行注册:

代码语言:javascript
复制
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了。:)

票数 11
EN

Stack Overflow用户

发布于 2013-02-16 23:02:56

安全过滤器的名称应该是springSecurityFilterChain,这是Spring-security名称空间分配给Spring bean的名称。

代码语言:javascript
复制
<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应用程序通常加载了两个应用程序上下文,使用ContextLoaderListener指定的应用程序上下文(称为Root Web Application Context),以及使用DispatcherServlet加载的第二个应用程序上下文(称为Servlet Web Application Context),后者实际上是根上下文的子级,并且在根上下文中定义了对其可见的bean。根上下文包含与核心应用程序相关的bean(服务、存储库、entityManagers、安全性等),而servlet应用程序上下文仅包含Spring MVC所需的内容(控制器、视图解析器等)。现在,在您的示例中,您已经将ApplicationConfig指定为根上下文和servlet上下文的一部分,这不是必需的。相反,您可以仅将ApplicationConfig用作根上下文,并为web应用程序上下文使用不同的特定于MVC的上下文。

票数 6
EN

Stack Overflow用户

发布于 2013-04-24 09:38:06

如果您使用的是Spring,那么您可以尝试向web.xml添加上下文配置部分;这将把所有这些都绑定在一起。为了简单地让它在没有web配置的情况下工作,尝试如下所示:

代码语言:javascript
复制
servletContext.addFilter(
            "springSecurityFilterChain",
            new DelegatingFilterProxy("springSecurityFilterChain"))
        .addMappingForUrlPatterns(null, false, "/*");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14911485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档