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
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.WechatUserInfo;
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.JsonObject;
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://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
 * @author Exrickx
 */
@Slf4j
@Api(tags = "微信登录接口")
@RequestMapping("/xboot/social/wechat")
@Controller
public class WechatController {
 
    @Value("${xboot.social.wechat.appId}")
    private String appId;
 
    @Value("${xboot.social.wechat.appSecret}")
    private String appSecret;
 
    @Value("${xboot.social.wechat.callbackUrl}")
    private String callbackUrl;
 
    @Value("${xboot.social.callbackFeUrl}")
    private String callbackFeUrl;
 
    @Value("${xboot.social.callbackFeRelateUrl}")
    private String callbackFeRelateUrl;
 
    private static final String STATE = SecurityConstant.WECHAT_STATE;
 
    private static final Integer TYPE = CommonConstant.SOCIAL_TYPE_WECHAT;
 
    @Autowired
    private SocialService socialService;
 
    @Autowired
    private SecurityUtil securityUtil;
 
    @Autowired
    private RedisTemplateHelper redisTemplate;
 
    /**
     * 微信认证服务器地址
     */
    private static final String AUTHORIZE_URL = "https://open.weixin.qq.com/connect/qrconnect";
 
    /**
     * 申请令牌地址
     */
    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
 
    /**
     * 获取用户信息地址
     */
    private static final String GET_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo";
 
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ApiOperation(value = "获取wechat认证链接")
    @ResponseBody
    public Result<Object> login() throws UnsupportedEncodingException {
 
        // 生成并保存state 忽略该参数有可能导致CSRF攻击
        String state = String.valueOf(System.currentTimeMillis());
        redisTemplate.set(STATE + state, "VALID", 3L, TimeUnit.MINUTES);
 
        // 传递参数response_type、client_id、state、redirect_uri
        String url = AUTHORIZE_URL + "?appid=" + appId + "&redirect_uri=" + URLEncoder.encode(callbackUrl, "utf-8") + "&response_type=code" +
                "&scope=snsapi_login&state=" + state;
 
        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");
        }
 
        // 传递参数grant_type、code、redirect_uri、appid、appsecret
        String accessTokenUrl = ACCESS_TOKEN_URL + "?appid=" + appId + "&secret=" + appSecret + "&code=" + code +
                "&grant_type=authorization_code";
 
        // 申请令牌
        String result = HttpUtil.get(accessTokenUrl);
        if (!result.contains("access_token")) {
            return "redirect:" + callbackFeUrl + "?error=" + URLEncoder.encode("获取access_token失败", "utf-8");
        }
 
        JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject();
        String accessToken = jsonObject.get("access_token").getAsString();
        String openId = jsonObject.get("openid").getAsString();
        // 获取用户信息
        String userInfo = HttpUtil.get(GET_USERINFO_URL + "?access_token=" + accessToken + "&openid=" + openId);
        WechatUserInfo wu = new Gson().fromJson(userInfo, WechatUserInfo.class);
        // 存入数据库
        Social wechat = socialService.findByOpenIdAndPlatform(wu.getOpenid(), TYPE);
        if (wechat == null) {
            Social w = new Social().setOpenId(wu.getOpenid()).setUsername(wu.getNickname()).setAvatar(wu.getHeadimgurl()).setPlatform(TYPE);
            wechat = socialService.save(w);
        }
 
        String url = "";
        // 判断是否绑定账号
        if (StrUtil.isNotBlank(wechat.getRelateUsername())) {
            // 已绑定 直接登录
            String JWT = securityUtil.getToken(wechat.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(wechat.getRelateUsername())), null, null);
            SecurityContextHolder.getContext().setAuthentication(authentication);
        } else {
            // 未绑定 Redis存入id
            String idToken = IdUtil.simpleUUID();
            redisTemplate.set(idToken, wechat.getId(), 5L, TimeUnit.MINUTES);
            url = callbackFeRelateUrl + "?socialType=" + TYPE + "&id=" + idToken;
        }
        return "redirect:" + url;
    }
}