example java json web token jwt

jwt login example java



Biblioteca JWT(JSON Web Token) para Java (9)

Al consultar https://jwt.io/ puede encontrar implementaciones de jwt en muchos idiomas, incluido java . También el sitio proporciona alguna comparación entre estas implementaciones (los algoritmos que soportan y ....).

Para java estas son bibliotecas mencionadas:

Estoy trabajando en una aplicación web desarrollada usando Java y AngularJS y elegí implementar autenticación y autorización de tokens. Para el propósito del ejercicio, he llegado al punto en el que envío las credenciales al servidor, genero un token store al azar y se lo envío al cliente. En cada solicitud al servidor, adjunto el token en el encabezado y funciona perfectamente. Para el punto de vista de autenticación es perfecto y no necesitaría más.

Sin embargo, ahora quiero hacer un seguimiento del tipo de usuario (administrador, usuario habitual ...), así como su id o cualquier otro campo único; como entendí, tengo que encriptar eso en el token que estoy enviando al cliente durante la acción de inicio de sesión. ¿Es eso correcto?

¿Hay alguna biblioteca JWT que haya utilizado y puede generar, encriptar y desencriptar dichos tokens? Un enlace a la API de la biblioteca y la dependencia de Maven sería muy apreciado.

Gracias



Esta biblioteca parece funcionar bien: https://code.google.com/p/jsontoken/ .

Depende de Google Guava. Estos son los artefactos de Maven:

<dependency> <groupId>com.googlecode.jsontoken</groupId> <artifactId>jsontoken</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency>

La biblioteca de hecho es utilizada por Google Wallet.

Aquí es cómo crear un jwt, y para verificarlo y deserializarlo:

