Spring Boot中的Spring Security

5

我希望在Spring Boot应用程序中实现Spring Security。之前,我已经使用JavaConfig在我的Spring 4.0应用程序中完成了这项工作。然而,我发现示例的风格存在一些差异。

在我的情况下,用户是预认证的,并且我们有自己的授权机制,其中包含业务活动。

在我的旧应用程序中,我使用以下配置:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

这个过滤器实际上会在我的应用程序中启用Spring Security,进而将用户令牌传递给更进一步的步骤。

在Spring Boot参考文档中,我没有找到任何关于springSecurityFilterChain/DelegatingFilterProxy的痕迹,所以我不确定如何开始开发我的模块。

所以问题是,我需要手动配置它吗?还是已经在自动配置中处理了?

其次,我需要对Spring Security进行许多自定义设置,因此我不需要Spring Boot Security提供的基本身份验证功能。在这种情况下,只需创建具有@EnableWebSecurity注释的自己的bean即可关闭功能?

其他信息

4.2.使用安全命名空间配置入门 在本节中,我们将介绍如何构建一个命名空间配置,以使用框架的一些主要功能。假设您最初想尽快启动并向现有Web应用程序添加身份验证支持和访问控制,并进行一些测试登录。然后,我们将介绍如何更改为对数据库或其他安全存储库进行身份验证。在后面的章节中,我们将介绍更高级的命名空间配置选项。

4.2.1. web.xml配置 您需要做的第一件事是在web.xml文件中添加以下过滤器声明:

<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>

这提供了一个钩子进入Spring Security web基础设施。DelegatingFilterProxy是Spring Framework的一个类,它委托给在应用程序上下文中定义为Spring bean的过滤器实现。在这种情况下,bean的名称为"springSecurityFilterChain",它是由命名空间创建的内部基础设施bean,用于处理Web安全性。请注意,您不应该自己使用此bean名称。一旦将其添加到web.xml中,您就可以开始编辑应用程序上下文文件了。Web安全服务是使用元素配置的。


我只看了那份文档。所以在那个链接中,我没有找到任何关于springSecurityFilterChain/DelegatingFilterProxy的参考资料。 - user3534483
2
好的...你能告诉我在哪个类中注册了springSecurityFilterChain/DelegatingFilterProxy吗? - user3534483
检查自动配置类... https://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security - M. Deinum
好的,谢谢。我会在这些类中搜索过滤器配置。 - user3534483
4
看起来你在经历了一些“旧”的Spring后,希望尝试Spring Boot。这会导致你试图找出本来不存在的问题 :) Spring Boot已经为你完成了很多工作。实际上,在开始阶段你会感到不知所措,因为你会觉得某些配置需要进行,但其实已经完成了。有很多示例可以帮助你:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples - hi_my_name_is
显示剩余3条评论
1个回答

0

自从Spring Security 3.2(2013年12月)以来,不再需要自己配置springSecurityFilterChain@EnableWebSecurity注释将为您完成此操作,前提是您包括spring-security-config模块(不包括spring-security-web,但Spring Boot会自动包含它):

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>

WebSecurityConfiguration配置器在@EnableWebSecurity上激活并委托给WebSecurity构建器类来实际构建配置,最终使用bean名称"springSecurityFilterChain"实例化FilterChainProxy

只需按照文档激活和配置Web安全即可。例如:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().authorizeRequests()
            .anyRequest().hasAnyRole("USER", "ADMIN");
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user").password("{noop}user").roles("USER")
                .and()
                .withUser("admin").password("{noop}admin").roles("ADMIN");
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接