Commit 814e296a by TongZuu

..

parents
/target/
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Test-jwt</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.csc.library</groupId>
<artifactId>Test-jwt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test-jwt</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.csc.library;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
package com.csc.library.jwt;
import java.security.Key;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.crypto.MacProvider;
public class TestJjwt {
public static void main(String[] args) {
Key key = MacProvider.generateKey();
String compactJws = Jwts.builder()
.setSubject("Joe")
.signWith(SignatureAlgorithm.HS512, key)
.compact();
System.out.println(compactJws);
}
}
package com.csc.library.jwt;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.interfaces.DecodedJWT;
public class TestJwt {
public static void main(String[] args) throws IllegalArgumentException, UnsupportedEncodingException {
String secret = "zeeme.csc-secret";
String issuer = "Computer Science Corporation Limited";
String subject = "zeeme.csc";
try {
long st1 = System.currentTimeMillis();
Map<String,Object> header = new HashMap<String,Object>();
header.put("type", "JWT");
header.put("alg", "HS256");
header.put("test", "test");
Algorithm alg = Algorithm.HMAC256(secret);
long st2 = System.currentTimeMillis();
System.out.println("pre data step : st : "+st2+" st1 : "+st1 +" == "+(st2-st1));
String token = JWT.create()
.withSubject(subject)
// .withHeader(header)
.withIssuer(issuer)
.withClaim("test claim", "test claim")
//.withExpiresAt(new Date())
.sign(alg);
long st3 = System.currentTimeMillis();
System.out.println("sign step : st2 : "+st2+" st3 : "+st3 +" == "+(st3-st2));
System.out.println(token);
// try {
// Thread.sleep(3000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println(JWT.decode(token).getClaims());
long st4 = System.currentTimeMillis();
System.out.println("decode step : st3 : "+st3+" st4 : "+st4 +" == "+(st4-st3));
JWTVerifier verifier = JWT.require(alg)
//.acceptExpiresAt(1000)
.withSubject(subject)
.withIssuer(issuer)
.build(); //Reusable verifier instance
DecodedJWT jwt = verifier.verify(token);
System.out.println(jwt.getSubject());
System.out.println(jwt.getHeaderClaim("type").asString());
long st5 = System.currentTimeMillis();
System.out.println("verify step : st4 : "+st4+" st5 : "+st5 +" == "+(st5-st4));
} catch (JWTCreationException exception){
//Invalid Signing configuration / Couldn't convert Claims.
}
}
}
package com.csc.library;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment