Spring Boot 2.0.x如何禁用特定配置文件的安全性?

16
在Spring Boot 1.5.x中,我已经配置了安全性,并在某些配置文件(如本地)中添加了security.basic.enabled=false行以禁用该配置文件的所有安全性。现在我正在尝试迁移到新的Spring Boot 2,在那里该配置属性被移除了。在Spring Boot 2.0.x中,如何实现相同的行为(不使用此属性)?
我已经阅读了Spring-Boot-Security-2.0security-changes-in-spring-boot-2-0-m4,但没有关于这个属性的内容。

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

17

你需要添加自定义Spring Security配置,请参考Spring Boot参考指南

28.1 MVC安全性

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

例如:

@Configuration
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

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

若要仅针对某个配置文件使用,请在类中添加@Profile注解。如果您想通过属性启用它,请在类中添加ConditionalOnProperty注解。


2
请注意,如果您有多个不相互排斥的安全配置,则还应定义@Order。例如,如果您的主要安全配置使用默认配置文件(未指定@Profile),并且您将dev配置文件添加到另一个配置中,则在使用活动的dev配置文件运行时,Spring将不得不决定哪个配置优先。 - The Gilbert Arenas Dagger

11

这是我最终解决问题的方法。以下是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();
    }
}

由于Spring Boot 2中已删除security.basic.enabled(但仍将其保留为属性名),因此我最终使用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();
    }
}

1
我认为最完整的答案是... 对于安全性,ignored = "*.js"。如果您想要忽略某些静态资源,我们是否仍然使用web.ignore()?还是完全不同的方法... - Eric Huang
1
您可以在SecurityConfig类上添加@ConditionalOnProperty(prefix = "spring", name = "security.enabled"),以避免在configure()内部使用if-else。 - Muzammil

5

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

@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})

在主类中添加以下内容。

2
你可以通过排除 SecurityAutoConfiguration.classManagementWebSecurityAutoConfiguration.class 来禁用Spring安全自动配置,当使用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 {

1

Spring Boot 2.1.3

对于特定的配置文件"profile dev"

创建一个新的Spring配置类

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

}

这将禁用Spring Security。

0

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

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class} )
public class JwtWithSpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(JwtWithSpringBootApplication.class, args);
    }
}

1
你的回答并没有真正回答问题,因为你只是禁用了Spring Boot的默认配置,而没有禁用你自己的自定义配置。 - dur

0
在Spring Boot 2.1.4中,我必须这样做。
@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class})

这给我造成了以下运行时错误:org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration中inMemoryUserDetailsManager方法的参数0需要找到一个类型为'org.springframework.boot.autoconfigure.security.SecurityProperties'的bean,但是没有找到。 - Brandon
Brandon,听起来你需要启用安全性? - Kalpesh Soni

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