From 36b2ce817ee24b3327e0a48085fc2f5ccd4d2df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Thu, 16 Aug 2018 20:27:24 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E7=89=B9=E6=80=A7=E3=80=82=E5=AE=8C=E5=96=84=E5=BE=8C=E5=8F=B0?= =?UTF-8?q?=E7=B6=81=E5=AE=9A=E7=9A=84=E4=BB=A3=E7=A2=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pigx/auth/PigxAuthApplication.java | 5 -- .../service/PigxUserDetailsServiceImpl.java | 2 +- .../core/config/RestTemplateConfig.java | 35 ++++++++++++ .../main/resources/META-INF/spring.factories | 1 + .../main/resources/config/application-dev.yml | 2 +- .../pigx/admin/controller/UserController.java | 12 +++++ .../pigx/admin/service/SysUserService.java | 9 ++++ .../service/impl/SysUserServiceImpl.java | 53 +++++++++++++++---- 8 files changed, 102 insertions(+), 17 deletions(-) create mode 100644 pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/RestTemplateConfig.java 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 a0bbe00da..fe63beea7 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 @@ -38,9 +38,4 @@ 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/service/PigxUserDetailsServiceImpl.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxUserDetailsServiceImpl.java index 5fe59b7ca..80a458c07 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 @@ -94,7 +94,7 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService { String url = String.format(WX_AUTHORIZATION_CODE_URL , wxSocialConfig.getAppid(), wxSocialConfig.getSecret(), code); String result = restTemplate.getForObject(url, String.class); - log.warn("微信响应报文:{}", result); + log.debug("微信响应报文:{}", result); Object obj = JSONUtil.parseObj(result).get("openid"); if (obj != null) { diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/RestTemplateConfig.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/RestTemplateConfig.java new file mode 100644 index 000000000..b4f844ddd --- /dev/null +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/RestTemplateConfig.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.core.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * @author lengleng + * @date 2018/8/16 + * RestTemplate + */ +@Configuration +public class RestTemplateConfig { + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/pigx-common/pigx-common-core/src/main/resources/META-INF/spring.factories b/pigx-common/pigx-common-core/src/main/resources/META-INF/spring.factories index a5296d20b..d7e609cc1 100644 --- a/pigx-common/pigx-common-core/src/main/resources/META-INF/spring.factories +++ b/pigx-common/pigx-common-core/src/main/resources/META-INF/spring.factories @@ -1,4 +1,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.pig4cloud.pigx.common.core.config.JacksonConfig,\ com.pig4cloud.pigx.common.core.config.RedisConfig,\ + com.pig4cloud.pigx.common.core.config.RestTemplateConfig,\ com.pig4cloud.pigx.common.core.util.SpringContextHolder diff --git a/pigx-config/src/main/resources/config/application-dev.yml b/pigx-config/src/main/resources/config/application-dev.yml index de532fa1e..01bf8a6cb 100644 --- a/pigx-config/src/main/resources/config/application-dev.yml +++ b/pigx-config/src/main/resources/config/application-dev.yml @@ -93,7 +93,7 @@ swagger: ## spring security 配置 security: auth: - server: http://pigx-gateway:9999/auth/oauth + server: http://localhost:9999/auth/oauth oauth2: resource: token-info-uri: ${security.auth.server}/check_token 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 e74c4423b..b427af97f 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 @@ -91,6 +91,18 @@ public class UserController { return new R<>(userService.findUserInfo(type, openid)); } + /** + * 绑定社交账号 + * + * @param state 类型 + * @param code code + * @return + */ + @PostMapping("/social/bind") + public R bind(String state, String code) { + return new R<>(userService.bindSocial(state, code)); + } + /** * 通过ID查询当前用户信息 * 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 384ef2fd9..3a325a66b 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 @@ -83,4 +83,13 @@ public interface SysUserService extends IService { * @return 用户信息 */ UserVO selectUserVoById(Integer id); + + /** + * 绑定社交账号 + * + * @param state 类型 + * @param code code + * @return + */ + Boolean bindSocial(String state, String code); } 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 fe17d89a5..c91d20948 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 @@ -22,6 +22,7 @@ package com.pig4cloud.pigx.admin.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.baomidou.mybatisplus.service.impl.ServiceImpl; @@ -40,15 +41,18 @@ 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 com.pig4cloud.pigx.common.security.util.SecurityUtils; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; import java.time.LocalDateTime; import java.util.ArrayList; @@ -62,16 +66,17 @@ import java.util.Set; */ @Slf4j @Service +@AllArgsConstructor public class SysUserServiceImpl extends ServiceImpl implements SysUserService { private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder(); - @Autowired - private SysMenuService sysMenuService; - @Autowired - private SysUserMapper sysUserMapper; - @Autowired - private SysRoleService sysRoleService; - @Autowired - private SysUserRoleService sysUserRoleService; + 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 final SysMenuService sysMenuService; + private final SysUserMapper sysUserMapper; + private final CacheManager cacheManager; + private final RestTemplate restTemplate; + private final SysRoleService sysRoleService; + private final SysUserRoleService sysUserRoleService; /** * 通过用户名查用户的全部信息 @@ -80,7 +85,7 @@ public class SysUserServiceImpl extends ServiceImpl impl * @return */ @Override - @Cacheable(value = "user_details", key = "#type +':'+ #username") + @Cacheable(value = "user_details", key = "#username") public UserInfo findUserInfo(String type, String username) { SysUser condition = new SysUser(); if (EnumLoginType.PWD.getType().equals(type)) { @@ -122,6 +127,34 @@ public class SysUserServiceImpl extends ServiceImpl impl return userInfo; } + /** + * 绑定社交账号 + * + * @param state 类型 + * @param code code + * @return + */ + @Override + public Boolean bindSocial(String state, String code) { + String result = restTemplate.getForObject(WX_AUTHORIZATION_CODE_URL, String.class); + log.debug("微信响应报文:{}", result); + + Object obj = JSONUtil.parseObj(result).get("openid"); + if (obj == null) { + return Boolean.FALSE; + } + + SysUser condition = new SysUser(); + condition.setUsername(SecurityUtils.getUser()); + SysUser sysUser = this.selectOne(new EntityWrapper<>(condition)); + sysUser.setWxOpenid(obj.toString()); + sysUserMapper.updateAllColumnById(sysUser); + + //更新緩存 + cacheManager.getCache("user_details").evict(sysUser.getUsername()); + return Boolean.TRUE; + } + @Override public Page selectWithRolePage(Query query) { Object username = query.getCondition().get("username");