SpringBoot整和SpringSecurity-Hello(一)

SpringBoot整和SpringSecurity-Hello(一)

1.创建一个Spring Boot项目

1.使用idea Spring Boot脚手架新建,配置好相关信息后,点击Next

bd6b927c0ec1f59355fc95d004536b02.png

2. 点击Create

9d76268c153ba2ad380211ac3d758ccb.png

3.等待下载完成后,Spring Boot项目就创建成功了

136ef0992fbf8d9d9417384814d35bdb.png

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.编写代码

  1. 在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>
    
  2. 新增controller.IndexController类

    @Controller
    public class IndexController {
        @GetMapping
        public String index() {
            return "index";
        }
    }
    
  3. 启动项目

    e2207ffc8026c0237c30284afaa7fcc1.png

    在没有进行任何配置时,启动时控制台会打印一串密码

  4. 浏览器访问localhost:8080

    cc3d7f70dbaa96df987f3e724e19479d.png

  5. 输入用户名:root 密码:控制台打印的字符串,可以看到浏览器能访问到index.html页面

    3eb5ca19b7a824498d87770720a5afaf.png

  6. 点击Log Out,返回到登录页面

    95fc1a9e2684ac2925604a9dd5e751e1.png

到这里我们就完成了SpringBoot和SpirngSecurity的代码整和。

希望大家多多点赞关注!

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值