Java-微信和支付宝进行默认授权获取openId/UserId

前提:有些需求需要用到微信用户的openID的时候,而又不想用户进行确认授权的时候,就可以进行默认授权

一、微信默认授权

1.1设置网页授权域名--(公众号设置-功能设置-网页授权域名)

1.2.获取APPID和secret

1.3 默认授权页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
</head>
<body>
<script th:inline="javascript">
    window.onload = function () {
        window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + [[${appId}]] + "&
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java实现的微信支付功能模块整合和获取openid的代码块: 1. 配置微信支付参数 ```java public class WXPayConfigImpl extends WXPayConfig { private String appID = "your_app_id"; // 微信公众号appid private String mchID = "your_mch_id"; // 微信商户号 private String apiKey = "your_api_key"; // API密钥 @Override public String getAppID() { return appID; } @Override public String getMchID() { return mchID; } @Override public String getKey() { return apiKey; } @Override public InputStream getCertStream() { // 支付证书(选填) return null; } @Override public IWXPayDomain getWXPayDomain() { return WXPayDomainSimpleImpl.instance(); } } ``` 2. 创建微信支付请求对象 ```java public class WXPayRequest { private WXPay wxpay; private String notifyUrl = "your_notify_url"; // 支付结果通知地址 public WXPayRequest() throws Exception { WXPayConfigImpl config = new WXPayConfigImpl(); wxpay = new WXPay(config); } /** * 统一下单 * * @param body 商品描述 * @param outTradeNo 商户订单号 * @param totalFee 标价金额(分) * @param spbillCreateIp 用户IP地址 * @return * @throws Exception */ public Map<String, String> unifiedOrder(String body, String outTradeNo, int totalFee, String spbillCreateIp) throws Exception { Map<String, String> data = new HashMap<>(); data.put("body", body); data.put("out_trade_no", outTradeNo); data.put("fee_type", "CNY"); data.put("total_fee", String.valueOf(totalFee)); data.put("spbill_create_ip", spbillCreateIp); data.put("notify_url", notifyUrl); data.put("trade_type", "JSAPI"); data.put("openid", ""); // openid return wxpay.unifiedOrder(data); } /** * 查询订单 * * @param outTradeNo 商户订单号 * @return * @throws Exception */ public Map<String, String> orderQuery(String outTradeNo) throws Exception { Map<String, String> data = new HashMap<>(); data.put("out_trade_no", outTradeNo); return wxpay.orderQuery(data); } } ``` 3. 获取openid ```java public class WXPayUtil { private static final String TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token"; /** * 获取openid * * @param code * @return * @throws Exception */ public static String getOpenid(String code) throws Exception { String url = TOKEN_URL + "?appid=your_app_id&secret=your_app_secret&code=" + code + "&grant_type=authorization_code"; String result = HttpsUtil.httpsRequestToString(url, "GET", null); JSONObject json = JSONObject.fromObject(result); String openid = json.getString("openid"); return openid; } } ``` 其中,`HttpsUtil.httpsRequestToString()`方法是一个封装了https请求的工具类,具体实现可以自行搜索。 在使用时,只需要调用`WXPayRequest`中的`unifiedOrder`方法进行支付,调用`WXPayRequest`中的`orderQuery`方法查询订单状态,调用`WXPayUtil`中的`getOpenid`方法获取openid即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值