springboot版本的微信授权

以前项目都是ssm版本的,最近从网上找了一些资料,修改了一版springboot版本的微信授权

代码:

<!--微信授权-->
<dependency>
   <groupId>com.github.binarywang</groupId>
   <artifactId>weixin-java-mp</artifactId>
   <version>3.7.0</version>
</dependency>
############微信公众号支付配置############
wechat:
  #公众号APPID
  appId: wx890e1ee5ee081dfe
  #公众号秘钥
  appSecret: 810135b716ab24e22b121c7916972cb5
  #商户号Id
  mchId: 1518762741
  #商户号秘钥
  mchKey: abcdefghijklmnopqrstuvwxyz123456
  #微信支付回调地址
  notifyUrl: http://nanhongshun.com/pay/notify
  # 证书所在位置,退款使用
  keyPath: C:/weixin_cert/h5.p12
  #微信授权回调地址
  callback: http://nanhongshun.com/wxAuth/callBack
package com.nan.config;


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

@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
    //公众号APPID
    private String appId;
    //公众号秘钥
    private String appSecret;
    //微信授权回调地址
    private String callback;
    //商户号id
    private String mchId;
    //商户号密钥
    private String mchKey;
    //商户证书路径
    private String keyPath;
    //微信支付异步回调地址
    private String notifyUrl;

}
package com.nan.controller;


import com.nan.config.WechatAccountConfig;
import com.nan.service.PushMessageService;
import com.nan.service.UserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.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.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.Session;


@Slf4j
@Controller
@RequestMapping("/wxAuth")
public class WxLoginController {
    @Autowired
    private WechatAccountConfig wechatAccountConfig;
//    @Autowired
//    private UserService userService;
    @Autowired
    private WxMpService wxMpService;
//    @Autowired
//    private PushMessageService messageService;

    /**
     * 微信授权获取code
     *
     */
    @RequestMapping("/login")
    public String wxLogin(){
        String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(wechatAccountConfig.getCallback(), WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
        log.info("【微信网页授权】获取code,result={}",redirectUrl);
        return "redirect:"+ redirectUrl;
    }

    /**
     * 微信授权回调
     *
     */
    @RequestMapping("/callBack")
    public ModelAndView wxCallBack(@RequestParam("code")String code, ModelAndView model, HttpServletRequest request){
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
        try {
            wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
        }catch(WxErrorException e){
            log.error("【微信网页授权】{}",e.getMessage());
            e.printStackTrace();
        }
        WxMpUser wxMpUser = new WxMpUser();
        try{
            wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
        }catch(Exception e){
          log.error("【微信授权】{}",e.getMessage());
          e.printStackTrace();
        }
        model.setViewName("chongzhi");
        model.addObject("nickname",wxMpUser.getNickname());
        model.addObject("headimgurl",wxMpUser.getHeadImgUrl());
        model.addObject("openid",wxMpUser.getOpenId());
        //模板消息
//        messageService.loginTemplate(wxMpUser.getNickname(),wxMpUser.getOpenId());
        return model;
    }

}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../jquery-1.7.2.min.js"></script>
</head>
<body>

    <div style="width: 100%;height: 400px;margin-top: 100px;text-align:center">
        <img th:src="${headimgurl}" style="width: 180px;height: 180px;margin-top: 40px"></img><br>
        <div th:text="${nickname}" style="font-size: 50px;padding-top: 40px;"></div>
        <div th:text="${openid}" style="font-size: 50px;padding-top: 40px;"></div>
    </div>

    

</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值