java auth weixin_Java微信SDK方式进行网页授权

SDK作用:他已经把微信大部分功能都做好了,我们只需要进行配置,再调用其方法就可以完成微信的一些功能开发。

目前开源的微信公众号SDK有:jfinal-weixin,weixin-java-tools,fastweixin,weixin-popular

本次使用weixin-java-tools。

1.在maven中添加依赖

com.github.binarywang

weixin-java-mp

3.0.0

2.在项目中进行配置,

一般配置信息不会在项目代码中进行配置,会统一做一个全局配置

package com.wechat.order.config;

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

/*微信公众号账号设置*/

@Data//使用了lombok依赖,相当于在代码中getter和setter的作用

@Component

@ConfigurationProperties(prefix = "wechat")//账号信息写在了application.yml中。

public class WeChatAccountConfig {

private String mpAppId;

private String mpAppSecret;

}

package com.wechat.order.config;

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 weChatAccountConfig;

//把你要实例化的对象转化成一个Bean,放在IoC容器中

@Bean

public WxMpService wxMpService() {

WxMpService wxService = new WxMpServiceImpl();// 实际项目中请注意要保持单例,不要在每次请求时构造实例,具体可以参考demo项目

wxService.setWxMpConfigStorage(wxMpConfigStorage());

return wxService;

}

@Bean

private WxMpConfigStorage wxMpConfigStorage() {

WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();

config.setAppId(weChatAccountConfig.getMpAppId()); // 设置微信公众号的appid

config.setSecret(weChatAccountConfig.getMpAppSecret()); // 设置微信公众号的app corpSecret

return config;

}

}

3.调用方法

package com.wechat.order.controller;

import com.wechat.order.enums.ResultEnum;

import com.wechat.order.exception.SellException;

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 java.net.URLEncoder;

@Controller

@RequestMapping("/wechat")

@Slf4j//日志注解

public class WeChatController {

@Autowired

private WxMpService wxMpService;

/**

* 换取微信用户openId

* @param returnUrl

* @return

*/

@GetMapping("/authorize")

public String authorize(@RequestParam("returnUrl")String returnUrl) {

//1.配置(在上步已配置完毕)

//2.调用方法

String url = "http://e724bad2.ngrok.io/sell/wechat/userInfo";//实际项目中,网站根目录应该做一个全局配置,此处为了方便,直接写

String redirectUri = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, URLEncoder.encode(returnUrl));

log.info("redirectUri={}", redirectUri);//日志

return "redirect:" + redirectUri;//跳转

}

/*获得用户信息*/

@GetMapping("/userInfo")

public String userInfo(@RequestParam("code") String code,

@RequestParam("state") String returnUrl) {

//获得access token

WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();

try {

wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);

}catch (WxErrorException e){

log.error("【微信网页授权】{}",e);

throw new SellException(ResultEnum.WECHAT_MP_ERROR);//授权失败抛出异常,此方法同学们自行编写,若不会可删除此行。

}

//获取openId

String openId = wxMpOAuth2AccessToken.getOpenId();

log.info("openId={}",openId);

return "redirect:" + returnUrl+"?openid="+openId;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值