Spring Boot:禁用安全自动配置

6
我有一个多部分的网络项目。其中 Web Admin 部分包含:
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.springframework.boot:spring-boot-starter-actuator")

主项目构建文件包含:
compile group: 'org.springframework', name: "spring-webmvc", version: springVersion
compile(group: 'org.springframework.security', name: "spring-security-web", version: springSecurityVersion) { exclude(module: 'spring-jdbc') }

Spring Boot 应用程序文件:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class WebAdminApplication {

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

但是,当我向我的管理员部分发送http请求时,我的AuthenticationProvider中会得到用户和密码:
auth.getPrincipal() -> user
auth.getCredentials() -> caeebd3a-307b-4edf-8f2f-833fad9ebc00

我该如何关闭自动安全功能?

你想要禁用什么?如果你没有将spring-security作为依赖项,spring-boot就不会自动配置它。如果你想要不同的配置,你需要自己进行配置。 - Gergely Bacso
我想完全禁用默认用户/密码的创建。 - Lugaru
2个回答

7

我也曾面临同样的问题,所以我添加了以下代码。

  • 情况1:如果您尚未激活“ACTUATOR”: @SpringBootApplication(exclude = { SecurityAutoConfiguration.class })

  • 情况2:如果您已经激活了“ACTUATOR”: @SpringBootApplication(exclude = { org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration.class})


4

如果你查看spring boot的spring.factories(写作时的版本为1.3.5),你会发现安全性有4个自动配置类:

org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration,\

您可能还想禁用SecurityFilterAutoConfiguration(或所有4个)


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