12.18 微信扫码获取openid与登陆
官方文档: https://open.weixin.qq.com/
1.先获取code
1-1 配置项目配置文件
|
1
2
3
4
5
|
wechat: mpAppId: wxd898fcb01713c658 mpAppSecret: 47ccc303338cee6e62894fxxxxxxxxxxx openAppId: wx6ad144e54af67d87///////// openAppSecret: 91a2ff6d38a2bbccfb7e9f9079108e2e/////////配置密钥 |
1-2 配置相应的配置文件
|
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
|
Data@Component@ConfigurationProperties(prefix = "wechat")public class WechatAccountConfig { /** * 公众平台id */ private String mpAppId; /** * 公众平台密钥 */ private String mpAppSecret; /** * 开放平台id */ private String openAppId; /** * 开放平台密钥 */ private String openAppSecret; /** * 商户号 */ private String mchId; /** * 商户密钥 */ private String mchKey; /** * 商户证书路径 */ private String keyPath; /** * 微信支付异步通知地址 */ private String notifyUrl; /** * 微信模版id */ private Map<String, String> templateId;} |
1-3开放平台文件配置
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@Componentpublic class WechatOpenConfig { @Autowired private WechatAccountConfig accountConfig; @Bean public WxMpService wxOpenService() { WxMpService wxOpenService = new WxMpServiceImpl(); wxOpenService.setWxMpConfigStorage(wxOpenConfigStorage()); return wxOpenService; } @Bean public WxMpConfigStorage wxOpenConfigStorage() { WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage(); wxMpInMemoryConfigStorage.setAppId(accountConfig.getOpenAppId()); wxMpInMemoryConfigStorage.setSecret(accountConfig.getOpenAppSecret()); return wxMpInMemoryConfigStorage; }} |
1-4 controller,并配置同意的Url配置文件
url配置文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@Data@ConfigurationProperties(prefix = "projectUrl")@Componentpublic class ProjectUrlConfig { /** * 微信公众平台授权url */ public String wechatMpAuthorize; /** * 微信开放平台授权url */ public String wechatOpenAuthorize; /** * 点餐系统 */ public String sell;} |
项目配置文件
|
1
2
3
4
|
projectUrl: wechatMpAuthorize: http://sell.natapp4.cc wechatOpenAuthorize: http://sell.natapp4.cc sell: http://sell.natapp4.cc |
|
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
|
@Controller@RequestMapping("/wechat")@Slf4jpublic class WechatController { @Autowired private WxMpService wxMpService; @Autowired private WxMpService wxOpenService;@Autowired private ProjectUrlConfig projectUrlConfig; @GetMapping("/qrAuthorize") public String qrAuthorize(@RequestParam("returnUrl") String returnUrl) { String url = projectUrlConfig.getWechatOpenAuthorize() + "/sell/wechat/qrUserInfo"; String redirectUrl = wxOpenService.buildQrConnectUrl(url, WxConsts.QRCONNECT_SCOPE_SNSAPI_LOGIN, URLEncoder.encode(returnUrl)); return "redirect:" + redirectUrl; } @GetMapping("/qrUserInfo") public String qrUserInfo(@RequestParam("code") String code, @RequestParam("state") String returnUrl) { WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken(); try { wxMpOAuth2AccessToken = wxOpenService.oauth2getAccessToken(code); } catch (WxErrorException e) { log.error("【微信网页授权】{}", e); throw new SellException(ResultEnum.WECHAT_MP_ERROR.getCode(), e.getError().getErrorMsg()); } log.info("wxMpOAuth2AccessToken={}", wxMpOAuth2AccessToken); String openId = wxMpOAuth2AccessToken.getOpenId(); return "redirect:" + returnUrl + "?openid=" + openId; }} |
2.登陆
2-1controller创建登陆方法
步骤://1. openid去和数据库里的数据匹配
2. 设置token至redis:[
使用redis步骤:
1.引入依赖
|
1
2
3
4
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> |
2.在controller里面引入
|
1
2
|
@Autowired private StringRedisTemplate redisTemplate; |
3.在方法里
|
1
|
redisTepla.opsForValue().set("abc","1111111");//告诉redic调用并set key-value |
]
/3. 设置token至cookie
|
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
|
@Controller@RequestMapping("/seller")public class SellerUserController { @Autowired private SellerService sellerService; @Autowired private StringRedisTemplate redisTemplate; @Autowired private ProjectUrlConfig projectUrlConfig; @GetMapping("/login") public ModelAndView login(@RequestParam("openid") String openid, HttpServletResponse response, Map<String, Object> map) { //1. openid去和数据库里的数据匹配 SellerInfo sellerInfo = sellerService.findSellerInfoByOpenid(openid); if (sellerInfo == null) { map.put("msg", ResultEnum.LOGIN_FAIL.getMessage()); map.put("url", "/sell/seller/order/list"); return new ModelAndView("common/error"); } //2. 设置token至redis String token = UUID.randomUUID().toString(); Integer expire = RedisConstant.EXPIRE; redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX, token), openid, expire, TimeUnit.SECONDS); //3. 设置token至cookie CookieUtil.set(response, CookieConstant.TOKEN, token, expire); return new ModelAndView("redirect:" + projectUrlConfig.getSell() + "/sell/seller/order/list"); }} |
浙公网安备 33010602011771号