import java.security.InvalidKeyException; import java.security.SignatureException; import java.util.Calendar; import java.util.List; import net.oauth.jsontoken.JsonToken; import net.oauth.jsontoken.JsonTokenParser; import net.oauth.jsontoken.crypto.HmacSHA256Signer; import net.oauth.jsontoken.crypto.HmacSHA256Verifier; import net.oauth.jsontoken.crypto.SignatureAlgorithm; import net.oauth.jsontoken.crypto.Verifier; import net.oauth.jsontoken.discovery.VerifierProvider; import net.oauth.jsontoken.discovery.VerifierProviders; import org.apache.commons.lang3.StringUtils; import org.bson.types.ObjectId; import org.joda.time.DateTime; import com.google.common.collect.Lists; import com.google.gson.JsonObject; /** * Provides static methods for creating and verifying access tokens and such. * @author davidm * */ public class AuthHelper { private static final String AUDIENCE = "NotReallyImportant"; private static final String ISSUER = "YourCompanyOrAppNameHere"; private static final String SIGNING_KEY = "LongAndHardToGuessValueWithSpecialCharacters@^($%*$%"; /** * Creates a json web token which is a digitally signed token that contains a payload (e.g. userId to identify * the user). The signing key is secret. That ensures that the token is authentic and has not been modified. * Using a jwt eliminates the need to store authentication session information in a database. * @param userId * @param durationDays * @return */ public static String createJsonWebToken(String userId, Long durationDays) { //Current time and signing algorithm Calendar cal = Calendar.getInstance(); HmacSHA256Signer signer; try { signer = new HmacSHA256Signer(ISSUER, null, SIGNING_KEY.getBytes()); } catch (InvalidKeyException e) { throw new RuntimeException(e); } //Configure JSON token JsonToken token = new net.oauth.jsontoken.JsonToken(signer); token.setAudience(AUDIENCE); token.setIssuedAt(new org.joda.time.Instant(cal.getTimeInMillis())); token.setExpiration(new org.joda.time.Instant(cal.getTimeInMillis() + 1000L * 60L * 60L * 24L * durationDays)); //Configure request object, which provides information of the item JsonObject request = new JsonObject(); request.addProperty("userId", userId); JsonObject payload = token.getPayloadAsJsonObject(); payload.add("info", request); try { return token.serializeAndSign(); } catch (SignatureException e) { throw new RuntimeException(e); } } /** * Verifies a json web token''s validity and extracts the user id and other information from it. * @param token * @return * @throws SignatureException * @throws InvalidKeyException */ public static TokenInfo verifyToken(String token) { try { final Verifier hmacVerifier = new HmacSHA256Verifier(SIGNING_KEY.getBytes()); VerifierProvider hmacLocator = new VerifierProvider() { @Override public List<Verifier> findVerifier(String id, String key){ return Lists.newArrayList(hmacVerifier); } }; VerifierProviders locators = new VerifierProviders(); locators.setVerifierProvider(SignatureAlgorithm.HS256, hmacLocator); net.oauth.jsontoken.Checker checker = new net.oauth.jsontoken.Checker(){ @Override public void check(JsonObject payload) throws SignatureException { // don''t throw - allow anything } }; //Ignore Audience does not mean that the Signature is ignored JsonTokenParser parser = new JsonTokenParser(locators, checker); JsonToken jt; try { jt = parser.verifyAndDeserialize(token); } catch (SignatureException e) { throw new RuntimeException(e); } JsonObject payload = jt.getPayloadAsJsonObject(); TokenInfo t = new TokenInfo(); String issuer = payload.getAsJsonPrimitive("iss").getAsString(); String userIdString = payload.getAsJsonObject("info").getAsJsonPrimitive("userId").getAsString(); if (issuer.equals(ISSUER) && !StringUtils.isBlank(userIdString)) { t.setUserId(new ObjectId(userIdString)); t.setIssued(new DateTime(payload.getAsJsonPrimitive("iat").getAsLong())); t.setExpires(new DateTime(payload.getAsJsonPrimitive("exp").getAsLong())); return t; } else { return null; } } catch (InvalidKeyException e1) { throw new RuntimeException(e1); } } } public class TokenInfo { private ObjectId userId; private DateTime issued; private DateTime expires; public ObjectId getUserId() { return userId; } public void setUserId(ObjectId userId) { this.userId = userId; } public DateTime getIssued() { return issued; } public void setIssued(DateTime issued) { this.issued = issued; } public DateTime getExpires() { return expires; } public void setExpires(DateTime expires) { this.expires = expires; } }

Esto se basa en el código aquí: https://developers.google.com/wallet/instant-buy/about-jwts Y aquí: https://code.google.com/p/wallet-online-sample-java/source/browse/src/com/google/wallet/online/jwt/util/WalletOnlineService.java?r=08b3333bd7260b20846d7d96d3cf15be8a128dfa



IETF ha sugerido jose libs en su wiki: http://trac.tools.ietf.org/wg/jose/trac/wiki

Recomiendo usarlos para firmar. No soy Java, pero parece que jose4j parece ser una buena opción. Tiene buenos ejemplos también: https://bitbucket.org/b_c/jose4j/wiki/JWS%20Examples

Actualización: jwt.io proporciona una clara comparación de varias bibliotecas relacionadas con jwt y sus características. ¡Una visita obligada!

Me encantaría saber qué otros desarrolladores de Java prefieren.




Si solo necesita analizar tokens sin encriptar sin firmar, puede usar este código:

boolean parseJWT_2() { String authToken = getToken(); String[] segments = authToken.split("//."); String base64String = segments[1]; int requiredLength = (int)(4 * Math.ceil(base64String.length() / 4.0)); int nbrPaddings = requiredLength - base64String.length(); if (nbrPaddings > 0) { base64String = base64String + "====".substring(0, nbrPaddings); } base64String = base64String.replace("-", "+"); base64String = base64String.replace("_", "/"); try { byte[] data = Base64.decode(base64String, Base64.DEFAULT); String text; text = new String(data, "UTF-8"); tokenInfo = new Gson().fromJson(text, TokenInfo.class); } catch (Exception e) { e.printStackTrace(); return false; } return true; }


https://github.com/networknt/jsontoken

Esta es una bifurcación de google jsontoken original

No se ha actualizado desde el 11 de septiembre de 2012 y depende de algunos paquetes antiguos.

Que he hecho:

Convert from Joda time to Java 8 time. So it requires Java 8. Covert Json parser from Gson to Jackson as I don''t want to include two Json parsers to my projects. Remove google collections from dependency list as it is stopped long time ago. Fix thread safe issue with Java Mac.doFinal call.

Todas las pruebas de unidad existentes pasaron junto con algunos casos de prueba agregados recientemente.

Aquí hay una muestra para generar el token y verificar el token. Para obtener más información, consulte el https://github.com/networknt/light código fuente para conocer su uso.

Soy el autor de ambos jsontoken y Omni-Channel Application Framework.