微信公众平台开发概述
- 导入
| <dependency> |
| <groupId>io.jsonwebtoken</groupId> |
| <artifactId>jjwt</artifactId> |
| <version>0.6.0</version> |
| </dependency> |
- 格式化代码:ctrl+alt+L
- 认证
- 每个用户针对每个公众号会产生一个安全的OpenID
- 但他对所有这些同一开放平台账号下的公众号和应用,只有一个UnionID
- 公众平台以access_token为接口调用凭据,来调用接口,所有接口的调用需要先获取access_token,过期时间默认为2小时。
- 用户向公众号发送消息时,公众号方收到的消息发送者是一个OpenID,是使用用户微信号加密后的结果,每个用户对每个公众号有一个唯一的OpenID。
- 全局返回码说明
- 接口调用请求说明
CreateJwtParse01
| public class CreateJwtParse01 { |
| public static void main(String[] args) { |
| String jwtTest = CreateJwtTest(); |
| System.out.println(jwtTest); |
| System.out.println("______________________________"); |
| ParseJwtTest(jwtTest); |
| } |
| |
| static String CreateJwtTest() { |
| JwtBuilder builder = Jwts.builder() |
| .setId("2450") |
| .setSubject("张三三") |
| .setIssuedAt(new Date()) |
| .signWith(SignatureAlgorithm.HS256, "cheep1"); |
| return builder.compact(); |
| } |
| |
| static void ParseJwtTest(String str) { |
| Claims claims = |
| Jwts.parser().setSigningKey("cheep1").parseClaimsJws(str).getBody(); |
| System.out.println("id:" + claims.getId()); |
| System.out.println("subject:" + claims.getSubject()); |
| SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| System.out.println("IssuedAt:" + sdf1.format(claims.getIssuedAt())); |
| } |
| } |
| |
| |
| |
| |
| |
CreateJwtParse02 设置过期时间为1分钟
| public class CreateJwtParse02 { |
| public static void main(String[] args) { |
| String jwtTest01 = CreateJwtTest01(); |
| System.out.println(jwtTest01); |
| System.out.println("______________________________"); |
| ParseJwtTest01(jwtTest01); |
| } |
| |
| static String CreateJwtTest01() { |
| |
| long now = System.currentTimeMillis(); |
| long exp = now + 1000 * 60; |
| JwtBuilder builder = Jwts.builder().setId("888") |
| .setSubject("小白") |
| .setIssuedAt(new Date()) |
| .signWith(SignatureAlgorithm.HS256, "itcast") |
| .setExpiration(new Date(exp)); |
| return builder.compact(); |
| } |
| |
| static void ParseJwtTest01(String str) { |
| Claims claims = Jwts.parser().setSigningKey("itcast").parseClaimsJws(str).getBody(); |
| System.out.println("id:" + claims.getId()); |
| System.out.println("subject:" + claims.getSubject()); |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy‐MM‐dd hh:mm:ss"); |
| System.out.println("签发时间:" + sdf.format(claims.getIssuedAt())); |
| System.out.println("过期时间:" + sdf.format(claims.getExpiration())); |
| System.out.println("当前时间:" + sdf.format(new Date())); |
| |
| |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
CreateJwtParse01 Token添加自定义属性
| public class CreateJwtParse03 { |
| public static void main(String[] args) { |
| String jwtTest02 = CreateJwtTest02(); |
| System.out.println(jwtTest02); |
| System.out.println("______________________________"); |
| ParseJwtTest02(jwtTest02); |
| } |
| |
| static String CreateJwtTest02() { |
| |
| long now = System.currentTimeMillis(); |
| long exp = now + 1000 * 60; |
| JwtBuilder builder = Jwts.builder().setId("888") |
| .setSubject("小白") |
| .setIssuedAt(new Date()) |
| .signWith(SignatureAlgorithm.HS256, "itcast") |
| .setExpiration(new Date(exp)) |
| .claim("roles", "admin") |
| .claim("logo", "logo.png"); |
| return builder.compact(); |
| } |
| |
| static void ParseJwtTest02(String str) { |
| Claims claims = Jwts.parser().setSigningKey("itcast").parseClaimsJws(str).getBody( |
| ); |
| System.out.println("id:" + claims.getId()); |
| System.out.println("subject:" + claims.getSubject()); |
| System.out.println("roles:" + claims.get("roles")); |
| System.out.println("logo:" + claims.get("logo")); |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy‐MM‐dd hh:mm:ss"); |
| System.out.println("签发时间:" + sdf.format(claims.getIssuedAt())); |
| System.out.println("过期时间:" + sdf.format(claims.getExpiration())); |
| System.out.println("当前时间:" + sdf.format(new Date())); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~