原文地址:https://www.baeldung.com/spring-boot-security-autoconfiguration
参考文章:https://www.cnblogs.com/xjknight/p/10925123.html
在本文中,我们将了解Spring Boot对spring Security的支持。
简而言之,我们将专注于默认Security配置以及如何在需要时禁用或自定义它。
为了增加Spring Boot应用程序的安全性,我们需要添加安全启动器依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
这将包括SecurityAutoConfiguration类 - 包含初始/默认安全配置。
注意我们在这里没有指定版本,假设项目已经使用Boot作为父项。
简而言之,默认情况下,为应用程序启用身份验证。此外,内容协商用于确定是否应使用basic或formLogin。
有一些预定义的属性,例如:
spring.security.user.name
spring.security.user.password
如果我们不使用预定义属性spring.security.user.password配置密码并启动应用程序,我们会注意到随机生成默认密码并在控制台日志中打印:
Using default security password: c8be15de-4488-4490-9dc6-fab3f91435c6
要放弃安全性自动配置并添加我们自己的配置,我们需要排除SecurityAutoConfiguration类。
这可以通过简单的排除来完成: