From bbf5614cfc6fdfd6270a15279d86ef5064d2e24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Sat, 11 May 2024 12:36:52 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E4=BC=98=E5=8C=96=20OAuth?= =?UTF-8?q?=202.0=20=E7=99=BB=E5=BD=95=E8=BF=87=E7=A8=8B=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E8=BE=93=E5=87=BA=EF=BC=8C=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E8=BF=87=E7=A8=8B=E4=B8=AD=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PigxTocDefaultUserDetailsServiceImpl.java | 144 +++++++++--------- .../PigxTocMobileUserDetailsServiceImpl.java | 130 ++++++++-------- 2 files changed, 146 insertions(+), 128 deletions(-) diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocDefaultUserDetailsServiceImpl.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocDefaultUserDetailsServiceImpl.java index 9dac7db85..ec28c7ef5 100755 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocDefaultUserDetailsServiceImpl.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocDefaultUserDetailsServiceImpl.java @@ -36,6 +36,9 @@ import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.security.oauth2.core.OAuth2Error; +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; import java.util.*; @@ -48,88 +51,93 @@ import java.util.*; @RequiredArgsConstructor public class PigxTocDefaultUserDetailsServiceImpl implements PigxUserDetailsService { - private final CacheManager cacheManager; + private final CacheManager cacheManager; - private final Optional remoteAppUserServiceOptional; + private final Optional remoteAppUserServiceOptional; - /** - * 用户密码登录 - * @param username 用户密码登录 - * @return - */ - @Override - @SneakyThrows - public UserDetails loadUserByUsername(String username) { - if (!remoteAppUserServiceOptional.isPresent()) { - throw new UnsupportedOperationException(); - } + /** + * 用户密码登录 + * + * @param username 用户密码登录 + * @return + */ + @Override + @SneakyThrows + public UserDetails loadUserByUsername(String username) { + if (!remoteAppUserServiceOptional.isPresent()) { + throw new UnsupportedOperationException(); + } - Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS_MINI); - if (cache != null && cache.get(username) != null) { - return cache.get(username, PigxUser.class); - } - R info = remoteAppUserServiceOptional.get().info(username, SecurityConstants.FROM_IN); - UserDetails userDetailsAppUser = this.getUserDetailsAppUser(info); - if (cache != null) { - cache.put(username, userDetailsAppUser); - } - return userDetailsAppUser; - } + Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS_MINI); + if (cache != null && cache.get(username) != null) { + return cache.get(username, PigxUser.class); + } - @Override - public UserDetails loadUserByUser(PigxUser pigxUser) { - return pigxUser; - } + R info = remoteAppUserServiceOptional.get().info(username, SecurityConstants.FROM_IN); + UserDetails userDetailsAppUser = this.getUserDetailsAppUser(info); + if (cache != null) { + cache.put(username, userDetailsAppUser); + } + return userDetailsAppUser; + } - UserDetails getUserDetailsAppUser(R result) { - // @formatter:off + @Override + public UserDetails loadUserByUser(PigxUser pigxUser) { + return pigxUser; + } + + UserDetails getUserDetailsAppUser(R result) { + // @formatter:off return RetOps.of(result) + .assertSuccess(r -> new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, "用户信息获取失败",null))) .getData() .map(this::convertUserDetailsAppUser) .orElseThrow(() -> new UsernameNotFoundException("用户不存在")); // @formatter:on - } + } - /** - * UserInfo 转 UserDetails - * @param info - * @return 返回UserDetails对象 - */ - UserDetails convertUserDetailsAppUser(AppUserInfo info) { - Set 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 转 UserDetails + * + * @param info + * @return 返回UserDetails对象 + */ + UserDetails convertUserDetailsAppUser(AppUserInfo info) { + Set 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 authorities = AuthorityUtils - .createAuthorityList(dbAuthsSet.toArray(new String[0])); - AppUser user = info.getAppUser(); - // 构造security用户 + } + Collection authorities = AuthorityUtils + .createAuthorityList(dbAuthsSet.toArray(new String[0])); + AppUser user = info.getAppUser(); + // 构造security用户 - return new PigxUser(user.getUserId(), user.getUsername(), null, user.getPhone(), user.getAvatar(), - user.getNickname(), user.getName(), user.getEmail(), user.getTenantId(), - SecurityConstants.BCRYPT + user.getPassword(), true, true, UserTypeEnum.TOC.getStatus(), true, - !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities); - } + return new PigxUser(user.getUserId(), user.getUsername(), null, user.getPhone(), user.getAvatar(), + user.getNickname(), user.getName(), user.getEmail(), user.getTenantId(), + SecurityConstants.BCRYPT + user.getPassword(), true, true, UserTypeEnum.TOC.getStatus(), true, + !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities); + } - @Override - public int getOrder() { - return 10; - } + @Override + public int getOrder() { + return 10; + } - /** - * 支持所有的 mobile 类型 - * @param clientId 目标客户端 - * @param grantType 授权类型 - * @return true/false - */ - @Override - public boolean support(String clientId, String grantType) { - String header = WebUtils.getRequest().getHeader(SecurityConstants.HEADER_TOC); - return SecurityConstants.HEADER_TOC_YES.equals(header); - } + /** + * 支持所有的 mobile 类型 + * + * @param clientId 目标客户端 + * @param grantType 授权类型 + * @return true/false + */ + @Override + public boolean support(String clientId, String grantType) { + String header = WebUtils.getRequest().getHeader(SecurityConstants.HEADER_TOC); + return SecurityConstants.HEADER_TOC_YES.equals(header); + } } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocMobileUserDetailsServiceImpl.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocMobileUserDetailsServiceImpl.java index 0604448b9..03935ba2a 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocMobileUserDetailsServiceImpl.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocMobileUserDetailsServiceImpl.java @@ -33,6 +33,9 @@ import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; +import org.springframework.security.oauth2.core.OAuth2Error; +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; import java.util.*; @@ -45,74 +48,81 @@ import java.util.*; @RequiredArgsConstructor public class PigxTocMobileUserDetailsServiceImpl implements PigxUserDetailsService { - private final Optional remoteAppUserServiceOptional; + private final Optional remoteAppUserServiceOptional; - /** - * 用户密码登录 - * @param phone 用户密码登录 - * @return - */ - @Override - @SneakyThrows - public UserDetails loadUserByUsername(String phone) { - if (!remoteAppUserServiceOptional.isPresent()) { - throw new UnsupportedOperationException(); - } - R info = remoteAppUserServiceOptional.get().social(phone, SecurityConstants.FROM_IN); - return this.getUserDetailsAppUser(info); - } + /** + * 用户密码登录 + * + * @param phone 用户密码登录 + * @return + */ + @Override + @SneakyThrows + public UserDetails loadUserByUsername(String phone) { + if (!remoteAppUserServiceOptional.isPresent()) { + throw new UnsupportedOperationException(); + } + R info = remoteAppUserServiceOptional.get().social(phone, SecurityConstants.FROM_IN); + return this.getUserDetailsAppUser(info); + } - @Override - public UserDetails loadUserByUser(PigxUser pigxUser) { - return pigxUser; - } + @Override + public UserDetails loadUserByUser(PigxUser pigxUser) { + return pigxUser; + } - UserDetails getUserDetailsAppUser(R result) { - // @formatter:off - return RetOps.of(result).getData().map(this::convertUserDetailsAppUser).orElseThrow(() -> new UsernameNotFoundException("用户不存在")); + UserDetails getUserDetailsAppUser(R result) { + // @formatter:off + return RetOps.of(result) + .assertSuccess(r -> new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, "用户信息获取失败",null))) + .getData() + .map(this::convertUserDetailsAppUser) + .orElseThrow(() -> new UsernameNotFoundException("用户不存在")); // @formatter:on - } + } - /** - * UserInfo 转 UserDetails - * @param info - * @return 返回UserDetails对象 - */ - UserDetails convertUserDetailsAppUser(AppUserInfo info) { - Set 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 转 UserDetails + * + * @param info + * @return 返回UserDetails对象 + */ + UserDetails convertUserDetailsAppUser(AppUserInfo info) { + Set 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 authorities = AuthorityUtils - .createAuthorityList(dbAuthsSet.toArray(new String[0])); - AppUser user = info.getAppUser(); - // 构造security用户 + } + Collection authorities = AuthorityUtils + .createAuthorityList(dbAuthsSet.toArray(new String[0])); + AppUser user = info.getAppUser(); + // 构造security用户 - return new PigxUser(user.getUserId(), user.getUsername(), null, user.getPhone(), user.getAvatar(), - user.getNickname(), user.getName(), user.getEmail(), user.getTenantId(), - SecurityConstants.BCRYPT + user.getPassword(), true, true, UserTypeEnum.TOC.getStatus(), true, - !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities); - } + return new PigxUser(user.getUserId(), user.getUsername(), null, user.getPhone(), user.getAvatar(), + user.getNickname(), user.getName(), user.getEmail(), user.getTenantId(), + SecurityConstants.BCRYPT + user.getPassword(), true, true, UserTypeEnum.TOC.getStatus(), true, + !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities); + } - @Override - public int getOrder() { - return 15; - } + @Override + public int getOrder() { + return 15; + } - /** - * 支持所有的 mobile 类型 - * @param clientId 目标客户端 - * @param grantType 授权类型 - * @return true/false - */ - @Override - public boolean support(String clientId, String grantType) { - String header = WebUtils.getRequest().getHeader(SecurityConstants.HEADER_TOC); - return SecurityConstants.HEADER_TOC_YES.equals(header) && SecurityConstants.GRANT_MOBILE.equals(grantType); - } + /** + * 支持所有的 mobile 类型 + * + * @param clientId 目标客户端 + * @param grantType 授权类型 + * @return true/false + */ + @Override + public boolean support(String clientId, String grantType) { + String header = WebUtils.getRequest().getHeader(SecurityConstants.HEADER_TOC); + return SecurityConstants.HEADER_TOC_YES.equals(header) && SecurityConstants.GRANT_MOBILE.equals(grantType); + } }