Spring Boot中的Spring安全性

use*_*483 5 spring spring-security spring-boot

我想在Spring Boot应用程序中实现Spring Security.我之前使用JavaConfig使用Spring 4.0应用程序完成了这项工作.但是,我发现给出的例子风格有些不同

就我而言,用户是预先认证的,我们有自己的授权机制,其中包含业务活动.

在我的prev应用程序中,我用来配置

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
Run Code Online (Sandbox Code Playgroud)

这个过滤器实际上会启用我的应用程序中的spring安全性,这会将用户令牌传递给更多步骤.

在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>
Run Code Online (Sandbox Code Playgroud)

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

rus*_*tyx 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>
Run Code Online (Sandbox Code Playgroud)

配置WebSecurityConfiguration器激活@EnableWebSecurity并委托给WebSecurity构建器类来实际构建配置,最后FilterChainProxy使用 bean name进行实例化"springSecurityFilterChain"

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

@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");
    }

}
Run Code Online (Sandbox Code Playgroud)