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

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.dtcms.com/a/318096.html

相关文章:

  • clock_adjtime、clock_getres、clock_gettime、 clock_nanosleep、clock_settime 系统调用及示例
  • 【面试八股总结】线程/进程同步问题
  • 概率/期望 DP Let‘s Play Osu!
  • 【数论】素数
  • Vue3入门到精通: 1.2 Vue3响应式系统深度解析
  • go与grpc
  • 网站、域名、IP在什么场景下需要备案
  • Linux之Shell脚本基本语法
  • InfluxDB 集群部署与高可用方案(二)
  • 基于vue的财务管理系统/基于php的财务管理系统
  • 02.【数据结构-C语言】顺序表(线性表概念、顺序表实现:增删查、前向声明、顺序表实现通讯录项目:增删改查、通讯录数据导入及保存到本地文件)
  • <form> + <iframe> 方式下载大文件的机制
  • Python 通过Playwright+OpenCV破解滑动验证码 实例
  • 【Python】命令行工具实现监控ctrl+c与运行时长终止任务
  • 2024学年云南省职业院校技能大赛 “信息安全管理与评估”赛项 比赛样题任务书
  • FreeRTOS临界资源保护方法
  • 商派小程序商城(小程序/官网/APP···)的范式跃迁与增长再想象
  • android NDK 报错日志解读和还原报错方法名
  • Mybatis的高级特性
  • 【自动化运维神器Ansible】playbook核心组件之tags深度解析
  • 第一性原理科学计算服务器如何选择配置-CPU选择篇
  • thinkpad E14重装win 10系统
  • 云端软件工程智能代理:任务委托与自动化实践全解
  • Spring Boot Actuator 监控功能的简介及禁用
  • Java面试题036:一文深入了解VUE(1)
  • 批量提问程序开发方案:基于Python的百度文小言接口实现
  • 学习嵌入式之硬件——ARM体系
  • vue margin与padding对比
  • 用户体验设计中微投入设计:用户不知不觉付出的 3 种方式
  • 【24】C++实战篇——【 C++ 外部变量】 C++多个文件共用一个枚举变量,外部变量 extern,枚举外部变量 enum