【问题标题】:Spring Boot 2.0.x disable security for certain profileSpring Boot 2.0.x 禁用某些配置文件的安全性
【发布时间】:2018-08-21 20:41:54
【问题描述】:

在 Spring Boot 1.5.x 中,我已经配置了安全性,并且在某些配置文件(例如本地)中,我已将 security.basic.enabled=false 行添加到 .properties 文件以禁用该配置文件的所有安全性。我正在尝试迁移到新的 Spring Boot 2,其中删除了该配置属性。如何在 Spring Boot 2.0.x 中实现相同的行为(不使用此属性)?

我已经阅读了Spring-Boot-Security-2.0security-changes-in-spring-boot-2-0-m4 并且没有关于此属性的任何内容。

【问题讨论】:

  • 此属性在此处被列为重大更改:github.com/spring-projects/spring-boot/wiki/…
  • @KeatsPeeks 是的,这是真的。这就是为什么我要问如何在 Spring Boot 2 中实现相同的行为(当然,没有这个属性)。
  • @dur 是的,这也是真的。这就是为什么我要问如何做到这一点。大多数关于禁用安全性的 SO 答案都使用该配置属性。所有其他人都使用 Profile 注释来禁用某些配置文件的配置 bean,但这不是我想要的行为,因为 Spring Security 默认需要登录。我想完全禁用那些特定配置文件的登录。

标签: java spring-boot spring-security


【解决方案1】:

您可以通过排除 SecurityAutoConfiguration.classManagementWebSecurityAutoConfiguration.class 来禁用 Spring Security 自动配置获得 spring-boot-actuator 依赖时排除

@SpringBootApplication(
exclude = { SecurityAutoConfiguration.class,  
            ManagementWebSecurityAutoConfiguration.class })
public class MySpringBootApplication {

然后您可以使用类似这样的配置文件或配置参数有条件地配置您的自定义 WebSecurityConfigurerAdapter

@Configuration
@ConditionalOnProperty(prefix = "security.custom", name = "enabled", havingValue="true")
@EnableWebSecurity
public class CustomWebSecurityConfiguration extends WebSecurityConfigurerAdapter {

【讨论】:

    【解决方案2】:

    Spring Boot 2.1.3

    对于某个配置文件“dev”

    创建一个新的 Spring 配置类

    @Configuration
    @EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})
    @Profile("dev")
    public class WebSecurityConfigDisable  {
    
    }
    

    这将禁用弹簧安全性。

    【讨论】:

      【解决方案3】:

      在 springboot 2.1.4 中我必须这样做

      @EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class})
      

      【讨论】:

      • 这对我造成了以下运行时错误: org.springframework.boot.autoconfigure.security.servlet.UserDetailsS​​erviceAutoConfiguration 中的方法 inMemoryUserDetailsManager 的参数 0 需要 'org.springframework.boot.autoconfigure 类型的 bean。无法找到 security.SecurityProperties'。
      • Brandon,听起来您需要启用安全性?
      【解决方案4】:

      在 Spring Boot 2 中还有另一个禁用安全性的选项

      @EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})
      

      在主类上添加这个

      【讨论】:

        【解决方案5】:

        其实这个问题的某些配置有几个答案;就我而言,我一直在使用 Spring Security 和 JWT 玩 Spring Boot 2。为了保护我的 REST 端点,我需要创建令牌等。在我编写代码并尝试测试我的端点以查看 JWT 是否正常工作后,我最终面临默认登录页面。我尝试更改上面提到的属性文件,最后我通过将 @SpringBootApplication(exclude = {SecurityAutoConfiguration.class} ) 注释添加到我的应用程序类来禁用它。

        @SpringBootApplication(exclude = {SecurityAutoConfiguration.class} )
        public class JwtWithSpringBootApplication {
        
            public static void main(String[] args) {
                SpringApplication.run(JwtWithSpringBootApplication.class, args);
            }
        }
        

        【讨论】:

        • 您的回答并不是真正的问题,因为您只是禁用了 Spring Boot 的默认配置,而不是您自己的自定义配置。
        【解决方案6】:

        您必须添加自定义 Spring Security 配置,请参阅Spring Boot Reference Guide

        28.1 MVC 安全性

        默认安全配置在SecurityAutoConfigurationUserDetailsServiceAutoConfiguration 中实现。 SecurityAutoConfiguration 导入 SpringBootWebSecurityConfiguration 用于 Web 安全,UserDetailsServiceAutoConfiguration 配置身份验证,这在非 Web 应用程序中也相关。要完全关闭默认的 Web 应用程序安全配置,您可以添加 WebSecurityConfigurerAdapter 类型的 bean(这样做不会禁用 UserDetailsService 配置或执行器的安全性)。

        例如:

        @Configuration
        public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
        
            @Override
            public void configure(WebSecurity web) throws Exception {
                web
                   .ignoring()
                       .antMatchers("/**");
            }
        }
        

        要将配置仅用于配置文件,请将@Profile 添加到类。如果你想通过属性启用它,请在类中添加ConditionalOnProperty

        【讨论】:

        • 请注意,如果您有多个不互斥的安全配置,您还应该定义@Order。例如,如果您的主要安全配置使用默认配置文件(没有指定@Profile)并且您将开发配置文件添加到另一个配置,那么当您使用开发配置文件运行时,Spring 将不得不决定哪个配置优先。
        【解决方案7】:

        这就是我最终解决问题的方法。这是我的安全配置在 Spring Boot 1.5.x 中的外观示例。属性 security.basic.enabled=false 禁用了安全性:

        @Configuration
        @EnableWebSecurity
        public class SecurityConfig extends WebSecurityConfigurerAdapter {
        
            @Override
            public void configure(WebSecurity web) throws Exception {
                web.ignoring().antMatchers("/upload/**");
            }
        
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().disable().authorizeRequests()
                        .anyRequest().authenticated()
                        .and().httpBasic();
            }
        }
        

        由于 security.basic.enabled 在 Spring Boot 2 中被删除(但仍保留为属性名称),我最终使用 security.enabled 作为自定义属性。这是我的配置在 Spring Boot 2 中的外观示例:

        @Configuration
        @EnableWebSecurity
        public class SecurityConfig extends WebSecurityConfigurerAdapter {
        
            @Value("${security.enabled:true}")
            private boolean securityEnabled;
        
            @Override
            public void configure(WebSecurity web) throws Exception {
                if (securityEnabled)
                    web.ignoring().antMatchers("/upload/**");
                else
                    web.ignoring().antMatchers("/**");
            }
        
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                if (securityEnabled)
                    http.csrf().disable().authorizeRequests()
                            .anyRequest().authenticated()
                            .and().httpBasic();
            }
        }
        

        【讨论】:

        • 我认为最完整的答案 imo.... 对于 security.ignored = "*.js" 。如果你想忽略一些静态资源,我们仍然使用 web.ignore() 吗?或者它完全不同......
        • 您可以在 SecurityConfig 类中添加 @ConditionalOnProperty(prefix = "spring", name = "security.enabled") 以避免在 configure() 中出现 if-else
        猜你喜欢
        • 2018-04-26
        • 2018-10-07
        • 2016-02-12
        • 2014-07-16
        • 2020-06-11
        • 2019-02-17
        • 1970-01-01
        相关资源
        最近更新 更多