Spring Boot 2.0禁用默认安全性

60
我希望使用Spring Security来进行JWT身份验证。但是它自带了默认的身份验证方式。我尝试禁用它,但是旧的做法 - 通过application.properties禁用 - 在2.0中已被废弃。
这是我尝试过的方法:
@Configuration
public class StackWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().disable();
        // http.authorizeRequests().anyRequest().permitAll(); // Also doesn't work.
    }
}

我该如何简单地禁用基本安全性?

更新
可能需要知道的是,我不使用web mvc而是web flux。

截图:
Basic login form


你是否尝试按照这里所示的方式排除该包? - Novy
添加 @EnableWebFlux 会有效地禁用所有 WebFlux 自动配置。这是您打算做的吗? - Brian Clozel
@BrianClozel 不好意思,我本来以为 @EnableWebFlux 会启用 WebFlux...但是我发现即使将其删除,Web服务器仍然会启动,但基本安全性仍然被启用。 - Jan Wytze
这个回答解决了你的问题吗?Spring Boot安全性:禁用安全性 - Dupinder Singh
请注意 - Spring Security 适用于 Servlet 应用程序,建议在任何自定义配置中添加 @EnableWebSecurity。 - Irshad
显示剩余3条评论
17个回答

70
根据Spring 2.0的最新更新,如果classpath中有Spring Security,则Spring Boot将添加@EnableWebSecurity。因此,在应用程序属性中添加条目将无效(即不再可通过此方式进行自定义)。有关更多信息,请访问官方网站Spring Boot 2.0中的安全更改。虽然不确定您的要求,但我可以想到以下解决方法:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration  extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests().antMatchers("/").permitAll();
    }
}

希望这可以帮到你。


1
这个问题涉及在Spring WebFlux中配置安全性;而@EnableWebSecurity是针对Spring MVC的。 - Brian Clozel
1
@BrianClozel 当我回答这个问题时,我想那时还没有关于WebFlux的更新。 - Sen
BrianClozel,如何禁用@EnableWebSecurity。它似乎是默认存在的,并且在我定义反应式Spring安全性时阻止我的应用程序启动。 - DBS
2
在我看来,这并不比从类路径中删除Spring Security依赖项更好。如果“http.httpBasic().disable().formLogin().disable();”实际起作用会更好。文档说:“Spring Boot现在有一个单一的行为,只要您添加自己的WebSecurityConfigurerAdapter,它就会停止后退。”但这似乎是错误的陈述。我有一个WebSecurityConfigurerAdapter以及“http.httpBasic().disable().formLogin().disable();”,但我仍然得到那个愚蠢的Spring登录页面。这真是让人恼火。 - peekay
@Sen,是的,我明白这一点,我有自己的身份验证提供程序来处理身份验证,因此不需要登录表单或基本身份验证。因此,通过添加禁用调用,它应该停止阻止我并实际上退出,而不是继续弹出并防止我的身份验证提供程序完成工作。 - peekay
显示剩余3条评论

47

从Spring Boot 2.1开始,如果您包含spring-boot-actuator,则仅排除SecurityAutoconfiguration不再足够,您还需要排除ManagementWebSecurityAutoConfiguration,方法如下:

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

谢谢!这个回答中的建议很有效,当然也要根据 Spring Boot 的版本(在我这里是 2.1.5)来看。 - octy
2
只要我使用@EnableWebSecurity注释,这个答案对我不起作用(Spring Boot 2.1.5,spring-security-web 4.2.3.RELEASE)。你还需要做其他什么吗? - Brandon
2
如果您想排除AutoConfiguration,我相信您将无法使用@EnableWebSecurity,并且您需要像使用纯Spring /没有Spring Boot一样自己配置所需的Spring安全bean。 - Integrating Stuff
最简单的方法。谢谢。 - Frankie Drake

25

根据参考文档,使用WebFlux允许所有请求的安全配置应如下所示:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;

@Configuration
public class SecurityConfig {

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http.authorizeExchange().anyExchange().permitAll();
        return http.build();
    }
}

