mirror of
https://gitee.com/wangkang/udf.git
synced 2026-11-06 07:25:31 +00:00
更新
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package org.itkk.udf.api.common;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.itkk.udf.api.common.dto.UserDto;
|
||||
import org.itkk.udf.starter.core.exception.AuthException;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 工具类
|
||||
@@ -40,6 +44,23 @@ public class CommonUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从cookie中获得token
|
||||
*
|
||||
* @param request request
|
||||
* @return String
|
||||
*/
|
||||
public static String getTokenByCookie(HttpServletRequest request) {
|
||||
if (ArrayUtils.isNotEmpty(request.getCookies())) {
|
||||
Optional<Cookie> cookie = Arrays.stream(request.getCookies()).filter(a -> a.getName().equals(CommonConstant.PARAMETER_NAME_TOKEN)).findFirst();
|
||||
if (cookie.isPresent()) {
|
||||
return cookie.get().getValue();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 私有化构造函数
|
||||
*/
|
||||
|
||||
@@ -36,10 +36,15 @@ public class UserDto implements Serializable {
|
||||
private String nickName;
|
||||
|
||||
/**
|
||||
* 用户头像文件ID(非空)
|
||||
* 用户头像文件ID
|
||||
*/
|
||||
private String avatarFileId;
|
||||
|
||||
/**
|
||||
* 用户头像路径
|
||||
*/
|
||||
private String avatarFilePath;
|
||||
|
||||
/**
|
||||
* 用户状态 (1:正常)
|
||||
*/
|
||||
|
||||
@@ -12,4 +12,12 @@ public interface IUserService {
|
||||
*/
|
||||
UserDto info(String userId);
|
||||
|
||||
/**
|
||||
* 获得用户信息
|
||||
*
|
||||
* @param token token
|
||||
* @return UserDto
|
||||
*/
|
||||
UserDto infoByToken(String token);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.itkk.udf.api.rbac;
|
||||
|
||||
import org.itkk.udf.api.common.CommonConstant;
|
||||
import org.itkk.udf.api.rbac.auth.CommonHandlerInterceptor;
|
||||
import org.itkk.udf.api.rbac.auth.WebHandlerInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -18,6 +19,12 @@ public class RbacConfiguration implements WebMvcConfigurer {
|
||||
@Autowired
|
||||
private WebHandlerInterceptor webHandlerInterceptor;
|
||||
|
||||
/**
|
||||
* commonHandlerInterceptor
|
||||
*/
|
||||
@Autowired
|
||||
private CommonHandlerInterceptor commonHandlerInterceptor;
|
||||
|
||||
/**
|
||||
* 添加拦截器
|
||||
*
|
||||
@@ -25,7 +32,7 @@ public class RbacConfiguration implements WebMvcConfigurer {
|
||||
*/
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
//身份认证拦截器
|
||||
registry.addInterceptor(webHandlerInterceptor).order(1).addPathPatterns(CommonConstant.URL_ROOT_WEB_PRIVATE + "**");
|
||||
registry.addInterceptor(commonHandlerInterceptor).order(1).addPathPatterns("/**");
|
||||
registry.addInterceptor(webHandlerInterceptor).order(2).addPathPatterns(CommonConstant.URL_ROOT_WEB_PRIVATE + "**");
|
||||
}
|
||||
}
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package org.itkk.udf.api.rbac.auth;
|
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.itkk.udf.api.common.CommonUtil;
|
||||
import org.itkk.udf.api.common.dto.UserDto;
|
||||
import org.itkk.udf.api.common.service.IUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CommonHandlerInterceptor implements HandlerInterceptor {
|
||||
/**
|
||||
* iUserService
|
||||
*/
|
||||
@Autowired
|
||||
private IUserService iUserService;
|
||||
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
//判空
|
||||
if (modelAndView != null) {
|
||||
//获得用户信息
|
||||
UserDto userDto = iUserService.infoByToken(CommonUtil.getTokenByCookie(request));
|
||||
//判断是否登陆
|
||||
if (userDto != null) {
|
||||
//放入用户信息
|
||||
modelAndView.addObject("userDto", userDto);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-14
@@ -2,12 +2,10 @@ package org.itkk.udf.api.rbac.auth;
|
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.itkk.udf.api.common.CommonConstant;
|
||||
import org.itkk.udf.api.common.CommonProperties;
|
||||
import org.itkk.udf.api.common.dto.UserDto;
|
||||
import org.itkk.udf.api.rbac.service.UserService;
|
||||
import org.itkk.udf.starter.cache.db.dto.DbCacheDto;
|
||||
import org.itkk.udf.api.common.service.IUserService;
|
||||
import org.itkk.udf.starter.cache.db.service.DbCacheService;
|
||||
import org.itkk.udf.starter.core.CoreUtil;
|
||||
import org.itkk.udf.starter.core.exception.AuthException;
|
||||
@@ -35,10 +33,10 @@ public class WebHandlerInterceptor implements HandlerInterceptor {
|
||||
private CommonProperties commonProperties;
|
||||
|
||||
/**
|
||||
* userService
|
||||
* iUserService
|
||||
*/
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
private IUserService iUserService;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
@@ -46,18 +44,12 @@ public class WebHandlerInterceptor implements HandlerInterceptor {
|
||||
if (!HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
|
||||
//获得token
|
||||
final String token = CoreUtil.getParameter(request, CommonConstant.PARAMETER_NAME_TOKEN);
|
||||
//获得tokenInfo
|
||||
UserDto userDto = iUserService.infoByToken(token);
|
||||
//判空
|
||||
if (StringUtils.isBlank(token)) {
|
||||
throw new AuthException("缺少必要参数token");
|
||||
}
|
||||
//获得token信息
|
||||
DbCacheDto dbCacheDto = dbCacheService.getDto(token);
|
||||
//判空
|
||||
if (dbCacheDto == null) {
|
||||
if (userDto == null) {
|
||||
throw new AuthException("token不存在");
|
||||
}
|
||||
//获得tokenInfo
|
||||
UserDto userDto = userService.info(token, dbCacheDto.getValue());
|
||||
//放入请求作用域
|
||||
request.setAttribute(CommonConstant.PARAMETER_NAME_TOKEN_INFO, userDto);
|
||||
//刷新token ttl
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.itkk.udf.api.rbac.dto.RegisteredDto;
|
||||
import org.itkk.udf.api.rbac.dto.UserSaveDto;
|
||||
import org.itkk.udf.api.rbac.entity.UserEntity;
|
||||
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;
|
||||
@@ -252,6 +253,21 @@ public class UserService implements IUserService {
|
||||
return userDto;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDto infoByToken(String token) {
|
||||
//判空
|
||||
if (StringUtils.isBlank(token)) {
|
||||
//获得token信息
|
||||
DbCacheDto dbCacheDto = dbCacheService.getDto(token);
|
||||
//判空
|
||||
if (dbCacheDto != null) {
|
||||
this.info(token, dbCacheDto.getValue());
|
||||
}
|
||||
}
|
||||
//返回
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDto info(String userId) {
|
||||
//定义返回值
|
||||
|
||||
@@ -14,8 +14,6 @@ public class HomeController {
|
||||
*/
|
||||
@GetMapping(value = {"/", "/index.html"})
|
||||
public String index(Model map) {
|
||||
//放入作用域参数
|
||||
map.addAttribute("title", "ITKK");
|
||||
//跳转到首页
|
||||
return "index";
|
||||
}
|
||||
|
||||
@@ -293,7 +293,7 @@
|
||||
padding-top: 5px;
|
||||
}
|
||||
</style>
|
||||
<title>[(${title})]</title>
|
||||
<title>ITKK</title>
|
||||
</head>
|
||||
<body>
|
||||
<div th:fragment="top">
|
||||
@@ -319,18 +319,16 @@
|
||||
<sup class="el-badge__content el-badge__content--undefined is-fixed is-dot"></sup>
|
||||
</div>
|
||||
</div>
|
||||
<!--
|
||||
<div class="item header-right-bar-user-div">
|
||||
<button type="button" class="btn btn-info btn-custom3">
|
||||
<div th:if="${userDto!=null}" class="item header-right-bar-user-div">
|
||||
<button th:if="${userDto.avatarFilePath==null}" type="button" class="btn btn-info btn-custom3 user-head-img-def">
|
||||
<i class="fa fa-user" aria-hidden="true"></i>
|
||||
</button>
|
||||
<!–<img width="40px" height="40px" src="https://getbootstrap.com/docs/4.4/assets/img/favicons/apple-touch-icon.png" class="img-rounded" alt="头像">–>
|
||||
<img th:if="${userDto.avatarFilePath!=null}" width="40px" height="40px" th:src="${userDto.avatarFilePath}" class="img-rounded user-head-img" alt="头像">
|
||||
</div>
|
||||
-->
|
||||
<div class="item header-right-bar-login-div">
|
||||
<div th:if="${userDto==null}" class="item header-right-bar-login-div">
|
||||
<button type="button" class="btn btn-custom rounded-pill">登陆</button>
|
||||
</div>
|
||||
<div class="item header-right-bar-registered-div">
|
||||
<div th:if="${userDto==null}" class="item header-right-bar-registered-div">
|
||||
<button type="button" class="btn btn-custom rounded-pill">注册</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -360,6 +358,12 @@
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap-toasts-js@1.1.4/dist/js/bootstrap-toasts-js.min.js"
|
||||
integrity="sha256-8eA/hiFNtfHeSl0SbSynWCaqa6DeEzsLD1lxsS8JnzM=" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript">
|
||||
//token名称
|
||||
var TOKEN_NAME = 'udf_token';
|
||||
//错误消息持续时间
|
||||
var ERROR_MESSAGE_DURATION = 3;
|
||||
//默认提示语
|
||||
var DEF_ERROR_INFO_MESSAGE = '服务器开小差了 , 请稍后再试';
|
||||
//页面加载完成
|
||||
$(document).ready(function () {
|
||||
//设置main的高度
|
||||
@@ -426,11 +430,11 @@
|
||||
$('.toastBox').addToast({
|
||||
showTimeLabel: false,
|
||||
title: '消息',
|
||||
content: '服务器开小差了,请稍后再试',
|
||||
content: DEF_ERROR_INFO_MESSAGE,
|
||||
autoHide: true,
|
||||
type: 'info',
|
||||
progress: {show: false},
|
||||
displayTime: 4000
|
||||
displayTime: ERROR_MESSAGE_DURATION * 1000
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user