1、完成一個註冊登錄功能,數據不需要保存到數據庫。不需要分頁。必須是SpringBoot項目,頁面模板必須是Thymeleaf
(1)引入LayUI或Bootstrap等其它美化頁面(5)
登錄頁面 login.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"> <!--爲了讓頁面有更好的提示效果-->
<head>
<meta charset="UTF-8">
<title>後臺管理-登陸</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
<!--[if lt IE 9]>
<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<style>
html, body {width: 100%;height: 100%;overflow: hidden}
body {background: #1E9FFF;}
body:after {content:'';background-repeat:no-repeat;background-size:cover;-webkit-filter:blur(3px);-moz-filter:blur(3px);-o-filter:blur(3px);-ms-filter:blur(3px);filter:blur(3px);position:absolute;top:0;left:0;right:0;bottom:0;z-index:-1;}
.layui-container {width: 100%;height: 100%;overflow: hidden}
.admin-login-background {width:360px;height:300px;position:absolute;left:50%;top:40%;margin-left:-180px;margin-top:-100px;}
.logo-title {text-align:center;letter-spacing:2px;padding:14px 0;}
.logo-title h1 {color:#1E9FFF;font-size:25px;font-weight:bold;}
.login-form {background-color:#fff;border:1px solid #fff;border-radius:3px;padding:14px 20px;box-shadow:0 0 8px #eeeeee;}
.login-form .layui-form-item {position:relative;}
.login-form .layui-form-item label {position:absolute;left:1px;top:1px;width:38px;line-height:36px;text-align:center;color:#d2d2d2;}
.login-form .layui-form-item input {padding-left:36px;}
.captcha {width:60%;display:inline-block;}
.captcha-img {display:inline-block;width:34%;float:right;}
.captcha-img img {height:34px;border:1px solid #e6e6e6;height:36px;width:100%;}
</style>
</head>
<body>
<div class="layui-container">
<div class="admin-login-background">
<div class="layui-form login-form">
<form class="layui-form" th:action="@{/admin/login}" method="post">
<div class="layui-form-item logo-title">
<h1>後臺登錄</h1>
</div>
<div class="layui-form-item">
<label class="layui-icon layui-icon-username" for="account"></label>
<input type="text" name="account" placeholder="用戶名或者郵箱" autocomplete="off" class="layui-input">
<!--lay-verify是必填項,不填數據也會認爲數據不合法,對於非必填項來說,只有填了才進行校驗-->
</div>
<div class="layui-form-item">
<label class="layui-icon layui-icon-password" for="password"></label>
<input type="password" name="password" placeholder="密碼" autocomplete="off" class="layui-input">
</div>
<div class="layui-form-item">
<label class="layui-icon layui-icon-vercode" for="captcha"></label>
<input type="text" name="captcha" placeholder="圖形驗證碼" autocomplete="off" class="layui-input verification captcha" value="xszg">
<div class="captcha-img">
<img id="captchaPic" th:src="@{/images/captcha.jpg}">
</div>
</div>
<div class="layui-form-item">
<input type="checkbox" name="rememberMe" value="true" lay-skin="primary" title="記住密碼">
</div>
<div class="layui-form-item">
<button class="layui-btn layui-btn layui-btn-normal layui-btn-fluid" lay-submit="" lay-filter="login">登 陸</button>
</div>
<div class="layui-form-item">
<button class="layui-btn layui-btn layui-btn-normal layui-btn-fluid" lay-submit="" lay-filter="register"><a href="register.html">注 冊</button>
</div>
</form>
</div>
</div>
</div>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/jq-module/jquery.particleground.min.js}" charset="utf-8"></script>
<script>
layui.use(['form'], function () {
var form = layui.form,
layer = layui.layer;
// 登錄過期的時候,跳出ifram框架
if (top.location != self.location) top.location = self.location;
// 粒子線條背景
$(document).ready(function(){
$('.layui-container').particleground({
dotColor:'#7ec7fd',
lineColor:'#7ec7fd'
});
});
// 進行登錄操作
form.on('submit(login)', function (data) {
data = data.field;
if (data.username == '') {
layer.msg('用戶名不能爲空');
return false;
}
if (data.password == '') {
layer.msg('密碼不能爲空');
return false;
}
if (data.captcha == '') {
layer.msg('驗證碼不能爲空');
return false;
}
// layer.msg('登錄成功', function () {
// window.location = 'index.html';
// });
return true;
});
});
</script>
</body>
</html>
註冊頁面register.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>註冊頁</title>
<link rel="stylesheet" th:href="@{/css/layuimini.css}">
<link rel="stylesheet" th:href="@{/css/style.css}">
</head>
<body>
<div class="login-main">
<header class="layui-elip" style="width: 85%">註冊</header>
<form class="layui-form">
<!--輸入用戶名-->
<div class="layui-input-inline">
<div class="layui-inline" style="width: 85%">
<input type="text" name="account" id="account" required lay-verify="required" placeholder="用戶名" autocomplete="off" class="layui-input">
</div>
<!--判斷用戶名是否可用的圖標 -->
<div class="layui-inline">
<i class="layui-icon" hidden id="ri" style="color: green; font-weight: bold"></i>
<i class="layui-icon" hidden id="le" style="color: red; font-weight: bold">ဆ</i>
</div>
</div>
<!--輸入密碼-->
<div class="layui-input-inline">
<div class="layui-inline" style="width: 85%">
<input type="password" name="password" id="password" required lay-verify="required" placeholder="密碼" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-input-inline login-btn" style="width: 85%">
<button type="submit" lay-submit lay-filter="sub" class="layui-btn" lay->註冊</button>
</div>
<br/>
<p style="width: 85%">
<a th:href="login.html" class="fl">已有賬號?立即登錄</a>
<a href="javascript:;" class="fr">忘記密碼?</a>
</p>
</form>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}"></script>
<script type="text/javascript">
layui.use(['form', 'layer','jquery'], function () {
var form = layui.form;
var $ = layui.jquery;
//爲表單添加blur事件
$('#account').blur(function () {
var account = $('#account').val();
//ajax異步刷新
$.ajax({
url:'page/checkUser.php',
type:'post',
dataType:'text',
data:{account:account},
//驗證用戶名是否可用
success:function (data) {
if(data=='1'){
//layer.msg('可以註冊')
$('#ri').removeAttr('hidden');
$('#le').attr('hidden','hidden');
}else{
//layer.msg('用戶名已被佔用')
$('#ri').attr('hidden','hidden');
$('#le').removeAttr('hidden');
}
}
})
});
//添加表單監聽事件
form.on('submit(sub)',function () {
$.ajax({
url:'page/regist.php',
type:'post',
data:{account:$('#account').val(),
password:$('#password').val()},
dataType:'text',
//判斷註冊狀態
success:function (data) {
if (data==1){
layer.msg('註冊成功')
}else{
layer.msg('註冊失敗')
}
}
})
//防止頁面跳轉
return false;
});
});
</script>
</body>
</html>
(2)完成登錄頁面(35)
-
必須是註冊過的賬號密碼(10)
-
實現驗證碼功能(10)
-
實現記住密碼功能(10)
-
實現表單輸入框非空驗證和完成跳轉到註冊頁面(5)
實現代碼
@GetMapping("toLogin")
public String toLogin() {
return "login";
}
@PostMapping("login")
public String login(String account, String password, String captcha, HttpServletRequest httpServletRequest) {
HttpSession session = httpServletRequest.getSession();
String captcha_key = (String) session.getAttribute("captcha_key");
if (captcha.equals(captcha_key)){
Admin admin = adminService.login(account, password);
if (admin != null) {
session.setAttribute("admin", admin);
return "index";
}
}
return "login";
}
public class Captcha {
private String captcha;
private LocalDateTime expireTime;
/**
* 設置驗證碼過期時間
* @param captcha
* @param expireAfterSeconds
*/
public Captcha(String captcha, int expireAfterSeconds){
this.captcha = captcha;
this.expireTime = LocalDateTime.now().plusSeconds(expireAfterSeconds);
}
public String getCaptcha() {
return captcha;
}
//判斷驗證碼是否過期
public boolean isExpired(){
return LocalDateTime.now().isAfter(expireTime);
}
}
@Configuration
@PropertySource(value = {"classpath:kaptcha.properties"})
public class CaptchaConfig {
@Value("${kaptcha.border}")
private String border;
@Value("${kaptcha.border.color}")
private String borderColor;
@Value("${kaptcha.textproducer.font.color}")
private String fontColor;
@Value("${kaptcha.image.width}")
private String imageWidth;
@Value("${kaptcha.image.height}")
private String imageHeight;
@Value("${kaptcha.seesion.key}")
private String sessionKey;
@Value("${kaptcha.textproducer.char.length}")
private String charLength;
@Value("${kaptcha.textproducer.font.names}")
private String fontNames;
@Value("${kaptcha.textproducer.font.size}")
private String fontSize;
@Bean(name = "captchaProducer")
public DefaultKaptcha getKaptchaBean(){
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty("kaptcha.border", border);
properties.setProperty("kaptcha.border.color", borderColor);
properties.setProperty("kaptcha.textproducer.font.color", fontColor);
properties.setProperty("kaptcha.image.width", imageWidth);
properties.setProperty("kaptcha.image.height", imageHeight);
properties.setProperty("kaptcha.seesion.key", sessionKey);
properties.setProperty("kaptcha.textproducer.char.length", charLength);
properties.setProperty("kaptcha.textproducer.font.names", fontNames);
properties.setProperty("kaptcha.textproducer.font.size", fontSize);
defaultKaptcha.setConfig(new Config(properties));
return defaultKaptcha;
}
}
@RestController
public class CaptchaController {
@Autowired
DefaultKaptcha captchaProducer;
@RequestMapping(value = "/kaptcha", method = RequestMethod.GET)
public void kaptcha(HttpSession session, HttpServletResponse response) throws IOException {
//禁止使用緩存圖片
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, ,ust-revalidate");
response.setHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
//創建驗證碼文本
String text = captchaProducer.createText();
//把文本存進緩存
session.setAttribute(MyContants.CAPTCHA_CODE_KEY, new CaptchaCode(text, 2 * 60));
try (ServletOutputStream outputStream = response.getOutputStream()) {
//創建驗證碼圖片
BufferedImage image = captchaProducer.createImage(text);
//把圖片送到頁面
ImageIO.write(image,"jpg", outputStream);
outputStream.flush();
}
}
}
(3)完成註冊頁面和簡單登錄成功頁面(10)
實現代碼
@Controller
public class AdminContorller {
@Autowired
private AdminService adminService;
@GetMapping("toLogin")
public String toLogin() {
return "login";
}
@PostMapping("login")
public String login(String account, String password, String captcha, HttpServletRequest httpServletRequest) {
HttpSession session = httpServletRequest.getSession();
String captcha_key = (String) session.getAttribute("captcha_key");
if (captcha.equals(captcha_key)){
Admin admin = adminService.login(account, password);
if (admin != null) {
session.setAttribute("admin", admin);
return "index";
}
}
return "login";
}
@PutMapping("register")
public String register(Admin admin) {
boolean result = adminService.register(admin);
if (result) {
return "login";
}else {
return "register";
}
}
}