• 使用sdk方式获取微信的用户信息


    一、pom.xml 依赖

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

    二、application.yml配置

    wechat:
      mpAppId: wx6beb2249b6c*****(appID)
      mpAppSecret: 9138282d930eb7ee7f7a59*******(appsecret)
    

    三、从application.yml中获取配置信息

    @Data
    @Component
    @ConfigurationProperties(prefix = "wechat")
    public class WechatAccountConfig {
    
        /**
         * 公众平台id
         */
        private String mpAppId;
    
        /**
         * 公众平台密钥
         */
        private String mpAppSecret;
    }

    四、配置微信参数信息

    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.stereotype.Component;
    
    @Component
    public class WechatMpConfig {
    
        @Autowired
        private WechatAccountConfig accountConfig;
    
        @Bean
        public WxMpService wxMpService() {
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
            return wxMpService;
        }
    
        @Bean
        public WxMpConfigStorage wxMpConfigStorage() {
            WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
            wxMpConfigStorage.setAppId(accountConfig.getMpAppId());
            wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret());
            return wxMpConfigStorage;
        }
    }

     五、微信授权接口

    
    

    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 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 com.liuhq.config.ProjectUrlConfig;
    import com.liuhq.enums.ResultEnum;
    import com.liuhq.exception.SellException;

    
    

    import java.net.URLEncoder;


    @Controller @RequestMapping(
    "/wechat") @Slf4j public class WechatController { @Autowired private WxMpService wxMpService; @Autowired private ProjectUrlConfig projectUrlConfig; @GetMapping("/authorize") public String authorize(@RequestParam("returnUrl") String returnUrl) { //1. 配置 //2. 调用方法 String url = http://域名 + "/wechat/userInfo"; String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl)); return "redirect:" + redirectUrl; } @GetMapping("/userInfo") public String userInfo(@RequestParam("code") String code, @RequestParam("state") String returnUrl) { WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken(); try { wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
    WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null); //微信用户信息 }
    catch (WxErrorException e) { log.error("【微信网页授权】{}", e); throw new SellException(ResultEnum.WECHAT_MP_ERROR.getCode(), e.getError().getErrorMsg()); } String openId = wxMpOAuth2AccessToken.getOpenId(); //获取openId return "redirect:" + returnUrl + "?openid=" + openId; } }
  • 相关阅读:
    linux系统的nobody用户
    java包命名规则
    配置文件解析
    jps参数
    Java数据类型总结
    JSON与JAVA数据的相互转换
    maven中使用net.sf.json-lib
    设计 REST 风格的 MVC 框架
    Java 5种字符串拼接方式性能比较
    spring获取webapplicationcontext,applicationcontext几种方法详解
  • 原文地址:https://www.cnblogs.com/lhq1996/p/13321484.html
Copyright © 2020-2023  润新知