SpringBoot+SpringSecurity+thymeleaf 代码实现

本文档介绍了如何使用SpringBoot、SpringSecurity和Thymeleaf搭建安全登录系统。详细阐述了MySQL数据库连接配置、设计登陆页面、配置webSecurityConfig、定义CustomUserService、userRepository以及用户和角色模型的创建过程。通过这个实践,读者可以理解SpringSecurity的认证与授权流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot+SpringSecurity+thymeleaf 代码实现

最近这两天在学习spring security 学的心累的不要不要的,终于在今天让我成功的写了一个成功案例,开心。

并且在学习的过程中找到了一个springsecurity4官方文档的中文版,写的非常棒(个人感觉) 传送门:没错我就是传送门

放代码之前说下环境:
1.开发工具 idea
2.项目集成工具 gradle 2.0
3.数据库 MySQL5.6
4.框架(只说了主要的):springboot-2.0 ;springsecurity-5.0

以下是主要代码:

MySQL数据库连接配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
登陆页面

在登陆页面中用户名必须是username,密码必须是password,提交的路径必须是/login,方式必须是post。 当你post请求/login时就会进入到spring security的验证方法中

<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
      layout:decorator="layout">
<head><title>Login Page</title></head>
<body onload='document.f.username.focus();'>
<h3>登陆</h3>
<form name='f' action="/login" th:action="@{/login}" th:method='POST'>
    <table>
        <tr>
            <td>User:</td>
            <td><input type='text' name='username' value=''></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type='password' name='password'/></td>
        </tr>
        <tr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值