diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/PigxAuthApplication.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/PigxAuthApplication.java index 223a53505..a0bbe00da 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/PigxAuthApplication.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/PigxAuthApplication.java @@ -23,6 +23,8 @@ package com.pig4cloud.pigx.auth; import org.springframework.boot.SpringApplication; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; /** * @author lengleng @@ -36,4 +38,9 @@ public class PigxAuthApplication { public static void main(String[] args) { SpringApplication.run(PigxAuthApplication.class, args); } + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } } diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java index 1c1ffa3f5..8e87f770a 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java @@ -22,6 +22,7 @@ package com.pig4cloud.pigx.auth.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.pig4cloud.pigx.common.security.mobile.MobileLoginSuccessHandler; import com.pig4cloud.pigx.common.security.mobile.MobileSecurityConfigurer; +import com.pig4cloud.pigx.common.security.util.PigxUserDetailsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -31,7 +32,6 @@ import org.springframework.core.annotation.Order; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.oauth2.provider.ClientDetailsService; @@ -52,7 +52,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Autowired private ClientDetailsService clientDetailsService; @Autowired - private UserDetailsService pigxUserDetailsServiceImpl; + private PigxUserDetailsService userDetailsService; @Lazy @Autowired private AuthorizationServerTokenServices defaultAuthorizationServerTokenServices; @@ -88,7 +88,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Bean public MobileSecurityConfigurer mobileSecurityConfigurer() { return new MobileSecurityConfigurer(mobileLoginSuccessHandler() - , pigxUserDetailsServiceImpl); + , userDetailsService); } diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxUserDetailsServiceImpl.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxUserDetailsServiceImpl.java index f709b31aa..7eeffb868 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxUserDetailsServiceImpl.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxUserDetailsServiceImpl.java @@ -21,21 +21,25 @@ package com.pig4cloud.pigx.auth.service; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; import com.pig4cloud.pigx.admin.api.dto.UserInfo; import com.pig4cloud.pigx.admin.api.entity.SysUser; import com.pig4cloud.pigx.admin.api.feign.RemoteUserService; import com.pig4cloud.pigx.common.core.constant.CommonConstant; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import com.pig4cloud.pigx.common.core.constant.enums.EnumLoginType; import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.security.social.WxSocialConfig; +import com.pig4cloud.pigx.common.security.util.PigxUserDetailsService; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; import java.util.Arrays; import java.util.Collection; @@ -50,13 +54,65 @@ import java.util.Set; @Slf4j @Service @AllArgsConstructor -public class PigxUserDetailsServiceImpl implements UserDetailsService { +public class PigxUserDetailsServiceImpl implements PigxUserDetailsService { + private static final String WX_AUTHORIZATION_CODE_URL = "https://api.weixin.qq.com/sns/oauth2/access_token" + + "?appid=%s&secret=%s&code=%s&grant_type=authorization_code"; + private static final String REGEX = "@"; + private final RestTemplate restTemplate; private final RemoteUserService remoteUserService; + private final WxSocialConfig wxSocialConfig; + /** + * 用户密码登录 + * + * @param username 用户名 + * @return + * @throws UsernameNotFoundException + */ @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { R result = remoteUserService.info(username, SecurityConstants.FROM_IN); + return getUserDetails(result); + } + + /** + * 根据社交登录code 登录 + * + * @param inStr TYPE@CODE + * @return UserDetails + * @throws UsernameNotFoundException + */ + @Override + public UserDetails loadUserBySocial(String inStr) throws UsernameNotFoundException { + String[] codeStr = inStr.split(REGEX); + String type = codeStr[0]; + String code = codeStr[1]; + + R userInfo = null; + if (EnumLoginType.WECHAT.getType().equals(type)) { + String url = String.format(WX_AUTHORIZATION_CODE_URL + , wxSocialConfig.getAppid(), wxSocialConfig.getSecret(), code); + String result = restTemplate.getForObject(url, String.class); + log.debug("微信响应报文:{}", result); + + Object obj = JSONUtil.parseObj(result).get("openid"); + if (obj != null) { + userInfo = remoteUserService.social(EnumLoginType.WECHAT.getType(), obj.toString()); + } else { + throw new UsernameNotFoundException("获取用户openid失败"); + } + } + return getUserDetails(userInfo); + } + + /** + * 构建userdetails + * + * @param result 用户信息 + * @return + */ + private UserDetails getUserDetails(R result) { if (result == null || result.getData() == null) { throw new UsernameNotFoundException("用户不存在"); } @@ -75,7 +131,7 @@ public class PigxUserDetailsServiceImpl implements UserDetailsService { SysUser user = info.getSysUser(); boolean enabled = StrUtil.equals(user.getDelFlag(), CommonConstant.STATUS_NORMAL); // 构造security用户 - return new User(username, SecurityConstants.BCRYPT + user.getPassword(), enabled, + return new User(info.getSysUser().getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled, true, true, true, authorities); } } diff --git a/pigx-common/pigx-common-core/pom.xml b/pigx-common/pigx-common-core/pom.xml index 4c33f512b..81c112062 100644 --- a/pigx-common/pigx-common-core/pom.xml +++ b/pigx-common/pigx-common-core/pom.xml @@ -50,7 +50,6 @@ javax.servlet javax.servlet-api - ${servlet-api.version} provided diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/MqQueueConstant.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/MqQueueConstant.java deleted file mode 100644 index 901b497ad..000000000 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/MqQueueConstant.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - * - */ - -package com.pig4cloud.pigx.common.core.constant; - -/** - * @author lengleng - * @date 2018/1/15 - * MQ 消息队列 - */ -public interface MqQueueConstant { - /** - * log rabbit队列名称 - */ - String LOG_QUEUE = "log"; - - /** - * 发送短信验证码队列 - */ - String MOBILE_CODE_QUEUE = "mobile_code_queue"; - - /** - * 短信服务状态队列 - */ - String MOBILE_SERVICE_STATUS_CHANGE = "mobile_service_status_change"; - - /** - * 钉钉服务状态队列 - */ - String DINGTALK_SERVICE_STATUS_CHANGE = "dingtalk_service_status_change"; - - /** - * zipkin 队列 - */ - String ZIPKIN_NAME_QUEUE = "zipkin"; - - /** - * 路由配置状态队列 - */ - String ROUTE_CONFIG_CHANGE = "route_config_change"; -} diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/enums/EnumLoginType.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/enums/EnumLoginType.java new file mode 100644 index 000000000..878cae171 --- /dev/null +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/enums/EnumLoginType.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.core.constant.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author lengleng + * @date 2018/8/15 + * 社交登录类型 + */ +@Getter +@AllArgsConstructor +public enum EnumLoginType { + /** + * 账号密码登录 + */ + PWD("PWD", "账号密码登录"), + + /** + * QQ登录 + */ + QQ("QQ", "QQ登录"), + + /** + * 微信登录 + */ + WECHAT("WX", "微信登录"); + + /** + * 类型 + */ + private final String type; + /** + * 描述 + */ + private final String description; +} diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/enums/EnumSmsChannel.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/enums/EnumSmsChannel.java deleted file mode 100644 index a77c77b4a..000000000 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/enums/EnumSmsChannel.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - * - */ - -package com.pig4cloud.pigx.common.core.constant.enums; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -/** - * @author lengleng - * @date 2018/1/16 - * 短信通道枚举 - */ -@Getter -@AllArgsConstructor -public enum EnumSmsChannel { - /** - * 阿里大鱼短信通道 - */ - ALIYUN("ALIYUN_SMS", "阿里大鱼"); - /** - * 通道名称 - */ - private final String name; - /** - * 通道描述 - */ - private final String description; -} diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/enums/EnumSmsChannelTemplate.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/enums/EnumSmsChannelTemplate.java deleted file mode 100644 index cd767d9ed..000000000 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/enums/EnumSmsChannelTemplate.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - * - */ - -package com.pig4cloud.pigx.common.core.constant.enums; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -/** - * @author LiXunHuan - * @date 2018/1/16 - * 短信通道模板 - */ -@Getter -@AllArgsConstructor -public enum EnumSmsChannelTemplate { - /** - * 登录验证 - */ - LOGIN_NAME_LOGIN("loginCodeChannel", "登录验证"), - /** - * 服务异常提醒 - */ - SERVICE_STATUS_CHANGE("serviceStatusChange", "Pig4Cloud"); - - /** - * 模板名称 - */ - private final String template; - /** - * 模板签名 - */ - private final String signName; -} diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileAuthenticationProvider.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileAuthenticationProvider.java index 03b01012f..c36ac85b6 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileAuthenticationProvider.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileAuthenticationProvider.java @@ -17,28 +17,33 @@ package com.pig4cloud.pigx.common.security.mobile; +import com.pig4cloud.pigx.common.security.util.PigxUserDetailsService; import lombok.Getter; import lombok.Setter; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; /** * @author lengleng * @date 2018/8/5 - * 手机号登录校验逻辑 + * 手机登录校验逻辑 + * 验证码登录、社交登录 */ public class MobileAuthenticationProvider implements AuthenticationProvider { @Getter @Setter - private UserDetailsService userDetailsService; + private PigxUserDetailsService userDetailsService; @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { MobileAuthenticationToken mobileAuthenticationToken = (MobileAuthenticationToken) authentication; - UserDetails userDetails = userDetailsService.loadUserByUsername("admin"); + + String principal = mobileAuthenticationToken.getPrincipal().toString(); + UserDetails userDetails = userDetailsService.loadUserBySocial(principal); + + MobileAuthenticationToken authenticationToken = new MobileAuthenticationToken(userDetails, userDetails.getAuthorities()); authenticationToken.setDetails(mobileAuthenticationToken.getDetails()); return authenticationToken; diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileSecurityConfigurer.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileSecurityConfigurer.java index d0ea01f69..8140b4893 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileSecurityConfigurer.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/mobile/MobileSecurityConfigurer.java @@ -17,11 +17,11 @@ package com.pig4cloud.pigx.common.security.mobile; +import com.pig4cloud.pigx.common.security.util.PigxUserDetailsService; import lombok.AllArgsConstructor; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.SecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.web.DefaultSecurityFilterChain; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @@ -36,7 +36,7 @@ import org.springframework.stereotype.Component; @AllArgsConstructor public class MobileSecurityConfigurer extends SecurityConfigurerAdapter { private AuthenticationSuccessHandler mobileLoginSuccessHandler; - private UserDetailsService userDetailsService; + private PigxUserDetailsService userDetailsService; @Override public void configure(HttpSecurity http) { diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/social/QqSocialConfig.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/social/QqSocialConfig.java new file mode 100644 index 000000000..25f48c835 --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/social/QqSocialConfig.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.security.social; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * @author lengleng + * @date 2018/8/16 + * qq登录配置信息 + */ +@Data +@Configuration +@ConfigurationProperties(prefix = "social.qq") +public class QqSocialConfig { + private String appid; + private String secret; +} diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/social/WxSocialConfig.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/social/WxSocialConfig.java new file mode 100644 index 000000000..988ecf7fb --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/social/WxSocialConfig.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.security.social; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * @author lengleng + * @date 2018/8/16 + * 微信登录配置 + */ +@Data +@Configuration +@ConfigurationProperties(prefix = "social.wx") +public class WxSocialConfig { + private String appid; + private String secret; +} diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/PigxUserDetailsService.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/PigxUserDetailsService.java new file mode 100644 index 000000000..7e94476b3 --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/util/PigxUserDetailsService.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */package com.pig4cloud.pigx.common.security.util; + +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +/** + * @author lengleng + * @date 2018/8/15 + */ +public interface PigxUserDetailsService extends UserDetailsService { + + /** + * 根据社交登录code 登录 + * + * @param code TYPE@CODE + * @return UserDetails + * @throws UsernameNotFoundException + */ + UserDetails loadUserBySocial(String code) throws UsernameNotFoundException; +} diff --git a/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories b/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories index c93a4a429..4e205b712 100644 --- a/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories +++ b/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories @@ -2,4 +2,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.pig4cloud.pigx.common.security.component.PermissionService,\ com.pig4cloud.pigx.common.security.component.PigAccessDeniedHandler,\ com.pig4cloud.pigx.common.security.component.ResourceAuthExceptionEntryPoint,\ - com.pig4cloud.pigx.common.security.feign.PigxFeignClientConfiguration + com.pig4cloud.pigx.common.security.feign.PigxFeignClientConfiguration,\ + com.pig4cloud.pigx.common.security.social.QqSocialConfig,\ + com.pig4cloud.pigx.common.security.social.WxSocialConfig diff --git a/pigx-common/pigx-common-swagger/pom.xml b/pigx-common/pigx-common-swagger/pom.xml index 1b7e63a15..89fdff780 100644 --- a/pigx-common/pigx-common-swagger/pom.xml +++ b/pigx-common/pigx-common-swagger/pom.xml @@ -45,7 +45,6 @@ javax.servlet javax.servlet-api - ${servlet-api.version} provided diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysUser.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysUser.java index 073bff67b..d5739ddc8 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysUser.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysUser.java @@ -87,4 +87,14 @@ public class SysUser implements Serializable { */ private Integer deptId; + /** + * 微信openid + */ + private String wxOpenid; + + /** + * QQ openid + */ + private String qqOpenid; + } diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/RemoteUserService.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/RemoteUserService.java index 0682f6775..4fa0c51d4 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/RemoteUserService.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/RemoteUserService.java @@ -44,4 +44,15 @@ public interface RemoteUserService { @GetMapping("/user/info/{username}") R info(@PathVariable("username") String username , @RequestHeader("from") String from); + + /** + * 通过社交账号查询用户、角色信息 + * + * @param type 社交账号类型 (QQ/WX) + * @param openid 账号 + * @return + */ + @GetMapping("/user/social/{type}/{openid}") + R social(@PathVariable("type") String type + , @PathVariable("openid") String openid); } diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/fallback/RemoteUserServiceFallbackImpl.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/fallback/RemoteUserServiceFallbackImpl.java index 900b354d0..49039463c 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/fallback/RemoteUserServiceFallbackImpl.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/fallback/RemoteUserServiceFallbackImpl.java @@ -44,4 +44,18 @@ public class RemoteUserServiceFallbackImpl implements RemoteUserService { log.error("feign 查询用户信息失败:{}", username); return null; } + + /** + * 通过社交账号查询用户、角色信息 + * + * @param type 社交账号类型 (QQ/WX) + * @param openid 账号 + * @return + */ + @Override + public R social(String type, String openid) { + log.error("feign 查询用户信息失败:{}", openid); + return null; + } + } diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java index 816c9aeef..6c6ff9745 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java @@ -50,6 +50,17 @@ public class UserVO implements Serializable { * 随机盐 */ private String salt; + + /** + * 微信openid + */ + private String wxOpenid; + + /** + * QQ openid + */ + private String qqOpenid; + /** * 创建时间 */ diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/ResourceServerConfigurer.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/ResourceServerConfigurer.java index e8746b16c..8668ed0a7 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/ResourceServerConfigurer.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/config/ResourceServerConfigurer.java @@ -44,7 +44,11 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() - .antMatchers("/actuator/**", "/user/info/*", "/log/**", "/v2/api-docs").permitAll() + .antMatchers("/actuator/**" + , "/user/info/*" + , "/user/social/**" + , "/log/**" + , "/v2/api-docs").permitAll() .anyRequest().authenticated() .and().csrf().disable(); } diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/UserController.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/UserController.java index c95cb7afc..e74c4423b 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/UserController.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/UserController.java @@ -21,7 +21,6 @@ package com.pig4cloud.pigx.admin.controller; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.plugins.Page; -import com.pig4cloud.pigx.common.core.util.Query; import com.pig4cloud.pigx.admin.api.dto.UserDTO; import com.pig4cloud.pigx.admin.api.dto.UserInfo; import com.pig4cloud.pigx.admin.api.entity.SysUser; @@ -30,6 +29,8 @@ import com.pig4cloud.pigx.admin.api.vo.UserVO; import com.pig4cloud.pigx.admin.service.SysUserService; import com.pig4cloud.pigx.common.core.constant.CommonConstant; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import com.pig4cloud.pigx.common.core.constant.enums.EnumLoginType; +import com.pig4cloud.pigx.common.core.util.Query; import com.pig4cloud.pigx.common.core.util.R; import com.pig4cloud.pigx.common.log.annotation.SysLog; import com.pig4cloud.pigx.common.security.util.SecurityUtils; @@ -75,7 +76,19 @@ public class UserController { username = SecurityUtils.getUser(); } - return new R<>(userService.findUserInfo(username)); + return new R<>(userService.findUserInfo(EnumLoginType.PWD.getType(), username)); + } + + /** + * 通过社交账号查询用户、角色信息 + * + * @param type 社交账号类型 (QQ/WX) + * @param openid 账号 + * @return + */ + @GetMapping("/social/{type}/{openid}") + public R social(@PathVariable String type, @PathVariable String openid) { + return new R<>(userService.findUserInfo(type, openid)); } /** diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysUserService.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysUserService.java index 4bf7c1d48..384ef2fd9 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysUserService.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysUserService.java @@ -21,11 +21,11 @@ package com.pig4cloud.pigx.admin.service; import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.service.IService; -import com.pig4cloud.pigx.common.core.util.Query; import com.pig4cloud.pigx.admin.api.dto.UserDTO; import com.pig4cloud.pigx.admin.api.dto.UserInfo; import com.pig4cloud.pigx.admin.api.entity.SysUser; import com.pig4cloud.pigx.admin.api.vo.UserVO; +import com.pig4cloud.pigx.common.core.util.Query; import com.pig4cloud.pigx.common.core.util.R; /** @@ -36,10 +36,11 @@ public interface SysUserService extends IService { /** * 查询用户信息 * + * @param type 类型 * @param username 用户名 * @return userInfo */ - UserInfo findUserInfo(String username); + UserInfo findUserInfo(String type, String username); /** * 分页查询用户信息(含有角色信息) diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java index 053bfcb45..fe17d89a5 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java @@ -25,7 +25,6 @@ import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.service.impl.ServiceImpl; -import com.pig4cloud.pigx.common.core.util.Query; import com.pig4cloud.pigx.admin.api.dto.UserDTO; import com.pig4cloud.pigx.admin.api.dto.UserInfo; import com.pig4cloud.pigx.admin.api.entity.SysRole; @@ -38,6 +37,8 @@ import com.pig4cloud.pigx.admin.service.SysMenuService; import com.pig4cloud.pigx.admin.service.SysRoleService; import com.pig4cloud.pigx.admin.service.SysUserRoleService; import com.pig4cloud.pigx.admin.service.SysUserService; +import com.pig4cloud.pigx.common.core.constant.enums.EnumLoginType; +import com.pig4cloud.pigx.common.core.util.Query; import com.pig4cloud.pigx.common.core.util.R; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; @@ -79,10 +80,16 @@ public class SysUserServiceImpl extends ServiceImpl impl * @return */ @Override - @Cacheable(value = "user_details", key = "#username") - public UserInfo findUserInfo(String username) { + @Cacheable(value = "user_details", key = "#type +':'+ #username") + public UserInfo findUserInfo(String type, String username) { SysUser condition = new SysUser(); - condition.setUsername(username); + if (EnumLoginType.PWD.getType().equals(type)) { + condition.setUsername(username); + } else if (EnumLoginType.WECHAT.getType().equals(type)) { + condition.setWxOpenid(username); + } else { + condition.setQqOpenid(username); + } SysUser sysUser = this.selectOne(new EntityWrapper<>(condition)); if (sysUser == null) { return null; diff --git a/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysUserMapper.xml b/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysUserMapper.xml index c5e43b771..62d743481 100644 --- a/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysUserMapper.xml +++ b/pigx-upms/pigx-upms-biz/src/main/resources/mapper/SysUserMapper.xml @@ -29,6 +29,8 @@ + + @@ -42,6 +44,8 @@ + + @@ -65,6 +69,8 @@ `user`.salt, `user`.phone, `user`.avatar, + `user`.wx_openid, + `user`.qq_openid, `user`.dept_id, `user`.create_time AS ucreate_time, `user`.update_time AS uupdate_time, @@ -95,6 +101,8 @@ `user`.salt, `user`.phone, `user`.avatar, + `user`.wx_openid, + `user`.qq_openid, `user`.create_time AS ucreate_time, `user`.update_time AS uupdate_time, `user`.del_flag AS udel_flag, @@ -123,6 +131,8 @@ `user`.salt, `user`.phone, `user`.avatar, + `user`.wx_openid, + `user`.qq_openid, `user`.create_time AS ucreate_time, `user`.update_time AS uupdate_time, `user`.del_flag AS udel_flag, diff --git a/pom.xml b/pom.xml index 3d132f08d..e8e619bea 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ 1.8 1.8 2.0.1 - 4.1.4 + 4.1.7 2.3 0.0.9 2.9.2 @@ -89,6 +89,7 @@ org.projectlombok lombok + provided