Search in sources :

Example 1 with JwtResponse

use of io.jsonwebtoken.jjwtfun.model.JwtResponse in project tutorials by eugenp.

the class BaseController method exception.

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({ SignatureException.class, MalformedJwtException.class, JwtException.class })
public JwtResponse exception(Exception e) {
    JwtResponse response = new JwtResponse();
    response.setStatus(JwtResponse.Status.ERROR);
    response.setMessage(e.getMessage());
    response.setExceptionType(e.getClass().getName());
    return response;
}
Also used : JwtResponse(io.jsonwebtoken.jjwtfun.model.JwtResponse) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 2 with JwtResponse

use of io.jsonwebtoken.jjwtfun.model.JwtResponse in project tutorials by eugenp.

the class DynamicJWTController method dynamicBuilderSpecific.

@RequestMapping(value = "/dynamic-builder-specific", method = POST)
public JwtResponse dynamicBuilderSpecific(@RequestBody Map<String, Object> claims) throws UnsupportedEncodingException {
    JwtBuilder builder = Jwts.builder();
    claims.forEach((key, value) -> {
        switch(key) {
            case "iss":
                ensureType(key, value, String.class);
                builder.setIssuer((String) value);
                break;
            case "sub":
                ensureType(key, value, String.class);
                builder.setSubject((String) value);
                break;
            case "aud":
                ensureType(key, value, String.class);
                builder.setAudience((String) value);
                break;
            case "exp":
                ensureType(key, value, Long.class);
                builder.setExpiration(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
                break;
            case "nbf":
                ensureType(key, value, Long.class);
                builder.setNotBefore(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
                break;
            case "iat":
                ensureType(key, value, Long.class);
                builder.setIssuedAt(Date.from(Instant.ofEpochSecond(Long.parseLong(value.toString()))));
                break;
            case "jti":
                ensureType(key, value, String.class);
                builder.setId((String) value);
                break;
            default:
                builder.claim(key, value);
        }
    });
    builder.signWith(SignatureAlgorithm.HS256, secretService.getHS256SecretBytes());
    return new JwtResponse(builder.compact());
}
Also used : JwtBuilder(io.jsonwebtoken.JwtBuilder) JwtResponse(io.jsonwebtoken.jjwtfun.model.JwtResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

JwtResponse (io.jsonwebtoken.jjwtfun.model.JwtResponse)2 JwtBuilder (io.jsonwebtoken.JwtBuilder)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1