mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 12:39:05 +08:00
⚡ 优化性能。 userdetails 缓存处理
This commit is contained in:
1 parent
10cdaa9435
commit
4151ec17e3
5 files changed
+27
-23
No files matched your search
+13
-3
@@ -27,6 +27,8 @@ import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
@@ -48,6 +50,7 @@ import java.util.Set;
|
||||
@AllArgsConstructor
|
||||
public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
private final RemoteUserService remoteUserService;
|
||||
private final CacheManager cacheManager;
|
||||
|
||||
/**
|
||||
* 用户密码登录
|
||||
@@ -58,8 +61,15 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
*/
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
Cache cache = cacheManager.getCache("user_details");
|
||||
if (cache != null && cache.get(username) != null) {
|
||||
return (PigxUser) cache.get(username).get();
|
||||
}
|
||||
|
||||
R<UserInfo> result = remoteUserService.info(username, SecurityConstants.FROM_IN);
|
||||
return getUserDetails(result);
|
||||
UserDetails userDetails = getUserDetails(result);
|
||||
cache.put(username, userDetails);
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
|
||||
@@ -96,12 +106,12 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
|
||||
}
|
||||
Collection<? extends GrantedAuthority> authorities
|
||||
= AuthorityUtils.createAuthorityList(dbAuthsSet.toArray(new String[0]));
|
||||
= AuthorityUtils.createAuthorityList(dbAuthsSet.toArray(new String[0]));
|
||||
SysUser user = info.getSysUser();
|
||||
boolean enabled = StrUtil.equals(user.getDelFlag(), CommonConstants.STATUS_NORMAL);
|
||||
// 构造security用户
|
||||
|
||||
return new PigxUser(user.getUserId(), user.getDeptId(), user.getTenantId(), user.getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled,
|
||||
true, true, !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities);
|
||||
true, true, !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities);
|
||||
}
|
||||
}
|
||||
+6
-6
@@ -40,7 +40,7 @@ import javax.validation.Valid;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2017/10/28
|
||||
* @date 2018/12/16
|
||||
*/
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@@ -57,12 +57,12 @@ public class UserController {
|
||||
@GetMapping(value = {"/info"})
|
||||
public R info() {
|
||||
String username = SecurityUtils.getUser().getUsername();
|
||||
SysUser sysUser = userService.getOne(Wrappers.<SysUser>query()
|
||||
.lambda().eq(SysUser::getUsername, username));
|
||||
if (sysUser == null) {
|
||||
SysUser user = userService.getOne(Wrappers.<SysUser>query()
|
||||
.lambda().eq(SysUser::getUsername, username));
|
||||
if (user == null) {
|
||||
return new R<>(Boolean.FALSE, "获取当前用户信息失败");
|
||||
}
|
||||
return new R<>(userService.findUserInfo(sysUser));
|
||||
return new R<>(userService.findUserInfo(user));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +74,7 @@ public class UserController {
|
||||
@GetMapping("/info/{username}")
|
||||
public R info(@PathVariable String username) {
|
||||
SysUser user = userService.getOne(Wrappers.<SysUser>query()
|
||||
.lambda().eq(SysUser::getUsername, username));
|
||||
.lambda().eq(SysUser::getUsername, username));
|
||||
if (user == null) {
|
||||
return new R<>(Boolean.FALSE, String.format("用户信息为空 %s", username));
|
||||
}
|
||||
|
||||
+5
-5
@@ -55,14 +55,14 @@ public class SmsLoginHandler extends AbstractLoginHandler {
|
||||
*/
|
||||
@Override
|
||||
public UserInfo info(String identify) {
|
||||
SysUser sysUser = sysUserService
|
||||
.getOne(Wrappers.<SysUser>query()
|
||||
.lambda().eq(SysUser::getPhone, identify));
|
||||
SysUser user = sysUserService
|
||||
.getOne(Wrappers.<SysUser>query()
|
||||
.lambda().eq(SysUser::getPhone, identify));
|
||||
|
||||
if (sysUser == null) {
|
||||
if (user == null) {
|
||||
log.info("手机号未注册:{}", identify);
|
||||
return null;
|
||||
}
|
||||
return sysUserService.findUserInfo(sysUser);
|
||||
return sysUserService.findUserInfo(user);
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -75,14 +75,14 @@ public class WeChatLoginHandler extends AbstractLoginHandler {
|
||||
*/
|
||||
@Override
|
||||
public UserInfo info(String openId) {
|
||||
SysUser sysUser = sysUserService
|
||||
SysUser user = sysUserService
|
||||
.getOne(Wrappers.<SysUser>query()
|
||||
.lambda().eq(SysUser::getWxOpenid, openId));
|
||||
|
||||
if (sysUser == null) {
|
||||
if (user == null) {
|
||||
log.info("微信未绑定:{}", openId);
|
||||
return null;
|
||||
}
|
||||
return sysUserService.findUserInfo(sysUser);
|
||||
return sysUserService.findUserInfo(user);
|
||||
}
|
||||
}
|
||||
-6
@@ -41,7 +41,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -99,12 +98,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@Cacheable(value = "user_details", key = "#sysUser.username", unless = "#result == null")
|
||||
public UserInfo findUserInfo(SysUser sysUser) {
|
||||
if (sysUser == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
UserInfo userInfo = new UserInfo();
|
||||
userInfo.setSysUser(sysUser);
|
||||
//设置角色列表 (ID)
|
||||
|
||||
Reference in new issue
Block a user