
Ein Web-Token ist mit Java schnell erzeugt….

Wir nutzen dazu die jsonwebtoken Lib. Wir brauchen diese Abhängigkeiten in der pom.xml, zwei nur zur Laufzeit
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 	<modelVersion>4.0.0</modelVersion> 	<groupId>de.wenzlaff.jwt</groupId> 	<artifactId>de.wenzlaff.jwt</artifactId> 	<version>0.0.1-SNAPSHOT</version> 	<dependencies> 		<dependency> 			<groupId>io.jsonwebtoken</groupId> 			<artifactId>jjwt-api</artifactId> 			<version>0.11.5</version> 		</dependency> 		<dependency> 			<groupId>io.jsonwebtoken</groupId> 			<artifactId>jjwt-impl</artifactId> 			<version>0.11.5</version> 			<scope>runtime</scope> 		</dependency> 		<dependency> 			<groupId>io.jsonwebtoken</groupId> 			<artifactId>jjwt-jackson</artifactId> 			<version>0.11.5</version> 			<scope>runtime</scope> 		</dependency> 	</dependencies> </project> | 
Dann noch etwas Java-Code:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |                 Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256); 		String jws = Jwts.builder().setSubject("kleinhirn.eu").signWith(key) 				.compact(); 		System.out.println("Token: " + jws); 		String subject = Jwts.parserBuilder().setSigningKey(key).build() 				.parseClaimsJws(jws).getBody().getSubject(); 		System.out.println("Subject: " + subject); 		Claims body = Jwts.parserBuilder().setSigningKey(key).build() 				.parseClaimsJws(jws).getBody(); 		System.out.println("Body: " + body); 		String algorithm = Jwts.parserBuilder().setSigningKey(key).build() 				.parseClaimsJws(jws).getHeader().getAlgorithm(); 		System.out.println("getAlgorithm: " + algorithm); | 
Schon haben wir einen Web-Token:
| 1 2 3 4 | Token: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJrbGVpbmhpcm4uZXUifQ.2ls4hoyYQyEn9GXpMAlA6b7BJgBIUPiwI0L18wdskkg Subject: kleinhirn.eu Body: {sub=kleinhirn.eu} getAlgorithm: HS256 | 
