This commit is contained in:
iizmg
2020-04-30 13:32:55 +08:00
parent bd410387df
commit a4e731a610
8 changed files with 193 additions and 21 deletions
@@ -7,6 +7,7 @@ import org.itkk.udf.starter.core.exception.AuthException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Optional;
@@ -60,6 +61,18 @@ public class CommonUtil {
return null;
}
/**
* 设置cookie
*
* @param httpServletResponse httpServletResponse
* @param name name
* @param value value
*/
public static void setCookie(HttpServletResponse httpServletResponse, String name, String value) {
Cookie cookie = new Cookie(name, value);
cookie.setPath("/");
httpServletResponse.addCookie(cookie);
}
/**
* 私有化构造函数
@@ -1,8 +1,10 @@
package org.itkk.udf.api.page.web;
import org.itkk.udf.api.common.CommonConstant;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HomeController {
@@ -14,7 +16,19 @@ public class HomeController {
*/
@GetMapping(value = {"/", "/index.html"})
public String index(Model map) {
//跳转到首页
return "index";
}
/**
* 注册页
*
* @param map map
* @return String
*/
@GetMapping("/registered.html")
public String registered(@RequestParam("redirectUrl") String redirectUrl, Model map) {
map.addAttribute("redirectUrl", redirectUrl);
map.addAttribute("tokenName", CommonConstant.PARAMETER_NAME_TOKEN);
return "registered";
}
}
@@ -18,7 +18,6 @@ import org.itkk.udf.api.rbac.repository.IUserRepository;
import org.itkk.udf.starter.cache.db.dto.DbCacheDto;
import org.itkk.udf.starter.cache.db.service.DbCacheService;
import org.itkk.udf.starter.core.CoreUtil;
import org.itkk.udf.starter.core.exception.AuthException;
import org.itkk.udf.starter.core.exception.ParameterValidException;
import org.itkk.udf.starter.core.id.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
@@ -90,7 +89,7 @@ public class UserService implements IUserService {
if (registeredDto == null) {
throw new ParameterValidException("注册信息为空");
}
if (registeredDto.getPswd().equals(registeredDto.getPswdConfim())) {
if (!registeredDto.getPswd().equals(registeredDto.getPswdConfim())) {
throw new ParameterValidException("两次密码输入不一致");
}
if (iUserRepository.selectCount(new QueryWrapper<UserEntity>().lambda().eq(UserEntity::getUserId, registeredDto.getUserId())) > 0) {
@@ -224,7 +223,7 @@ public class UserService implements IUserService {
@Override
public UserDto infoByToken(String token) {
//判空
if (StringUtils.isBlank(token)) {
if (StringUtils.isNotBlank(token)) {
//获得token信息
DbCacheDto dbCacheDto = dbCacheService.getDto(token);
//判空
@@ -271,18 +270,15 @@ public class UserService implements IUserService {
}
//获得用户信息
UserDto userDto = this.info(userId);
//判空
if (userDto == null) {
throw new AuthException("用户不存在");
//判空 (status = 1 为正常 , 其它为不正常)
if (userDto != null && Integer.valueOf(1).equals(userDto.getStatus())) {
//放入缓存
cache.put(cacheKey, userDto);
//返回
return userDto;
}
//判定状态(status = 1 为正常 , 其它为不正常)
if (!Integer.valueOf(1).equals(userDto.getStatus())) {
throw new AuthException("用户为非可用状态");
}
//放入缓存
cache.put(cacheKey, userDto);
//返回
return userDto;
return null;
}
/**
@@ -11,12 +11,20 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
@RestController
@RequestMapping(value = CommonConstant.URL_ROOT_WEB_PUBLIC + "rbac/user")
public class UserPublicController {
/**
* httpServletResponse
*/
@Autowired
private HttpServletResponse httpServletResponse;
/**
* pmPqrrService
*/
@@ -31,7 +39,11 @@ public class UserPublicController {
*/
@PostMapping("login")
public RestResponse<String> login(@RequestBody @Valid LoginDto loginDto) {
return new RestResponse<>(userService.login(loginDto));
final String token = userService.login(loginDto);
Cookie cookie = new Cookie(CommonConstant.PARAMETER_NAME_TOKEN, token);
cookie.setPath("/");
httpServletResponse.addCookie(cookie);
return new RestResponse<>(token);
}
/**
@@ -42,7 +54,11 @@ public class UserPublicController {
*/
@PostMapping("registered")
public RestResponse<String> registered(@RequestBody @Valid RegisteredDto registeredDto) {
return new RestResponse<>(userService.registered(registeredDto));
final String token = userService.registered(registeredDto);
Cookie cookie = new Cookie(CommonConstant.PARAMETER_NAME_TOKEN, token);
cookie.setPath("/");
httpServletResponse.addCookie(cookie);
return new RestResponse<>(token);
}
}
@@ -1,5 +1,7 @@
# HTTP
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
# file encoding
spring.mandatory-file-encoding=UTF-8
# INFO
@@ -16,6 +18,7 @@ info.build.java.target=${java.version}
spring.application.name=${project.name}
# HTTP SERVER
server.port=${server.port}
server.tomcat.uri-encoding=UTF-8
# compression
server.compression.enabled=true
server.compression.min-response-size=1KB
@@ -1,6 +1,6 @@
<!doctype html>
<html>
<head th:fragment="head">
<head th:fragment="head(title)">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css"
@@ -304,7 +304,7 @@
position: inherit;
}
</style>
<title>ITKK</title>
<title>[(${title})]</title>
</head>
<body>
<div th:fragment="top">
@@ -433,8 +433,16 @@
}
});
});
//注册按钮点击事件
$('.header-right-bar-registered-div button').on('click', function () {
//跳转到注册页面
openLoading();
//获得重定向地址
var redirectUrl = escape(window.location.href);
//跳转登陆界面
window.location.href = '/registered.html?redirectUrl=' + redirectUrl;
});
});
//设置(全局)的AJAX请求默认选项
$(function () {
$.ajaxSetup({
@@ -448,10 +456,28 @@
closeLoading();
},
error: function (xhr) {
var content = '服务器开小差了 , 请稍后再试';
try {
var errorResult = xhr.responseJSON;
if (errorResult) {
if ('org.springframework.web.bind.MethodArgumentNotValidException' === errorResult.errorDetail.type) {
var message = JSON.parse(errorResult.errorDetail.message.replace(/"/g, '"'));
var msgBody = '';
$.each(message, function (i, item) {
msgBody += (i + 1) + ' . ' + item.defaultMessage + '<br />';
});
content = msgBody;
} else {
content = errorResult.errorDetail.message;
}
}
} catch (e) {
console.log(e);
}
$('.toastBox').addToast({
showTimeLabel: false,
title: '消息',
content: '服务器开小差了 , 请稍后再试',
content: content,
autoHide: true,
type: 'info',
progress: {show: false},
@@ -497,6 +523,34 @@
$('.custom-loadding')[0].remove();
}
}
//写cookies
function setCookie(name, value) {
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
//读取cookies
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr === document.cookie.match(reg)) {
return unescape(arr[2]);
} else {
return null;
}
}
//删除cookies
function delCookie(name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if (cval != null) {
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
}
</script>
</div>
</body>
@@ -1,6 +1,6 @@
<!doctype html>
<html>
<header th:replace="common::head"></header>
<header th:replace="common::head(title='ITKK首页')"></header>
<style lang="css">
</style>
@@ -0,0 +1,76 @@
<!doctype html>
<html>
<header th:replace="common::head(title='ITKK注册')"></header>
<style lang="css">
.registered-card {
min-width: 300px;
max-width: 400px;
margin: auto !important;
}
</style>
<body>
<div th:include="common::top"></div>
<div class="main pre-scrollable">
<div class="card registered-card">
<div class="card-body">
<form class="registered-form">
<div class="form-group">
<label for="userId">用户名</label>
<input type="text" class="form-control form-control-lg" id="userId" aria-describedby="userIdHelp">
<small id="userIdHelp" class="form-text text-muted">需全站唯一</small>
</div>
<div class="form-group">
<label for="pswd">密码</label>
<input type="password" class="form-control form-control-lg" id="pswd" aria-describedby="pswdHelp">
<small id="pswdHelp" class="form-text text-muted">至少6位</small>
</div>
<div class="form-group">
<label for="pswdConfim">密码确认</label>
<input type="password" class="form-control form-control-lg" id="pswdConfim"
aria-describedby="pswdConfimHelp">
<small id="pswdConfimHelp" class="form-text text-muted">需和密码一致</small>
</div>
<div class="form-group">
<button type="button" class="btn btn-success registered-btn float-left">注册</button>
<button type="button" class="btn btn-link login-btn float-right">已有账号? 请直接登陆</button>
</div>
</form>
</div>
</div>
</div>
<div th:include="common::bottom"></div>
<div th:include="common::footer"></div>
</body>
<script type="text/javascript">
//页面加载完成
$(document).ready(function () {
//注册按钮点击事件
$('.registered-btn').on('click', function () {
//构造参数
var param = {
userId: $('#userId').val(),
pswd: $('#pswd').val(),
pswdConfim: $('#pswdConfim').val(),
};
//请求
$.ajax({
url: '/api/udf/web/public/rbac/user/registered',
type: 'POST',
async: true,
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify(param),
success: function (data) {
if (data.result) {
if ('[(${redirectUrl})]') {
window.location.href = '[(${redirectUrl})]';
} else {
window.location.href = '/';
}
}
}
});
});
});
</script>
</html>