谢谢!在扩展了WebSecurityConfigurerAdapter的配置中,似乎没有任何作用。但是当移除@EnableWebSecurity并使用这个配置时,它最终起作用了。 - Jan Wytze
1
@EnableWebSecurity 仅适用于 Spring MVC。如果您的类路径上有 Spring Security,则会自动应用 @EnableWebFluxSecurity。请参阅 https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0.0-M4-Release-Notes#security-1 获取更多信息。 - Brian Clozel
我必须在SecurityConfig中显式提到@EnableWebFluxSecurity - Sumit Ramteke
8
现在它无法工作,因为它抱怨缺少 "ServerHttpSecurity" bean。 - user1428716
1
我已经尝试了使用Spring Boot 2.2.0,但是找不到'ServerHttpSecurity'。 - Christian Ibanibo
在我进行以下调整后,这对我起作用了...在返回之前,我必须调用http.csrf().disable(); - T.Thamilvaanan

19

这对我有用:

@Configuration
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().anyRequest().permitAll();
    }
}

我认为不需要禁用csrf,这可能会为某些攻击打开大门。https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF) - rvazquezglez
这个可以工作。不过我移除了 .csrf().disable()。禁用 csrf 不是一个好主意。 - zookastos

17

您可以将以下内容添加/修改到您的Application类中:

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

}

谢谢,这对我有用 :) 我只需要禁用登录以便测试 API 功能。 - luther wardle

7

添加一些新的回答,我假设所有人都在使用actuator,如果没有,我打赌一个类排除就足够了。我通过属性成功地禁用了它:

spring:
  autoconfigure:
    exclude: ${spring.autoconfigure.sac}, ${spring.autoconfigure.mwsas}
    sac: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
    mwsas: org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration

我通过属性引用了两个自动配置类,以保持长度不变(请注意,如果您像这样引用它,IntelliJ Ultimate会因为不知道这些占位值是什么以及它们是否真的合法而发出警告,所以如果这使您感到烦恼,请内联处理)。

然而,应用程序并不像https://www.baeldung.com/spring-boot-security-autoconfiguration所声称的那样在启动时失败,只需要禁用 SecurityAutoConfiguration 即可。

如果它确实起作用,您将不再看到自动生成的密码,这比接受的答案稍微少一点混乱,因为开发人员在阅读日志时不会因基本验证生成的密码而感到困惑,而安全性则允许所有人。

仅禁用主要自动配置类是不够的原因是因为有这个家伙:

@Configuration
class ManagementWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .requestMatchers(
                        EndpointRequest.to(HealthEndpoint.class, InfoEndpoint.class))
                .permitAll().anyRequest().authenticated().and().formLogin().and()
                .httpBasic();
    }

}

之前我们做了很多工作来分离执行器和安全配置,这让我们都感到困惑,现在更加直观易懂了,但还是存在这样的构件。如果我说错了,Spring 开发人员可以纠正我 :-)


1
哦,伙计,这是在无数不起作用的建议中唯一可行的答案。这个答案绝对应该在帖子的顶部。非常感谢! - Speise
对于WebFlux,请排除以下两个配置: org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfigurationorg.springframework.boot.actuate.autoconfigure.security.reactive.ReactiveManagementWebSecurityAutoConfiguration - Litash

3

如果我在我的application.yml文件中将spring.security.enabled属性设置为false来禁用Spring Security,那么我会利用@ConditionalOnProperty来加载以下的SecurityConfig.java类,并且它可以正常工作。

@ConditionalOnProperty(name = "spring.security.enabled", havingValue = "false")
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
            .authorizeRequests().antMatchers("/").permitAll();
    }
}

2

如果您在类路径中使用了执行器(actuator),则可以使用以下排除项来禁用Spring Boot响应式Web应用程序的默认安全性。

@SpringBootApplication(exclude = {ReactiveSecurityAutoConfiguration.class, ReactiveManagementWebSecurityAutoConfiguration.class })

如果您正在使用反应器,则可以通过以下方式禁用身份验证。 - Chris Hinshaw

2
如果您在WebFlux或Spring Cloud Gateway应用程序中遇到了困难,以下内容对我有效:
@EnableWebFluxSecurity
public class InsecurityConfiguration {
    // @formatter:off
    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
         http
              .authorizeExchange()
                   .anyExchange().permitAll();
         return http.build();
    }
}

当我尝试禁用Spring Cloud网关应用程序的所有安全性时,这个解决方案对我很有效。 - Saideep Ullal

1

Spring Boot 2.6的简单解决方案

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

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