登录
This commit is contained in:
@ -27,6 +27,7 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
@ -45,8 +46,8 @@ public class OnlineController {
|
||||
@ApiOperation("查询在线用户")
|
||||
@GetMapping
|
||||
@PreAuthorize("@el.check()")
|
||||
public ResponseEntity<PageResult<OnlineUserDto>> queryOnlineUser(String username, Pageable pageable){
|
||||
return new ResponseEntity<>(onlineUserService.getAll(username, pageable),HttpStatus.OK);
|
||||
public ResponseEntity<PageResult<OnlineUserDto>> queryOnlineUser(String username, Pageable pageable) {
|
||||
return new ResponseEntity<>(onlineUserService.getAll(username, pageable), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("导出数据")
|
||||
@ -67,4 +68,5 @@ public class OnlineController {
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -77,11 +77,11 @@ public class AppTokenProvider implements InitializingBean {
|
||||
Map<String, Object> claims = new HashMap<>(6);
|
||||
// 设置用户ID
|
||||
// claims.put(AUTHORITIES_UID_KEY, user.getAppUser().getId());
|
||||
if (user.getAppUser() != null){
|
||||
claims.put(AUTHORITIES_UID_KEY, user.getAppUser().getId());
|
||||
}else {
|
||||
claims.put(AUTHORITIES_UID_KEY, 0);
|
||||
}
|
||||
// if (user.getAppUser() != null){
|
||||
// claims.put(AUTHORITIES_UID_KEY, user.getAppUser().getId());
|
||||
// }else {
|
||||
// claims.put(AUTHORITIES_UID_KEY, 0);
|
||||
// }
|
||||
// 设置UUID,确保每次Token不一样
|
||||
claims.put(AUTHORITIES_UUID_KEY, IdUtil.simpleUUID());
|
||||
return jwtBuilder
|
||||
|
@ -45,8 +45,6 @@ public class OnlineUserService {
|
||||
private final RedisUtils redisUtils;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 保存在线用户信息
|
||||
* @param jwtUserDto /
|
||||
@ -54,22 +52,14 @@ public class OnlineUserService {
|
||||
* @param request /
|
||||
*/
|
||||
public void save(JwtUserDto jwtUserDto, String token, HttpServletRequest request){
|
||||
// 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 dept = jwtUserDto.getUser().getDept() == null ? null : jwtUserDto.getUser().getDept().getName();
|
||||
String ip = StringUtils.getIp(request);
|
||||
String id = tokenProvider.getId(token);
|
||||
String browser = StringUtils.getBrowser(request);
|
||||
String address = StringUtils.getCityInfo(ip);
|
||||
OnlineUserDto onlineUserDto = null;
|
||||
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) {
|
||||
log.error(e.getMessage(),e);
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ import com.fuyuanshen.utils.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
@ -38,8 +39,10 @@ public class UserCacheManager {
|
||||
@Value("${login.user-cache.idle-time}")
|
||||
private long idleTime;
|
||||
|
||||
|
||||
/**
|
||||
* 返回用户缓存
|
||||
*
|
||||
* @param userName 用户名
|
||||
* @return JwtUserDto
|
||||
*/
|
||||
@ -48,24 +51,15 @@ public class UserCacheManager {
|
||||
userName = StringUtils.lowerCase(userName);
|
||||
if (StringUtils.isNotEmpty(userName)) {
|
||||
// 获取数据
|
||||
try {
|
||||
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 redisUtils.get(LoginProperties.cacheKey + userName, JwtUserDto.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加缓存到Redis
|
||||
* 添加缓存到Redis
|
||||
*
|
||||
* @param userName 用户名
|
||||
*/
|
||||
@Async
|
||||
@ -82,6 +76,7 @@ public class UserCacheManager {
|
||||
/**
|
||||
* 清理用户缓存信息
|
||||
* 用户信息变更时
|
||||
*
|
||||
* @param userName 用户名
|
||||
*/
|
||||
@Async
|
||||
@ -96,6 +91,7 @@ public class UserCacheManager {
|
||||
|
||||
/**
|
||||
* 返回用户缓存
|
||||
*
|
||||
* @param userName 用户名
|
||||
* @return JwtUserDto
|
||||
*/
|
||||
@ -106,7 +102,7 @@ public class UserCacheManager {
|
||||
// 获取数据
|
||||
try {
|
||||
JwtUserDto jwtUserDto = redisUtils.get(LoginProperties.cacheKey_app + userName, JwtUserDto.class);
|
||||
if (jwtUserDto != null){
|
||||
if (jwtUserDto != null) {
|
||||
jwtUserDto.getUsername();
|
||||
}
|
||||
return jwtUserDto;
|
||||
@ -121,11 +117,12 @@ public class UserCacheManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加缓存到Redis
|
||||
* 添加缓存到Redis
|
||||
*
|
||||
* @param userName 用户名
|
||||
*/
|
||||
@Async
|
||||
public void addUserCache(String userName, JwtUserDto user,Integer userType) {
|
||||
public void addUserCache(String userName, JwtUserDto user, Integer userType) {
|
||||
// 转小写
|
||||
userName = StringUtils.lowerCase(userName);
|
||||
if (StringUtils.isNotEmpty(userName)) {
|
||||
@ -138,10 +135,11 @@ public class UserCacheManager {
|
||||
/**
|
||||
* 清理用户缓存信息
|
||||
* 用户信息变更时
|
||||
*
|
||||
* @param userName 用户名
|
||||
*/
|
||||
@Async
|
||||
public void cleanUserCache(String userName,Integer userType) {
|
||||
public void cleanUserCache(String userName, Integer userType) {
|
||||
// 转小写
|
||||
userName = StringUtils.lowerCase(userName);
|
||||
if (StringUtils.isNotEmpty(userName)) {
|
||||
|
@ -59,7 +59,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
// 获取用户的权限
|
||||
List<AuthorityDto> authorities = roleService.buildPermissions(user);
|
||||
// 初始化JwtUserDto
|
||||
jwtUserDto = new JwtUserDto(user, null,dataService.getDeptIds(user), authorities);
|
||||
jwtUserDto = new JwtUserDto(user, dataService.getDeptIds(user), authorities);
|
||||
// 添加缓存数据
|
||||
userCacheManager.addUserCache(username, jwtUserDto);
|
||||
}
|
||||
@ -82,7 +82,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
// 获取用户的权限
|
||||
List<AuthorityDto> authorities = roleService.appBuildPermissions(user);
|
||||
// 初始化JwtUserDto
|
||||
jwtUserDto = new JwtUserDto(null,user, dataService.getDeptIds(user), authorities);
|
||||
// jwtUserDto = new JwtUserDto(null,user, dataService.getDeptIds(user), authorities);
|
||||
// 添加缓存数据
|
||||
userCacheManager.addUserCache(username, jwtUserDto, userType);
|
||||
}
|
||||
|
@ -16,16 +16,13 @@
|
||||
package com.fuyuanshen.modules.security.service.dto;
|
||||
|
||||
import com.alibaba.fastjson2.annotation.JSONField;
|
||||
import com.fuyuanshen.modules.system.domain.app.APPUser;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import com.fuyuanshen.modules.system.domain.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
@ -34,98 +31,56 @@ import java.util.stream.Collectors;
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-23
|
||||
*/
|
||||
@NoArgsConstructor(force = true)
|
||||
@Getter
|
||||
@Setter
|
||||
public class JwtUserDto implements UserDetails, java.io.Serializable {
|
||||
@AllArgsConstructor
|
||||
public class JwtUserDto implements UserDetails {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@ApiModelProperty(value = "用户")
|
||||
private final User user;
|
||||
|
||||
@ApiModelProperty(value = "App用户")
|
||||
private final APPUser appUser;
|
||||
|
||||
@ApiModelProperty(value = "数据权限")
|
||||
private final List<Long> dataScopes;
|
||||
|
||||
@ApiModelProperty(value = "角色")
|
||||
private final List<AuthorityDto> authorities;
|
||||
private String username;
|
||||
private String password;
|
||||
private boolean enabled = true;
|
||||
|
||||
public Set<String> getRoles() {
|
||||
if (authorities== null){
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
//@JSONField(serialize = false)
|
||||
@JSONField(serialize = false)
|
||||
public String getPassword() {
|
||||
if (appUser != null) {
|
||||
return appUser.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
|
||||
//@JSONField(serialize = false)
|
||||
@JSONField(serialize = false)
|
||||
public String getUsername() {
|
||||
if (appUser != null) {
|
||||
return appUser.getUsername();
|
||||
}
|
||||
return user.getUsername();
|
||||
}
|
||||
|
||||
|
||||
//@JSONField(serialize = false)
|
||||
public String appGetUsername() {
|
||||
return appUser.getUsername();
|
||||
}
|
||||
|
||||
//@JSONField(serialize = false)
|
||||
@JSONField(serialize = false)
|
||||
@Override
|
||||
public boolean isAccountNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
//@JSONField(serialize = false)
|
||||
@JSONField(serialize = false)
|
||||
@Override
|
||||
public boolean isAccountNonLocked() {
|
||||
return true;
|
||||
}
|
||||
|
||||
//@JSONField(serialize = false)
|
||||
@JSONField(serialize = false)
|
||||
@Override
|
||||
public boolean isCredentialsNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
//@JSONField(serialize = false)
|
||||
@JSONField(serialize = false)
|
||||
public boolean isEnabled() {
|
||||
|
||||
return enabled;
|
||||
return user.getEnabled();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user