Spring Boot的Security安全控制——应用SpringSecurity!

应用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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值