use of com.auth0.jwt.exceptions.JWTCreationException in project litemall by linlinjava.
the class JwtHelper method createToken.
public String createToken(Integer userId) {
try {
Algorithm algorithm = Algorithm.HMAC256(SECRET);
Map<String, Object> map = new HashMap<String, Object>();
Date nowDate = new Date();
// 过期时间:2小时
Date expireDate = getAfterDate(nowDate, 0, 0, 0, 2, 0, 0);
map.put("alg", "HS256");
map.put("typ", "JWT");
String token = JWT.create().withHeader(map).withClaim("userId", userId).withIssuer(ISSUSER).withSubject(SUBJECT).withAudience(AUDIENCE).withIssuedAt(nowDate).withExpiresAt(expireDate).sign(algorithm);
return token;
} catch (JWTCreationException exception) {
exception.printStackTrace();
}
return null;
}
use of com.auth0.jwt.exceptions.JWTCreationException in project Toy by gmoon92.
the class JwtUtils method generate.
public String generate(User user) {
try {
ZonedDateTime today = ZonedDateTime.now();
String token = JWT.create().withIssuer(apiVersion).withClaim("username", user.getUsername()).withClaim("role", user.getRole().name()).withIssuedAt(Date.from(today.toInstant())).withExpiresAt(Date.from(today.plusDays(DAY_OF_EXPIRATION).toInstant())).sign(algorithm);
return String.format("%s %s", AuthenticationSchema.BEARER.getName(), token);
} catch (JWTCreationException e) {
throw new JWTCreationException("Invalid Signing configuration or Couldn't convert Claims.", e);
}
}
use of com.auth0.jwt.exceptions.JWTCreationException in project Toy by gmoon92.
the class JwtUtil method generate.
public String generate(User user) {
try {
ZonedDateTime today = ZonedDateTime.now();
String token = JWT.create().withIssuer(apiVersion).withClaim("username", user.getUsername()).withClaim("role", user.getRole().name()).withIssuedAt(Date.from(today.toInstant())).withExpiresAt(Date.from(today.plusDays(DAY_OF_EXPIRATION).toInstant())).sign(algorithm);
return String.format("%s %s", AuthenticationSchema.BEARER.getName(), token);
} catch (JWTCreationException e) {
throw new JWTCreationException("Invalid Signing configuration or Couldn't convert Claims.", e);
}
}
use of com.auth0.jwt.exceptions.JWTCreationException in project framework by galasa-dev.
the class Authenticate method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Gson gson = new Gson();
Principal principal = req.getUserPrincipal();
if (principal == null) {
// TODO check that it was a basic auth principal to prevent JWT reauthenticating
resp.setStatus(401);
// *** Ability to set the realm
resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
// NOSONAR //TODO catch this as SQ says
resp.getWriter().write("Requires authentication");
return;
}
if (req.isUserInRole("admin")) {
String jwt;
try {
jwt = createJWT(principal.getName(), "admin", FOUR_HOURS_EXPIRE);
} catch (JWTCreationException e) {
resp.setStatus(500);
// *** Ability to set the realm
resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
// NOSONAR //TODO catch this as SQ says
resp.getWriter().write("Token could not be generated");
return;
}
AuthJson auth = new AuthJson();
auth.cps = jwt;
auth.dss = jwt;
auth.ras = jwt;
String json = gson.toJson(auth);
resp.setContentType("application/json");
try {
resp.getWriter().write(json);
} catch (IOException e) {
resp.setStatus(500);
// *** Ability to set the realm
resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
// NOSONAR //TODO catch this as SQ says
resp.getWriter().write("Failed to create json");
return;
}
return;
}
if (req.isUserInRole("user")) {
String jwt;
try {
jwt = createJWT(principal.getName(), "user", FOUR_HOURS_EXPIRE);
} catch (JWTCreationException e) {
resp.setStatus(500);
// *** Ability to set the realm
resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
// NOSONAR //TODO catch this as SQ says
resp.getWriter().write("Token could not be generated");
return;
}
AuthJson auth = new AuthJson();
auth.cps = jwt;
auth.dss = jwt;
auth.ras = jwt;
String json = gson.toJson(auth);
resp.setContentType("application/json");
try {
resp.getWriter().write(json);
} catch (IOException e) {
resp.setStatus(500);
// *** Ability to set the realm
resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
// NOSONAR //TODO catch this as SQ says
resp.getWriter().write("Failed to create json");
return;
}
return;
}
resp.setStatus(401);
// *** Ability to set the realm
resp.addHeader("WWW-Authenticate", "Basic realm=\"Galasa\"");
// NOSONAR
resp.getWriter().write("Does not have the 'user' role");
}
use of com.auth0.jwt.exceptions.JWTCreationException in project framework by galasa-dev.
the class Authenticate method createJWT.
public String createJWT(String subject, String role, long expireDuration) throws JWTCreationException {
Algorithm algorithm = Algorithm.HMAC256(this.configurationProperties.get(SECRET_KEY).toString());
long time = System.currentTimeMillis();
Date dateNow = new Date(time);
Date dateExpire = new Date(time + expireDuration);
String token = JWT.create().withIssuer("galasa").withIssuedAt(dateNow).withSubject(subject).withClaim("role", role).withExpiresAt(dateExpire).sign(algorithm);
return token;
}
Aggregations