yanyang #12

Merged
yanyang merged 2 commits from yanyang into master 2023-03-02 17:02:52 +08:00
3 changed files with 23 additions and 10 deletions
Showing only changes of commit e405dcb06a - Show all commits

View File

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

View File

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

View File

@ -1,5 +1,7 @@
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.SysLogLoginEntity;
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.context.SecurityContextHolder;
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.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@ -81,6 +86,11 @@ public class LoginServiceImpl implements LoginService {
map.put("token",jwt);
//把完整的用户信息存入redis userid作为key
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.setCreator(loginUser.getSysUserEntity().getId());