This commit is contained in:
2025-06-20 17:32:58 +08:00
parent 9fa8a64949
commit 7549219213
6 changed files with 38 additions and 93 deletions

View File

@ -27,6 +27,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.Set; import java.util.Set;
@ -45,8 +46,8 @@ public class OnlineController {
@ApiOperation("查询在线用户") @ApiOperation("查询在线用户")
@GetMapping @GetMapping
@PreAuthorize("@el.check()") @PreAuthorize("@el.check()")
public ResponseEntity<PageResult<OnlineUserDto>> queryOnlineUser(String username, Pageable pageable){ public ResponseEntity<PageResult<OnlineUserDto>> queryOnlineUser(String username, Pageable pageable) {
return new ResponseEntity<>(onlineUserService.getAll(username, pageable),HttpStatus.OK); return new ResponseEntity<>(onlineUserService.getAll(username, pageable), HttpStatus.OK);
} }
@ApiOperation("导出数据") @ApiOperation("导出数据")
@ -67,4 +68,5 @@ public class OnlineController {
} }
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
} }

View File

@ -77,11 +77,11 @@ public class AppTokenProvider implements InitializingBean {
Map<String, Object> claims = new HashMap<>(6); Map<String, Object> claims = new HashMap<>(6);
// 设置用户ID // 设置用户ID
// claims.put(AUTHORITIES_UID_KEY, user.getAppUser().getId()); // claims.put(AUTHORITIES_UID_KEY, user.getAppUser().getId());
if (user.getAppUser() != null){ // if (user.getAppUser() != null){
claims.put(AUTHORITIES_UID_KEY, user.getAppUser().getId()); // claims.put(AUTHORITIES_UID_KEY, user.getAppUser().getId());
}else { // }else {
claims.put(AUTHORITIES_UID_KEY, 0); // claims.put(AUTHORITIES_UID_KEY, 0);
} // }
// 设置UUID确保每次Token不一样 // 设置UUID确保每次Token不一样
claims.put(AUTHORITIES_UUID_KEY, IdUtil.simpleUUID()); claims.put(AUTHORITIES_UUID_KEY, IdUtil.simpleUUID());
return jwtBuilder return jwtBuilder

View File

@ -45,8 +45,6 @@ public class OnlineUserService {
private final RedisUtils redisUtils; private final RedisUtils redisUtils;
/** /**
* 保存在线用户信息 * 保存在线用户信息
* @param jwtUserDto / * @param jwtUserDto /
@ -54,22 +52,14 @@ public class OnlineUserService {
* @param request / * @param request /
*/ */
public void save(JwtUserDto jwtUserDto, String token, HttpServletRequest request){ public void save(JwtUserDto jwtUserDto, String token, HttpServletRequest request){
// String dept = jwtUserDto.getUser().getDept() == null ? null : jwtUserDto.getUser().getDept().getName(); String dept = jwtUserDto.getUser().getDept() == null ? null : jwtUserDto.getUser().getDept().getName();
String dept = null;
if (jwtUserDto.getUser() != null) {
dept = jwtUserDto.getUser().getDept() == null ? null : jwtUserDto.getUser().getDept().getName();
}else {
dept= jwtUserDto.getAppUser().getDept() == null ? null : jwtUserDto.getAppUser().getDept().getName();
}
String ip = StringUtils.getIp(request); String ip = StringUtils.getIp(request);
String id = tokenProvider.getId(token); String id = tokenProvider.getId(token);
String browser = StringUtils.getBrowser(request); String browser = StringUtils.getBrowser(request);
String address = StringUtils.getCityInfo(ip); String address = StringUtils.getCityInfo(ip);
OnlineUserDto onlineUserDto = null; OnlineUserDto onlineUserDto = null;
try { try {
onlineUserDto = new OnlineUserDto(id, jwtUserDto.getUsername(), jwtUserDto.getAppUser().getNickName(), dept, browser , ip, address, EncryptUtils.desEncrypt(token), new Date()); onlineUserDto = new OnlineUserDto(id, jwtUserDto.getUsername(), jwtUserDto.getUser().getNickName(), dept, browser , ip, address, EncryptUtils.desEncrypt(token), new Date());
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
} }

View File

@ -23,6 +23,7 @@ import com.fuyuanshen.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
/** /**
@ -38,8 +39,10 @@ public class UserCacheManager {
@Value("${login.user-cache.idle-time}") @Value("${login.user-cache.idle-time}")
private long idleTime; private long idleTime;
/** /**
* 返回用户缓存 * 返回用户缓存
*
* @param userName 用户名 * @param userName 用户名
* @return JwtUserDto * @return JwtUserDto
*/ */
@ -48,24 +51,15 @@ public class UserCacheManager {
userName = StringUtils.lowerCase(userName); userName = StringUtils.lowerCase(userName);
if (StringUtils.isNotEmpty(userName)) { if (StringUtils.isNotEmpty(userName)) {
// 获取数据 // 获取数据
try { return redisUtils.get(LoginProperties.cacheKey + userName, JwtUserDto.class);
JwtUserDto jwtUserDto = redisUtils.get(LoginProperties.cacheKey + userName, JwtUserDto.class);
if (jwtUserDto != null){
jwtUserDto.getUsername();
} }
return jwtUserDto;
} catch (Exception e) {
// redisUtils.del(LoginProperties.cacheKey + userName);
cleanUserCache(userName);
return null; return null;
} }
}
return null;
}
/** /**
* 添加缓存到Redis * 添加缓存到Redis
*
* @param userName 用户名 * @param userName 用户名
*/ */
@Async @Async
@ -82,6 +76,7 @@ public class UserCacheManager {
/** /**
* 清理用户缓存信息 * 清理用户缓存信息
* 用户信息变更时 * 用户信息变更时
*
* @param userName 用户名 * @param userName 用户名
*/ */
@Async @Async
@ -96,6 +91,7 @@ public class UserCacheManager {
/** /**
* 返回用户缓存 * 返回用户缓存
*
* @param userName 用户名 * @param userName 用户名
* @return JwtUserDto * @return JwtUserDto
*/ */
@ -106,7 +102,7 @@ public class UserCacheManager {
// 获取数据 // 获取数据
try { try {
JwtUserDto jwtUserDto = redisUtils.get(LoginProperties.cacheKey_app + userName, JwtUserDto.class); JwtUserDto jwtUserDto = redisUtils.get(LoginProperties.cacheKey_app + userName, JwtUserDto.class);
if (jwtUserDto != null){ if (jwtUserDto != null) {
jwtUserDto.getUsername(); jwtUserDto.getUsername();
} }
return jwtUserDto; return jwtUserDto;
@ -122,10 +118,11 @@ public class UserCacheManager {
/** /**
* 添加缓存到Redis * 添加缓存到Redis
*
* @param userName 用户名 * @param userName 用户名
*/ */
@Async @Async
public void addUserCache(String userName, JwtUserDto user,Integer userType) { public void addUserCache(String userName, JwtUserDto user, Integer userType) {
// 转小写 // 转小写
userName = StringUtils.lowerCase(userName); userName = StringUtils.lowerCase(userName);
if (StringUtils.isNotEmpty(userName)) { if (StringUtils.isNotEmpty(userName)) {
@ -138,10 +135,11 @@ public class UserCacheManager {
/** /**
* 清理用户缓存信息 * 清理用户缓存信息
* 用户信息变更时 * 用户信息变更时
*
* @param userName 用户名 * @param userName 用户名
*/ */
@Async @Async
public void cleanUserCache(String userName,Integer userType) { public void cleanUserCache(String userName, Integer userType) {
// 转小写 // 转小写
userName = StringUtils.lowerCase(userName); userName = StringUtils.lowerCase(userName);
if (StringUtils.isNotEmpty(userName)) { if (StringUtils.isNotEmpty(userName)) {

View File

@ -59,7 +59,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
// 获取用户的权限 // 获取用户的权限
List<AuthorityDto> authorities = roleService.buildPermissions(user); List<AuthorityDto> authorities = roleService.buildPermissions(user);
// 初始化JwtUserDto // 初始化JwtUserDto
jwtUserDto = new JwtUserDto(user, null,dataService.getDeptIds(user), authorities); jwtUserDto = new JwtUserDto(user, dataService.getDeptIds(user), authorities);
// 添加缓存数据 // 添加缓存数据
userCacheManager.addUserCache(username, jwtUserDto); userCacheManager.addUserCache(username, jwtUserDto);
} }
@ -82,7 +82,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
// 获取用户的权限 // 获取用户的权限
List<AuthorityDto> authorities = roleService.appBuildPermissions(user); List<AuthorityDto> authorities = roleService.appBuildPermissions(user);
// 初始化JwtUserDto // 初始化JwtUserDto
jwtUserDto = new JwtUserDto(null,user, dataService.getDeptIds(user), authorities); // jwtUserDto = new JwtUserDto(null,user, dataService.getDeptIds(user), authorities);
// 添加缓存数据 // 添加缓存数据
userCacheManager.addUserCache(username, jwtUserDto, userType); userCacheManager.addUserCache(username, jwtUserDto, userType);
} }

View File

@ -16,16 +16,13 @@
package com.fuyuanshen.modules.security.service.dto; package com.fuyuanshen.modules.security.service.dto;
import com.alibaba.fastjson2.annotation.JSONField; import com.alibaba.fastjson2.annotation.JSONField;
import com.fuyuanshen.modules.system.domain.app.APPUser;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import com.fuyuanshen.modules.system.domain.User; import com.fuyuanshen.modules.system.domain.User;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -34,98 +31,56 @@ import java.util.stream.Collectors;
* @author Zheng Jie * @author Zheng Jie
* @date 2018-11-23 * @date 2018-11-23
*/ */
@NoArgsConstructor(force = true)
@Getter @Getter
@Setter @AllArgsConstructor
public class JwtUserDto implements UserDetails, java.io.Serializable { public class JwtUserDto implements UserDetails {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "用户") @ApiModelProperty(value = "用户")
private final User user; private final User user;
@ApiModelProperty(value = "App用户")
private final APPUser appUser;
@ApiModelProperty(value = "数据权限") @ApiModelProperty(value = "数据权限")
private final List<Long> dataScopes; private final List<Long> dataScopes;
@ApiModelProperty(value = "角色") @ApiModelProperty(value = "角色")
private final List<AuthorityDto> authorities; private final List<AuthorityDto> authorities;
private String username;
private String password;
private boolean enabled = true;
public Set<String> getRoles() { public Set<String> getRoles() {
if (authorities== null){
return Collections.emptySet();
}
return authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet()); return authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet());
} }
@Override @Override
//@JSONField(serialize = false) @JSONField(serialize = false)
public String getPassword() { public String getPassword() {
if (appUser != null) {
return appUser.getPassword();
}
return user.getPassword(); return user.getPassword();
} }
public JwtUserDto(User user, APPUser appUser, List<Long> dataScopes, List<AuthorityDto> authorities) {
this.user = user;
this.appUser = appUser;
this.dataScopes = dataScopes;
this.authorities = authorities;
if (user != null) {
this.username = user.getUsername();
this.password = user.getPassword();
this.enabled = user.getEnabled();
} else if (appUser != null) {
this.username = appUser.getUsername();
this.password = appUser.getPassword();
this.enabled = appUser.getEnabled();
}
}
@Override @Override
//@JSONField(serialize = false) @JSONField(serialize = false)
public String getUsername() { public String getUsername() {
if (appUser != null) {
return appUser.getUsername();
}
return user.getUsername(); return user.getUsername();
} }
@JSONField(serialize = false)
//@JSONField(serialize = false)
public String appGetUsername() {
return appUser.getUsername();
}
//@JSONField(serialize = false)
@Override @Override
public boolean isAccountNonExpired() { public boolean isAccountNonExpired() {
return true; return true;
} }
//@JSONField(serialize = false) @JSONField(serialize = false)
@Override @Override
public boolean isAccountNonLocked() { public boolean isAccountNonLocked() {
return true; return true;
} }
//@JSONField(serialize = false) @JSONField(serialize = false)
@Override @Override
public boolean isCredentialsNonExpired() { public boolean isCredentialsNonExpired() {
return true; return true;
} }
@Override @Override
//@JSONField(serialize = false) @JSONField(serialize = false)
public boolean isEnabled() { public boolean isEnabled() {
return user.getEnabled();
return enabled;
} }
} }