Skip to content
Snippets Groups Projects
Commit 99165da3 authored by Manuel Spuhler's avatar Manuel Spuhler
Browse files

New JWT implementation

parent 762c9f5b
No related branches found
No related tags found
No related merge requests found
...@@ -294,9 +294,9 @@ ...@@ -294,9 +294,9 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.jsonwebtoken</groupId> <groupId>com.auth0</groupId>
<artifactId>jjwt</artifactId> <artifactId>java-jwt</artifactId>
<version>0.9.1</version> <version>3.8.3</version>
</dependency> </dependency>
</dependencies> </dependencies>
......
package eu.hbp.mip.controllers; package eu.hbp.mip.controllers;
import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE; import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;
import java.security.Key;
import java.util.Date;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import eu.hbp.mip.model.UserInfo;
import org.slf4j.Logger; import org.slf4j.Logger;
import eu.hbp.mip.model.User;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import io.jsonwebtoken.*; import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.JWT;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
...@@ -24,35 +19,17 @@ public class JWTApi { ...@@ -24,35 +19,17 @@ public class JWTApi {
private static final Logger LOGGER = LoggerFactory.getLogger(JWTApi.class); private static final Logger LOGGER = LoggerFactory.getLogger(JWTApi.class);
@Autowired
private UserInfo userInfo;
@ApiOperation(value = "Create a JSON Web Token", response = String.class) @ApiOperation(value = "Create a JSON Web Token", response = String.class)
@RequestMapping(method = RequestMethod.POST) @RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> createJWT() { public ResponseEntity<String> createJWT() {
LOGGER.info("Create a JSON Web Token"); LOGGER.info("Create a JSON Web Token");
// SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; Algorithm algorithm = Algorithm.HMAC512("secret");
// String apiKey = "6v2oxpJMzU14U-dqVireln5AUKTtx5fBPSEgaBZiI983d98cfa6"; String token = JWT.create()
// byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(apiKey); .withIssuer("mip.humanbrainproject.eu")
// Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName()); .withSubject("subj")
.sign(algorithm);
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
User user = userInfo.getUser();
// Set the JWT Claims
JwtBuilder builder = Jwts.builder().setIssuedAt(now).setIssuer("mip.humanbrainproject.eu")
.setSubject(user.getEmail()).signWith(SignatureAlgorithm.HS512,
"hbpSecret");
String token = builder.compact();
LOGGER.info(token);
// long expMillis = nowMillis + 86400 * 24;
// Date exp = new Date(expMillis);
// builder.setExpiration(exp);
return ResponseEntity.status(HttpStatus.CREATED).body(token); return ResponseEntity.status(HttpStatus.CREATED).body(token);
} }
......
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