forked from fanruan/finekit
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
3.0 KiB
77 lines
3.0 KiB
6 years ago
|
package com.fanruan.api.security;
|
||
|
|
||
|
import com.fanruan.api.Prepare;
|
||
|
import com.fr.cert.token.JwtBuilder;
|
||
|
import com.fr.cert.token.Jwts;
|
||
|
import com.fr.cert.token.SignatureAlgorithm;
|
||
|
import com.fr.security.SecurityToolbox;
|
||
|
import com.fr.stable.CodeUtils;
|
||
|
import org.junit.Assert;
|
||
|
import org.junit.Test;
|
||
|
|
||
|
import java.io.UnsupportedEncodingException;
|
||
|
import java.util.Calendar;
|
||
|
import java.util.Date;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
* @author richie
|
||
|
* @version 10.0
|
||
|
* Created by richie on 2019-09-03
|
||
|
*/
|
||
|
public class JwtKitTest extends Prepare {
|
||
|
|
||
|
@Test
|
||
|
public void testCreateDefaultJWT() throws Exception {
|
||
|
String tokenCN = JwtKit.createDefaultJWT("你好,我是中国人");
|
||
|
Map<String, Object> claims = JwtKit.parseJWT(tokenCN);
|
||
|
Assert.assertEquals("你好,我是中国人", CodeUtils.cjkDecode(String.valueOf(claims.get("sub"))));
|
||
|
|
||
|
String tokenEN = JwtKit.createDefaultJWT("Hello, world");
|
||
|
Map<String, Object> claims2 = JwtKit.parseJWT(tokenEN);
|
||
|
Assert.assertEquals("Hello, world", CodeUtils.cjkDecode(String.valueOf(claims2.get("sub"))));
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void testClaims() throws Exception {
|
||
|
String token = JwtKit.createDefaultJWT("千万", "我是千万的爹");
|
||
|
Map<String, Object> claims2 = JwtKit.parseJWT(token);
|
||
|
String text = CodeUtils.cjkDecode(String.valueOf(claims2.get("description")));
|
||
|
Assert.assertEquals("我是千万的爹", text);
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void test1() throws UnsupportedEncodingException {
|
||
|
String token = createToken().compact();
|
||
|
Assert.assertEquals(Jwts.parser().setSigningKey("abc=啊").parseClaimsJws(token).getBody().getSubject(), "hello.cpt");
|
||
|
|
||
|
token = createToken().signWithBase64SecretKey(SignatureAlgorithm.HS256, SecurityToolbox.byte2Base64("abc=啊".getBytes())).compact();
|
||
|
Assert.assertEquals(Jwts.parser()
|
||
|
.setBase64SigningKey(SecurityToolbox.byte2Base64("abc=啊".getBytes()))
|
||
|
.parseClaimsJws(token).getBody().getSubject(), "hello.cpt");
|
||
|
|
||
|
token = createToken()
|
||
|
.signWith(SignatureAlgorithm.HS256, "abc=啊".getBytes("GBK"))
|
||
|
.compact();
|
||
|
Assert.assertEquals("hello.cpt", Jwts.parser().setSigningKey("abc=啊".getBytes("GBK")).parseClaimsJws(token).getBody().getSubject());
|
||
|
}
|
||
|
|
||
|
private JwtBuilder createToken() {
|
||
|
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
|
||
|
|
||
|
Calendar calendar = Calendar.getInstance();
|
||
|
calendar.set(2019, Calendar.FEBRUARY, 20, 18, 0, 0);
|
||
|
Date currentTime = calendar.getTime();
|
||
|
calendar.set(2029, Calendar.FEBRUARY, 20, 18, 0, 0);
|
||
|
Date expirationTime = calendar.getTime();
|
||
|
return Jwts.builder()
|
||
|
.setHeaderParam("typ", "JWT")
|
||
|
.setIssuer("fanruan")
|
||
|
.setSubject("hello.cpt")
|
||
|
.setExpiration(expirationTime)
|
||
|
.setIssuedAt(currentTime)
|
||
|
.setId("01")
|
||
|
.signWith(signatureAlgorithm, "abc=啊");
|
||
|
}
|
||
|
|
||
|
}
|