wang-hao-jie
2021-10-19 e48043a2df9ca0c73fe18298bab3c4d42ca5c0c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package cn.exrick.xboot.social.controller;
 
import cn.exrick.xboot.core.common.annotation.SystemLog;
import cn.exrick.xboot.core.common.constant.CommonConstant;
import cn.exrick.xboot.core.common.constant.SecurityConstant;
import cn.exrick.xboot.core.common.enums.LogType;
import cn.exrick.xboot.core.common.redis.RedisTemplateHelper;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.core.config.security.SecurityUserDetails;
import cn.exrick.xboot.core.entity.User;
import cn.exrick.xboot.social.entity.Social;
import cn.exrick.xboot.social.service.SocialService;
import cn.exrick.xboot.social.vo.WokWechatUserInfo;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;
 
/**
 * https://work.weixin.qq.com/api/doc/90000/90135/91020
 * @author Exrickx
 */
@Slf4j
@Api(tags = "企业微信登录接口")
@RequestMapping("/xboot/social/workwechat")
@Controller
public class WorkWechatController {
 
    @Value("${xboot.social.workwechat.appId}")
    private String appId;
 
    @Value("${xboot.social.workwechat.agentId}")
    private String agentId;
 
    @Value("${xboot.social.workwechat.appSecret}")
    private String appSecret;
 
    @Value("${xboot.social.workwechat.callbackUrl}")
    private String callbackUrl;
 
    @Value("${xboot.social.callbackFeUrl}")
    private String callbackFeUrl;
 
    @Value("${xboot.social.callbackFeRelateUrl}")
    private String callbackFeRelateUrl;
 
    private static final String STATE = SecurityConstant.WORKWECHAT_STATE;
 
    private static final Integer TYPE = CommonConstant.SOCIAL_TYPE_WORKWECHAT;
 
    @Autowired
    private SocialService socialService;
 
    @Autowired
    private SecurityUtil securityUtil;
 
    @Autowired
    private RedisTemplateHelper redisTemplate;
 
    /**
     * 申请令牌地址
     */
    private static final String AUTHORIZE_URL = "https://open.work.weixin.qq.com/wwopen/sso/qrConnect";
 
    /**
     * 获取access_token地址
     */
    private static final String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
 
    /**
     * 获取用户ID地址
     */
    private static final String GET_USERINFO_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo";
 
    /**
     * 获取用户信息地址
     */
    private static final String GET_USER_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/get";
 
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ApiOperation(value = "获取企业微信认证链接")
    @ResponseBody
    public Result<Object> login() {
 
        // 生成并保存state 忽略该参数有可能导致CSRF攻击
        String state = String.valueOf(System.currentTimeMillis());
        redisTemplate.set(STATE + state, "VALID", 3L, TimeUnit.MINUTES);
 
        // 传递参数appId、agentid、state、redirect_uri
        String url = AUTHORIZE_URL + "?appid=" + appId + "&agentid=" + agentId + "&state=" + state
                + "&redirect_uri=" + callbackUrl;
 
        return ResultUtil.data(url);
    }
 
    @RequestMapping(value = "/callback", method = RequestMethod.GET)
    @ApiOperation(value = "获取accessToken")
    @SystemLog(description = "企业微信关联登录", type = LogType.LOGIN)
    public String callback(@RequestParam(required = false) String code,
                                 @RequestParam(required = false) String state) throws UnsupportedEncodingException {
 
        if (StrUtil.isBlank(code)) {
            return "redirect:" + callbackFeUrl + "?error=" + URLEncoder.encode("您未同意授权", "utf-8");
        }
        // 验证state
        String v = redisTemplate.get(STATE + state);
        redisTemplate.delete(STATE + state);
        if (StrUtil.isBlank(v)) {
            return "redirect:" + callbackFeUrl + "?error=" + URLEncoder.encode("授权超时或state不正确", "utf-8");
        }
 
        // 申请令牌 get请求 传递参数appId、appSecret
        String result = HttpUtil.get(ACCESS_TOKEN_URL + "?corpid=" + appId +"&corpsecret=" + appSecret);
        if (!result.contains("access_token")) {
            return "redirect:" + callbackFeUrl + "?error=" + URLEncoder.encode("获取access_token失败", "utf-8");
        }
        String accessToken = JsonParser.parseString(result).getAsJsonObject().get("access_token").getAsString();
 
        // 获取ID信息
        String userIdResult = HttpUtil.get(GET_USERINFO_URL + "?access_token=" + accessToken + "&code=" + code);
        String userId = JsonParser.parseString(userIdResult).getAsJsonObject().get("UserId").getAsString();
 
        // 获取User信息
        String userInfo = HttpUtil.get(GET_USER_URL + "?access_token=" + accessToken + "&userid=" + userId);
 
        WokWechatUserInfo w = new Gson().fromJson(userInfo, WokWechatUserInfo.class);
        // 存入数据库
        Social workwechat = socialService.findByOpenIdAndPlatform(userId, TYPE);
        if (workwechat == null) {
            Social neww = new Social().setOpenId(userId).setUsername(w.getName()).setAvatar(w.getAvatar()).setPlatform(TYPE);
            workwechat = socialService.save(neww);
        }
 
        String url = "";
        // 判断是否绑定账号
        if (StrUtil.isNotBlank(workwechat.getRelateUsername())) {
            // 已绑定 直接登录
            String JWT = securityUtil.getToken(workwechat.getRelateUsername(), true);
            // 存入redis
            String JWTKey = IdUtil.simpleUUID();
            redisTemplate.set(JWTKey, JWT, 2L, TimeUnit.MINUTES);
            url = callbackFeUrl + "?related=1&JWTKey=" + JWTKey;
            // 记录日志使用
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    new SecurityUserDetails(new User().setUsername(workwechat.getRelateUsername())), null, null);
            SecurityContextHolder.getContext().setAuthentication(authentication);
        } else {
            // 未绑定 Redis存入id
            String idToken = IdUtil.simpleUUID();
            redisTemplate.set(idToken, workwechat.getId(), 5L, TimeUnit.MINUTES);
            url = callbackFeRelateUrl + "?socialType=" + TYPE + "&id=" + idToken;
        }
        return "redirect:" + url;
    }
}