微信公众平台开发概述
- 导入
<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()));
}
}
//eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIyNDUwIiwic3ViIjoi5byg5LiJ5LiJIiwiaWF0IjoxNjA0OTA3NzUyfQ.1c1SK9Knij6i3hucepdv-C97pq-dYmUltovNKqc_Wv8
//______________________________
//id:2450
//subject:张三三
//IssuedAt:2020-11-09 15:42:32
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() {
//为了方便测试,我们将过期时间设置为1分钟
long now = System.currentTimeMillis();//当前时间
long exp = now + 1000 * 60;//过期时间为1分钟:60000
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()));
// 测试运行,当未过期时可以正常读取,当过期时会引发
// io.jsonwebtoken.ExpiredJwtException异常。
}
}
/*eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ODgiLCJzdWIiOiLlsI_nmb0iLCJpYXQiOjE2MDQ5MDg2MDAsImV4cCI6MTYwNDkwODY2MH0.uNQxJOpi-sn7zTR2v2Fttt1A8pfhLChWGPFxdby7WRs
______________________________
id:888
subject:小白
签发时间:2020‐11‐09 03:56:40
过期时间:2020‐11‐09 03:57:40
当前时间:2020‐11‐09 03:56:42*/
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() {
//为了方便测试,我们将过期时间设置为1分钟
long now = System.currentTimeMillis();//当前时间
long exp = now + 1000 * 60;//过期时间为1分钟
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()));
}
/*eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ODgiLCJzdWIiOiLlsI_nmb0iLCJpYXQiOjE2MDQ5MDg5OTYsImV4cCI6MTYwNDkwOTA1NSwicm9sZXMiOiJhZG1pbiIsImxvZ28iOiJsb2dvLnBuZyJ9.JP85Ez64SEcssRRQ-SB6Ba-WvXa_52r1pRjAnxZxeHQ
______________________________
id:888
subject:小白
roles:admin
logo:logo.png
签发时间:2020‐11‐09 04:03:16
过期时间:2020‐11‐09 04:04:15
当前时间:2020‐11‐09 04:03:17*/
}