SpringBoot整和SpringSecurity-Hello(一)
1.创建一个Spring Boot项目
1.使用idea Spring Boot脚手架新建,配置好相关信息后,点击Next
2. 点击Create
3.等待下载完成后,Spring Boot项目就创建成功了
4.修改SpringBoot的版本为2.7.17,刷新maven
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.pom文件导入SpringSecurity相关依赖
<!--security依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--spring-boot Web支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.编写代码
-
在resource目录下新增 templates/index.html
<!DOCTYPE html> <html lang="en" xmlns:th="https://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Hello Security!</title> </head> <body> <h1>Hello Security</h1> <a th:href="@{/logout}">Log Out</a> </body> </html>
-
新增controller.IndexController类
@Controller public class IndexController { @GetMapping public String index() { return "index"; } }
-
启动项目
在没有进行任何配置时,启动时控制台会打印一串密码
-
浏览器访问localhost:8080
-
输入用户名:root 密码:控制台打印的字符串,可以看到浏览器能访问到index.html页面
-
点击Log Out,返回到登录页面
到这里我们就完成了SpringBoot和SpirngSecurity的代码整和。
希望大家多多点赞关注!