Merge pull request 'yanyang' (#12) from yanyang into master

Reviewed-on: #12
This commit is contained in:
闫阳 2023-03-02 17:02:51 +08:00
commit dd9cff155e
3 changed files with 23 additions and 10 deletions

View File

@ -7,6 +7,7 @@ package com.cnbm.admin.base;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.cnbm.admin.dto.LoginUserDTO; import com.cnbm.admin.dto.LoginUserDTO;
import com.cnbm.admin.entity.LoginUser;
import com.cnbm.admin.enums.WhetherEnum; import com.cnbm.admin.enums.WhetherEnum;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@ -32,18 +33,18 @@ import java.time.LocalDateTime;
@Service @Service
public class BaseSupport { public class BaseSupport {
protected LoginUserDTO getLoginUser() { protected LoginUser getLoginUser() {
// 后续完善拦截器再使用该方式 // 后续完善拦截器再使用该方式
// LoginUser loginUser = loginUserHolder.get(); // LoginUser loginUser = loginUserHolder.get();
// if (loginUser != null) { // if (loginUser != null) {
// return loginUser; // return loginUser;
// } // }
HttpSession session = getHttpServletRequest().getSession(false); HttpSession session = getHttpServletRequest().getSession(false);
LoginUserDTO loginUser = null; LoginUser loginUser = null;
if (session != null) { if (session != null) {
String loginUserJson = (String) session.getAttribute(LoginUserDTO.HTTP_HEADER_NAME); String loginUserJson = (String) session.getAttribute(LoginUser.HTTP_HEADER_NAME);
if (StringUtils.isNotBlank(loginUserJson)) { if (StringUtils.isNotBlank(loginUserJson)) {
loginUser = JSON.parseObject(loginUserJson, LoginUserDTO.class); loginUser = JSON.parseObject(loginUserJson, LoginUser.class);
return loginUser; return loginUser;
} }
} }
@ -93,11 +94,11 @@ public class BaseSupport {
.enabled(WhetherEnum.YES.getValue()) .enabled(WhetherEnum.YES.getValue())
.valid(WhetherEnum.YES.getValue()) .valid(WhetherEnum.YES.getValue())
.createTime(LocalDateTime.now()) .createTime(LocalDateTime.now())
.creatorId(getLoginUser().getUserId()) .creatorId(getLoginUser().getSysUserEntity().getId())
.creatorName(getLoginUser().getUserName()) .creatorName(getLoginUser().getUsername())
.updateTime(LocalDateTime.now()) .updateTime(LocalDateTime.now())
.updaterId(getLoginUser().getUserId()) .updaterId(getLoginUser().getSysUserEntity().getId())
.updaterName(getLoginUser().getUserName()) .updaterName(getLoginUser().getUsername())
.build(); .build();
BeanUtils.copyProperties(commonField, t, ignoreProperties); BeanUtils.copyProperties(commonField, t, ignoreProperties);
return t; return t;
@ -117,8 +118,8 @@ public class BaseSupport {
*/ */
public <T extends Serializable> T setUpdateCommonField(T t) { public <T extends Serializable> T setUpdateCommonField(T t) {
CommonField commonField = CommonField.builder() CommonField commonField = CommonField.builder()
.updaterId(getLoginUser().getUserId()) .updaterId(getLoginUser().getSysUserEntity().getId())
.updaterName(getLoginUser().getUserName()) .updaterName(getLoginUser().getUsername())
.updateTime(LocalDateTime.now()) .updateTime(LocalDateTime.now())
.build(); .build();
BeanUtils.copyProperties(commonField, t, "enabled", "valid"); BeanUtils.copyProperties(commonField, t, "enabled", "valid");

View File

@ -24,6 +24,8 @@ public class LoginUser implements UserDetails{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static final String HTTP_HEADER_NAME = "loginUser";
private SysUserEntity sysUserEntity; private SysUserEntity sysUserEntity;
private List<String> permissions; private List<String> permissions;

View File

@ -1,5 +1,7 @@
package com.cnbm.admin.service.impl; package com.cnbm.admin.service.impl;
import com.alibaba.fastjson.JSON;
import com.cnbm.admin.dto.LoginUserDTO;
import com.cnbm.admin.entity.LoginUser; import com.cnbm.admin.entity.LoginUser;
import com.cnbm.admin.entity.SysLogLoginEntity; import com.cnbm.admin.entity.SysLogLoginEntity;
import com.cnbm.admin.enums.LoginOperationEnum; import com.cnbm.admin.enums.LoginOperationEnum;
@ -22,8 +24,11 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -81,6 +86,11 @@ public class LoginServiceImpl implements LoginService {
map.put("token",jwt); map.put("token",jwt);
//把完整的用户信息存入redis userid作为key //把完整的用户信息存入redis userid作为key
redisTemplate.opsForValue().set("login:"+userid,loginUser,1, TimeUnit.DAYS); redisTemplate.opsForValue().set("login:"+userid,loginUser,1, TimeUnit.DAYS);
//当前登录用户信息存入session中
HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
session.setAttribute(loginUser.HTTP_HEADER_NAME, JSON.toJSONString(loginUser));
//登录成功 //登录成功
log.setStatus(LoginStatusEnum.SUCCESS.value()); log.setStatus(LoginStatusEnum.SUCCESS.value());
log.setCreator(loginUser.getSysUserEntity().getId()); log.setCreator(loginUser.getSysUserEntity().getId());