diff --git a/pigx-app-server/pigx-app-server-api/src/main/java/com/pig4cloud/pigx/app/api/feign/RemoteAppUserService.java b/pigx-app-server/pigx-app-server-api/src/main/java/com/pig4cloud/pigx/app/api/feign/RemoteAppUserService.java index 4d4679e32..634d5ca65 100644 --- a/pigx-app-server/pigx-app-server-api/src/main/java/com/pig4cloud/pigx/app/api/feign/RemoteAppUserService.java +++ b/pigx-app-server/pigx-app-server-api/src/main/java/com/pig4cloud/pigx/app/api/feign/RemoteAppUserService.java @@ -47,15 +47,15 @@ public interface RemoteAppUserService { @GetMapping("/appuser/info/{username}") R info(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM) String from); -// /** -// * 通过社交账号或手机号查询用户、角色信息 -// * @param inStr appid@code -// * @param from 调用标志 -// * @return -// */ -// @GetMapping("/social/info/{inStr}") -// R social(@PathVariable("inStr") String inStr, @RequestHeader(SecurityConstants.FROM) String from); -// + /** + * 通过社交账号或手机号查询用户、角色信息 + * @param inStr appid@code + * @param from 调用标志 + * @return + */ + @GetMapping("/appuser/social/info/{inStr}") + R social(@PathVariable("inStr") String inStr, @RequestHeader(SecurityConstants.FROM) String from); + /** * 锁定用户 diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CacheConstants.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CacheConstants.java index 50b2938ce..1f29262b7 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CacheConstants.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CacheConstants.java @@ -30,6 +30,12 @@ public interface CacheConstants { */ String USER_DETAILS = "user_details"; + + /** + * 移动端用户信息缓存 + */ + String USER_DETAILS_MINI = "user_details_mini"; + /** * 角色信息缓存 */ diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java index c03c01a49..5f97fc48e 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java @@ -95,6 +95,12 @@ public interface SecurityConstants { */ String GRANT_MOBILE = "mobile"; + + /** + * 小程序授权 + */ + String CLIENT_MINI = "mini"; + /** * QQ获取token */ diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxAppUserDetailsServiceImpl.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxAppUserDetailsServiceImpl.java index 82b74a8dc..1b0673aeb 100755 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxAppUserDetailsServiceImpl.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxAppUserDetailsServiceImpl.java @@ -64,12 +64,16 @@ public class PigxAppUserDetailsServiceImpl implements PigxUserDetailsService { @Override @SneakyThrows public UserDetails loadUserByUsername(String username) { - Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS); + Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS_MINI); if (cache != null && cache.get(username) != null) { return cache.get(username, PigxUser.class); } R info = remoteAppUserService.info(username, SecurityConstants.FROM_IN); - return this.getUserDetailsAppUser(info); + UserDetails userDetailsAppUser = this.getUserDetailsAppUser(info); + if(cache != null){ + cache.put(username, userDetailsAppUser); + } + return userDetailsAppUser; } @@ -125,7 +129,7 @@ public class PigxAppUserDetailsServiceImpl implements PigxUserDetailsService { */ @Override public boolean support(String clientId, String grantType) { - return clientId.equals("app"); + return SecurityConstants.CLIENT_MINI.equals(clientId); } } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxMobildAppUserDetailsServiceImpl.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxMobildAppUserDetailsServiceImpl.java new file mode 100644 index 000000000..62a162e3e --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxMobildAppUserDetailsServiceImpl.java @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.pig4cloud.pigx.common.security.service; + +import cn.hutool.core.util.ArrayUtil; +import com.pig4cloud.pigx.app.api.dto.AppUserInfo; +import com.pig4cloud.pigx.app.api.entity.AppUser; +import com.pig4cloud.pigx.app.api.feign.RemoteAppUserService; +import com.pig4cloud.pigx.common.core.constant.CacheConstants; +import com.pig4cloud.pigx.common.core.constant.CommonConstants; +import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import com.pig4cloud.pigx.common.core.constant.enums.UserTypeEnum; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.core.util.RetOps; +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; +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; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +/** + * 用户详细信息 + * + * @author lengleng hccake + */ +@Slf4j +@RequiredArgsConstructor +public class PigxMobildAppUserDetailsServiceImpl implements PigxUserDetailsService { + + private final UserDetailsService pigxAppUserDetailsServiceImpl; + private final CacheManager cacheManager; + private final RemoteAppUserService remoteAppUserService; + + /** + * 用户密码登录 + * @param username 用户密码登录 + * @return + */ + @Override + @SneakyThrows + public UserDetails loadUserByUsername(String phone) { + Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS_MINI); + if (cache != null && cache.get(phone) != null) { + return cache.get(phone, PigxUser.class); + } + R info = remoteAppUserService.social(phone, SecurityConstants.FROM_IN); + UserDetails userDetailsAppUser = this.getUserDetailsAppUser(info); + if(cache != null){ + cache.put(phone, userDetailsAppUser); + } + return userDetailsAppUser; + } + + + @Override + public UserDetails loadUserByUser(PigxUser pigxUser) { + return pigxAppUserDetailsServiceImpl.loadUserByUsername(pigxUser.getUsername()); + } + + UserDetails getUserDetailsAppUser(R result) { + // @formatter:off + return RetOps.of(result) + .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())); + + } + 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); + } + + @Override + public int getOrder() { + return 10; + } + + /** + * 支持所有的 mobile 类型 + * @param clientId 目标客户端 + * @param grantType 授权类型 + * @return true/false + */ + @Override + public boolean support(String clientId, String grantType) { + return SecurityConstants.CLIENT_MINI.equals(clientId) && SecurityConstants.GRANT_MOBILE.equals(grantType); + } + +} diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxMobileUserDetailServiceImpl.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxMobileUserDetailServiceImpl.java index 747386cd4..0bf03e266 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxMobileUserDetailServiceImpl.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxMobileUserDetailServiceImpl.java @@ -13,10 +13,14 @@ import org.springframework.cache.CacheManager; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; +/** + * @author aeizzz + */ @Slf4j @RequiredArgsConstructor public class PigxMobileUserDetailServiceImpl implements PigxUserDetailsService { + private final UserDetailsService pigxDefaultUserDetailsServiceImpl; private final RemoteUserService remoteUserService; private final CacheManager cacheManager; @@ -53,6 +57,6 @@ public class PigxMobileUserDetailServiceImpl implements PigxUserDetailsService { */ @Override public boolean support(String clientId, String grantType) { - return SecurityConstants.GRANT_MOBILE.equals(grantType) && !clientId.equals("app"); + return SecurityConstants.GRANT_MOBILE.equals(grantType) && !SecurityConstants.CLIENT_MINI.equals(clientId); } }