PigxUserDetailsServiceImpl.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. *
  3. * Copyright (c) 2018-2025, lengleng All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * Neither the name of the pig4cloud.com developer nor the names of its
  14. * contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. * Author: lengleng (wangiegie@gmail.com)
  17. *
  18. */
  19. package com.pig4cloud.pigx.auth.service;
  20. import cn.hutool.core.util.ArrayUtil;
  21. import cn.hutool.core.util.StrUtil;
  22. import com.pig4cloud.pigx.admin.api.dto.UserInfo;
  23. import com.pig4cloud.pigx.admin.api.entity.SysUser;
  24. import com.pig4cloud.pigx.admin.api.feign.RemoteUserService;
  25. import com.pig4cloud.pigx.common.core.constant.CommonConstant;
  26. import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
  27. import com.pig4cloud.pigx.common.core.constant.enums.EnumLoginType;
  28. import com.pig4cloud.pigx.common.core.util.R;
  29. import com.pig4cloud.pigx.common.security.util.PigxUserDetailsService;
  30. import lombok.AllArgsConstructor;
  31. import lombok.extern.slf4j.Slf4j;
  32. import org.springframework.security.core.GrantedAuthority;
  33. import org.springframework.security.core.authority.AuthorityUtils;
  34. import org.springframework.security.core.userdetails.User;
  35. import org.springframework.security.core.userdetails.UserDetails;
  36. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  37. import org.springframework.stereotype.Service;
  38. import java.util.Arrays;
  39. import java.util.Collection;
  40. import java.util.HashSet;
  41. import java.util.Set;
  42. /**
  43. * 用户详细信息
  44. *
  45. * @author lengleng
  46. */
  47. @Slf4j
  48. @Service
  49. @AllArgsConstructor
  50. public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
  51. private final RemoteUserService remoteUserService;
  52. /**
  53. * 用户密码登录
  54. *
  55. * @param username 用户名
  56. * @return
  57. * @throws UsernameNotFoundException
  58. */
  59. @Override
  60. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  61. R<UserInfo> result = remoteUserService.info(username, SecurityConstants.FROM_IN);
  62. return getUserDetails(result);
  63. }
  64. /**
  65. * 根据社交登录code 登录
  66. *
  67. * @param inStr TYPE@CODE
  68. * @return UserDetails
  69. * @throws UsernameNotFoundException
  70. */
  71. @Override
  72. public UserDetails loadUserBySocial(String inStr) throws UsernameNotFoundException {
  73. return getUserDetails(remoteUserService.social(inStr));
  74. }
  75. /**
  76. * 构建userdetails
  77. *
  78. * @param result 用户信息
  79. * @return
  80. */
  81. private UserDetails getUserDetails(R<UserInfo> result) {
  82. if (result == null || result.getData() == null) {
  83. throw new UsernameNotFoundException("用户不存在");
  84. }
  85. UserInfo info = result.getData();
  86. Set<String> dbAuthsSet = new HashSet<>();
  87. if (ArrayUtil.isNotEmpty(info.getRoles())) {
  88. // 获取角色
  89. Arrays.stream(info.getRoles()).forEach(role -> dbAuthsSet.add(SecurityConstants.ROLE + role));
  90. // 获取资源
  91. dbAuthsSet.addAll(Arrays.asList(info.getPermissions()));
  92. }
  93. Collection<? extends GrantedAuthority> authorities
  94. = AuthorityUtils.createAuthorityList(dbAuthsSet.toArray(new String[0]));
  95. SysUser user = info.getSysUser();
  96. boolean enabled = StrUtil.equals(user.getDelFlag(), CommonConstant.STATUS_NORMAL);
  97. // 构造security用户
  98. return new User(info.getSysUser().getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled,
  99. true, true, true, authorities);
  100. }
  101. }