当前位置: 首页 > ai >正文

springboot项目justAuth扩展第二个小程序

1. 首先用的是bootstart

<dependency><groupId>com.youkol.support</groupId><artifactId>justauth-spring-boot-starter</artifactId><version>${justauth-spring-boot.version}</version>
</dependency>

2. 自定义request(枚举类配置方式),继承AuthDefaultRequest 

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthDefaultRequest;
import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.utils.UrlBuilder;public class AuthWechatMiniBrokerRequest extends AuthDefaultRequest {public AuthWechatMiniBrokerRequest(AuthConfig config, AuthSource source) {super(config, ExtendSource.WECHAT_MINI_PROGRAM_BROKER);}public AuthWechatMiniBrokerRequest(AuthConfig config, AuthStateCache authStateCache) {super(config, ExtendSource.WECHAT_MINI_PROGRAM_BROKER, authStateCache);}@Overridepublic AuthToken getAccessToken(AuthCallback authCallback) {// 参见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html 文档// 使用 code 获取对应的 openId、unionId 等字段String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode())).getBody();JSCode2SessionResponse accessTokenObject = JSONObject.parseObject(response, JSCode2SessionResponse.class);assert accessTokenObject != null;checkResponse(accessTokenObject);// 拼装结果return AuthToken.builder().openId(accessTokenObject.getOpenid()).unionId(accessTokenObject.getUnionId()).accessToken(accessTokenObject.getSessionKey()).build();}@Overridepublic AuthUser getUserInfo(AuthToken authToken) {// 参见 https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html 文档// 如果需要用户信息,需要在小程序调用函数后传给后端return AuthUser.builder().username("").nickname("").avatar("").uuid(authToken.getOpenId()).token(authToken).source(source.toString()).build();}/*** 检查响应内容是否正确** @param response 请求响应内容*/private void checkResponse(JSCode2SessionResponse response) {if (response.getErrorCode() != 0) {throw new AuthException(response.getErrorCode(), response.getErrorMsg());}}@Overrideprotected String accessTokenUrl(String code) {return UrlBuilder.fromBaseUrl(source.accessToken()).queryParam("appid", config.getClientId()).queryParam("secret", config.getClientSecret()).queryParam("js_code", code).queryParam("grant_type", "authorization_code").build();}@Data@SuppressWarnings("SpellCheckingInspection")private static class JSCode2SessionResponse {@JSONField(name = "errcode")private int errorCode;@JSONField(name = "errmsg")private String errorMsg;@JSONField(name = "session_key")private String sessionKey;private String openid;@JSONField(name = "unionid")private String unionId;}
}

3. 自定义AuthSource(枚举类配置方式),继承AuthSource


import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.request.AuthDefaultRequest;public enum ExtendSource implements AuthSource {WECHAT_MINI_PROGRAM_BROKER {@Overridepublic String authorize() {// 参见 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 文档throw new UnsupportedOperationException("不支持获取授权 url,请使用小程序内置函数 wx.login() 登录获取 code");}@Overridepublic String accessToken() {// 参见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html 文档// 获取 openid, unionId , session_key 等字段return "https://api.weixin.qq.com/sns/jscode2session";}@Overridepublic String userInfo() {// 参见 https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html 文档throw new UnsupportedOperationException("不支持获取用户信息 url,请使用小程序内置函数 wx.getUserProfile() 获取用户信息");}@Overridepublic Class<? extends AuthDefaultRequest> getTargetClass() {return AuthWechatMiniBrokerRequest.class;}}
}

4. yml配置

justauth:enabled: truetype:WECHAT_MINI_PROGRAM: # 微信小程序client-id: ${wx.miniapp.appid}client-secret: ${wx.miniapp.secret}ignore-check-redirect-uri: trueignore-check-state: true # 微信小程序,不会使用到 state,所以不进行校验extend:enum-class: com.demo.module.system.framework.justauth.extend.ExtendSourceconfig:WECHAT_MINI_PROGRAM_BROKER:request-class: com.demo.module.system.framework.justauth.extend.AuthWechatMiniBrokerRequestclient-id: wxea86b728b1e5ca8hclient-secret: c69e4d7b65b48h9150235e8de55b246cignore-check-redirect-uri: trueignore-check-state: true # 微信小程序,不会使用到 state,所以不进行校验cache:type: REDISprefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE::timeout: 24h # 超时时长,目前只对 Redis 缓存生效,默认 3 分钟

配置就是上面的配置,具体使用参看官网文档:

justauth-spring-boot-starter: Spring boot starter for JustAuth (Spring Boot 集成 JustAuth)

JustAuth

http://www.xdnf.cn/news/17092.html

相关文章:

  • Unity轻量观察相机
  • 功能安全和网络安全的综合保障流程
  • 云端软件工程智能代理:任务委托与自动化实践全解
  • CDP集群中通过Hive外部表迁移HBase数据的操作记录
  • 昇思+昇腾开发板+DeepSeek模型推理和性能优化
  • 自己本地搭建的服务器怎么接公网?公网IP直连服务器方法,和只有内网IP直接映射到互联网
  • 线性代数中矩阵的基本运算运算
  • 哲学中的主体性:历史演进、理论范式与当代重构
  • FLAN-T5:大规模指令微调的统一语言模型框架
  • python-自定义抠图
  • OpenSpeedy绿色免费版下载,提升下载速度,网盘下载速度等游戏变速工具
  • Datawhale AI夏令营 第三期 task2 稍微改进
  • MyBatis实现SQL
  • Python日志记录库——logaid
  • Centos-Stream 10 安装教程(2025版图文教程)
  • ASP3605I同步降压调节器的高频化设计与多相扩展技术优化方案
  • Python 函数详解
  • 重生之我在暑假学习微服务第十天《网关篇》
  • 微软Dragon Ambient eXperience (DAX) 深度解析
  • 《UE教程》第一章第六回——迁移独立项目(资源)
  • 【学习嵌入式day-17-数据结构-单向链表/双向链表】
  • 【计算机网络】6应用层
  • 深度学习·基础知识
  • selenium自动化收集资料
  • 从汇编角度揭秘C++构造函数(1)
  • 【深度学习新浪潮】混元3D是什么产品?
  • 2025《艾诺提亚失落之歌》逆向工程解包尝试
  • 【模电笔记】—— 直流稳压电源——整流、滤波电路
  • 二叉树——堆及其实现
  • 防御保护07-08