天天看点

SpringBoot 实现 微信网页授权登录

SpringBoot简单搭建若是不会,可以看我另一篇文章:https://blog.csdn.net/wang_jing_jing/article/details/115075991

SppringBoot 整合 Mybatis 不会的,可以看另一篇文章:https://blog.csdn.net/wang_jing_jing/article/details/115077270

一、pom.xml依赖配置

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-mp</artifactId>
    <version>2.9.0</version>
</dependency>
           

二、application.yml 添加配置

wechat:
  appId: xxxxxxxxxxxxxxx
  appSecret: xxxxxxxxxxxxxxxxxxxxxxxxx
  redirectURI: http://域名/wechat/userInfo
           

三、设置配置文件 WechatConfig.java 和 WeChatMpConfig

package com.example.emoticon.wechat;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatConfig {

    private String appId;

    private String appSecret;

    private String redirectURI;

}
           
package com.example.emoticon.wechat;


import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@Configuration
public class WeChatMpConfig {

    @Autowired
    private WechatConfig wechatAccountConfig;

    @Bean
    public WxMpService wxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage(){
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(wechatAccountConfig.getAppId());
        wxMpConfigStorage.setSecret(wechatAccountConfig.getAppSecret());
        return wxMpConfigStorage;
    }
}
           

四、controller 层编写

package com.example.emoticon.controller;

import com.example.emoticon.entity.WechatUser;
import com.example.emoticon.service.IWechatUserService;
import com.example.emoticon.wechat.WechatConfig;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.net.URLEncoder;

@Controller
@RequestMapping("/wechat")
@Slf4j
public class WechatController {

    @Autowired
    WxMpService wxMpService;

    @Autowired
    IWechatUserService wechatUserService;

    @Autowired
    private WechatConfig wechatAccountConfig;

    @GetMapping("/authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl) {
        log.info("微信网页授权--parm--returnUrl={}", returnUrl);
        String redirectURL = wxMpService.oauth2buildAuthorizationUrl(wechatAccountConfig.getRedirectURI(), WxConsts.OAuth2Scope.SNSAPI_USERINFO, URLEncoder.encode(returnUrl));
        log.info("微信网页授权:获取code,redirectURL={}", redirectURL);
        return "redirect:" + redirectURL;
    }

    @GetMapping("/userInfo")
    public String userInfo(@RequestParam("code") String code, @RequestParam("state") String returnUrl) throws Exception {
        log.info("微信网页授权: code={}", code);
        log.info("微信网页授权: state={}",returnUrl);	//即authorize中的returnUrl
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken;
        try {
            wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
        }catch (WxErrorException e) {
            log.info("微信网页授权: {}", e);
            throw new Exception(e.getError().getErrorMsg());
        }
        String openId = wxMpOAuth2AccessToken.getOpenId();

        //获取微信用户信息
        WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
        WechatUser wechatUser = new WechatUser();
        BeanUtils.copyProperties(wxMpUser, wechatUser);
        wechatUserService.insertTWechatUser(wechatUser);

        log.info("微信网页授权: openId={}", openId);
        return "redirect:" + returnUrl;
    }


}
           

五、service 层 和 dao 层就不做阐述了;若不会请看文章头部链接

注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!