应用Spring Security
前面介绍了在项目开发时为什么选择Spring Security,还介绍了它的原理。本节开始动手实践Spring Security的相关技术。
实战:Spring Security入门
现在开始搭建一个新项目,实践一个Spring Security的入门程序。
(1)新建一个spring-security-demo模块,添加项目依赖,在pom.xml中添加如下依赖:
<properties>
<java.version>11</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-thymeleaf</artifactId>
</dependency>
<!--thymeleaf对security5的支持依赖-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<!--<version>3.0.4.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
(2)在application.properties中添加Spring Security配置,配置当前登录的用户名和密码,配置内容如下:
#登录的用户名
spring.security.user.name=admin
#登录的密码
spring.security.user.password=123456
(3)在resources文件夹下创建页面add.html,表示添加页面,代码如下:
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org/">
<head>
title></title>
</head>
<body>
add 页面
</body>
</html>
(4)添加主页home.html,代码如下:
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org/">
<head>
<title>主页</title>
</head>
<body>
你已经登录成功!
<form th:action="https://blog.csdn.net/@%7B/logout%7D" action="https://blog.csdn.net/login" method="post">
<input type="submit" value="退出系统"/>
</form>
</body>
</html>
(5)添加login.html登录页,用于用户的登录,代码如下:
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org/">
<head>
<title>请登录</title>
</head>
<body>
<div>
<form th:action="https://blog.csdn.net/@%7B/login%7D" method="post" action="https://blog.csdn.net/login">
<p>
<span>请输入用户名:</span>
<input type="text" id="username" name="username">
</p>
<p>
<span>请输入密码:</span>
<input type="password" id="password"
name="password">
</p>
<input type="submit" value="登录"/>
</form>
</div>
</body>
</html>

1978

被折叠的 条评论
为什么被折叠?



