|
@@ -1,4 +1,3 @@
|
|
|
-
|
|
|
package com.qunzhixinxi.hnqz.auth.service;
|
|
|
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
@@ -38,73 +37,88 @@ import java.util.Set;
|
|
|
@AllArgsConstructor
|
|
|
public class HnqzUserDetailsServiceImpl implements HnqzUserDetailsService {
|
|
|
|
|
|
- private final RemoteUserService remoteUserService;
|
|
|
+ private final RemoteUserService remoteUserService;
|
|
|
|
|
|
- private final CacheManager cacheManager;
|
|
|
+ private final CacheManager cacheManager;
|
|
|
|
|
|
- /**
|
|
|
- * 用户密码登录
|
|
|
- *
|
|
|
- * @param username 用户名
|
|
|
- * @return
|
|
|
- * @throws UsernameNotFoundException
|
|
|
- */
|
|
|
- @Override
|
|
|
- @SneakyThrows
|
|
|
- public UserDetails loadUserByUsername(String username) {
|
|
|
- String u1 = username.endsWith("@mp") ? username.split("@")[0] : username;
|
|
|
+ /**
|
|
|
+ * 用户密码登录
|
|
|
+ *
|
|
|
+ * @param username 用户名
|
|
|
+ * @return
|
|
|
+ * @throws UsernameNotFoundException
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @SneakyThrows
|
|
|
+ public UserDetails loadUserByUsername(String username) {
|
|
|
+ String u1 = username.endsWith("@mp") ? username.split("@")[0] : username;
|
|
|
|
|
|
- Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
|
|
|
- if (cache != null && cache.get(u1) != null) {
|
|
|
- return (HnqzUser) cache.get(u1).get();
|
|
|
- }
|
|
|
+ Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
|
|
|
+ if (cache != null && cache.get(u1) != null) {
|
|
|
+ return (HnqzUser) cache.get(u1).get();
|
|
|
+ }
|
|
|
|
|
|
- R<UserInfo> result = remoteUserService.info(username, SecurityConstants.FROM_IN);
|
|
|
- UserDetails userDetails = getUserDetails(result);
|
|
|
- cache.put(username, userDetails);
|
|
|
- return userDetails;
|
|
|
- }
|
|
|
+ R<UserInfo> result = remoteUserService.info(username, SecurityConstants.FROM_IN);
|
|
|
+ UserDetails userDetails = getUserDetails(result);
|
|
|
+ cache.put(username, userDetails);
|
|
|
+ return userDetails;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 根据社交登录code 登录
|
|
|
- * @param inStr TYPE@CODE
|
|
|
- * @return UserDetails
|
|
|
- * @throws UsernameNotFoundException
|
|
|
- */
|
|
|
- @Override
|
|
|
- @SneakyThrows
|
|
|
- public UserDetails loadUserBySocial(String inStr) {
|
|
|
- return getUserDetails(remoteUserService.social(inStr, SecurityConstants.FROM_IN));
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 根据社交登录code 登录
|
|
|
+ *
|
|
|
+ * @param inStr TYPE@CODE
|
|
|
+ * @return UserDetails
|
|
|
+ * @throws UsernameNotFoundException
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @SneakyThrows
|
|
|
+ public UserDetails loadUserBySocial(String inStr) {
|
|
|
+ return getUserDetails(remoteUserService.social(inStr, SecurityConstants.FROM_IN));
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 构建userdetails
|
|
|
- * @param result 用户信息
|
|
|
- * @return
|
|
|
- */
|
|
|
- private UserDetails getUserDetails(R<UserInfo> result) {
|
|
|
- if (result == null || result.getData() == null) {
|
|
|
- throw new UsernameNotFoundException("用户不存在");
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 构建userdetails
|
|
|
+ *
|
|
|
+ * @param result 用户信息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private UserDetails getUserDetails(R<UserInfo> result) {
|
|
|
+ if (result == null || result.getData() == null) {
|
|
|
+ throw new UsernameNotFoundException("用户不存在");
|
|
|
+ }
|
|
|
|
|
|
- UserInfo info = result.getData();
|
|
|
- Set<String> dbAuthsSet = new HashSet<>();
|
|
|
- if (ArrayUtil.isNotEmpty(info.getRoles())) {
|
|
|
- // 获取角色
|
|
|
- Arrays.stream(info.getRoles()).forEach(roleId -> dbAuthsSet.add(SecurityConstants.ROLE + roleId));
|
|
|
- // 获取资源
|
|
|
- dbAuthsSet.addAll(Arrays.asList(info.getPermissions()));
|
|
|
+ UserInfo info = result.getData();
|
|
|
+ Set<String> dbAuthsSet = new HashSet<>();
|
|
|
+ if (ArrayUtil.isNotEmpty(info.getRoles())) {
|
|
|
+ // 获取角色
|
|
|
+ Arrays.stream(info.getRoles()).forEach(roleId -> dbAuthsSet.add(SecurityConstants.ROLE + roleId));
|
|
|
+ // 获取资源
|
|
|
+ dbAuthsSet.addAll(Arrays.asList(info.getPermissions()));
|
|
|
|
|
|
- }
|
|
|
- Collection<? extends GrantedAuthority> authorities = AuthorityUtils
|
|
|
- .createAuthorityList(dbAuthsSet.toArray(new String[0]));
|
|
|
- SysUser user = info.getSysUser();
|
|
|
- boolean enabled = StrUtil.equals(user.getLockFlag(), CommonConstants.STATUS_NORMAL);
|
|
|
- // 构造security用户
|
|
|
+ }
|
|
|
+ Collection<? extends GrantedAuthority> authorities = AuthorityUtils
|
|
|
+ .createAuthorityList(dbAuthsSet.toArray(new String[0]));
|
|
|
+ SysUser user = info.getSysUser();
|
|
|
+ boolean enabled = StrUtil.equals(user.getLockFlag(), CommonConstants.STATUS_NORMAL);
|
|
|
+ // 构造security用户
|
|
|
|
|
|
- return new HnqzUser(user.getUserId(), user.getDeptId(), user.getPhone(), user.getAvatar(), user.getTenantId(),
|
|
|
- user.getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled, true, true,
|
|
|
- !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities,user.getPlatId(),user.getDrugEntId(),info.getRoles());
|
|
|
- }
|
|
|
+ return new HnqzUser(user.getUserId(),
|
|
|
+ user.getDeptId(),
|
|
|
+ user.getPhone(),
|
|
|
+ user.getAvatar(),
|
|
|
+ user.getTenantId(),
|
|
|
+ user.getUsername(),
|
|
|
+ SecurityConstants.BCRYPT + user.getPassword(),
|
|
|
+ enabled,
|
|
|
+ true,
|
|
|
+ true,
|
|
|
+ !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()),
|
|
|
+ authorities,
|
|
|
+ user.getPlatId(),
|
|
|
+ user.getDrugEntId(),
|
|
|
+ info.getRoles(),
|
|
|
+ user.getRealname());
|
|
|
+ }
|
|
|
|
|
|
}
|