All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bitbucket.gkutiel.in.my.mind.feature.Jwt Maven / Gradle / Ivy

The newest version!
package org.bitbucket.gkutiel.in.my.mind.feature;

import static io.jsonwebtoken.Jwts.builder;
import static io.jsonwebtoken.Jwts.parser;
import static io.jsonwebtoken.SignatureAlgorithm.HS256;
import java.security.Key;
import com.google.gson.Gson;
import io.jsonwebtoken.impl.crypto.MacProvider;

public interface Jwt {
	static final Key key = MacProvider.generateKey();
	static final Gson gson = new Gson();

	public default  T decode(final String jwt, final Class type) {
		final String val = parser().setSigningKey(key).parseClaimsJws(jwt).getBody().getSubject();
		return gson.fromJson(val, type);
	}

	public default  String encode(final T obj) {
		final String sub = obj instanceof String ? (String) obj : gson.toJson(obj);
		return builder().setSubject(sub).signWith(HS256, key).compact();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy