「这是我参与11月更文挑战的第4天,活动详情查看:2021最后一次更文挑战」。
前言
随着
SpringBoot
的火热,同家族的SpringSecurity
也逐渐走进大家视野,在以前,和Apache
的Shiro
相比,SpringSecurity
复杂的配置直接劝退了,刚入行的我这个菜鸟,现在依托与SpringBoot
简化了大量配置,整合起来也变得尤为简单。
快速搭建SpringSecurity项目
- 创建
spring boot
工程 - 添加起始依赖
web
、Security
依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yang</groupId>
<artifactId>spring-security</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-security</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
复制代码
启动类SpringSecurityApplication
添加api
@RestController
@SpringBootApplication
public class SpringSecurityApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSecurityApplication.class, args);
}
@GetMapping("/hi")
public String hi (){
return "hi,spring-security";
}
}
复制代码
启动项目
- 访问
http://localhost:8080/hi
发现路径自动跳转到
http://localhost:8080/login
提示要求登录
当引入Spring Security
后,没有添加任何的配置或拦截编码,但是Spring Security
有一个默认的运行状态,
要求经过HTTP基本认证后才能访问URL资源
默认用户名: user
默认动态密码: 查看控制台打印
Using generated security password: 5f226ca2-5bc6-4e45-9f67-94760c5353bd
复制代码
- 输入用户名和密码,点击
Sign in
页面跳转
http://localhost:8080/hi
,页面输出 hi,spring-security
自定义用户名密码
我们也可以自定义登录用户名和密码
-
打开配置文件
application.yml
-
添加配置
spring:
security:
user:
name: caoshenyang
password: 123456
复制代码
- 重新启动项目
发现控制台不再打印密码
-
访问接口
http://localhost:8080/hi
-
输入自定义的用户名密码
登录成功
通常情况下一般不会选择这种HTTP基本认证的方式,因为安全性差、无法携带cookie,灵活性不足。基本采用表单认证,自己实现验证逻辑,提高安全性
近期评论