Spring Boot:禁用安全自动配置

Posted

技术标签:

【中文标题】Spring Boot:禁用安全自动配置【英文标题】:Spring boot: disable security auto configuration 【发布时间】:2016-02-12 20:18:45 【问题描述】:

我有多部分网络项目。 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 不会为您自动配置它。如果需要不同的配置,需要自己配置。 我想完全禁用默认用户/密码的创建 【参考方案1】:

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

案例 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)

【讨论】:

【参考方案2】:

如果您查看spring boot's 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 个)

【讨论】:

以上是关于Spring Boot:禁用安全自动配置的主要内容,如果未能解决你的问题,请参考以下文章