token #1
36
src/main/java/com/covas/Jwt2.java
Normal file
36
src/main/java/com/covas/Jwt2.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.covas;
|
||||||
|
|
||||||
|
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||||
|
|
||||||
|
@RegisterForReflection
|
||||||
|
public class Jwt2 {
|
||||||
|
|
||||||
|
public String name;
|
||||||
|
public Boolean status;
|
||||||
|
public String message;
|
||||||
|
|
||||||
|
public Jwt2(){
|
||||||
|
name = "";
|
||||||
|
status = true;
|
||||||
|
message = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public Jwt2(String name){
|
||||||
|
this.name = name;
|
||||||
|
status = true;
|
||||||
|
message = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public Jwt2(String name, String message){
|
||||||
|
this.name = name;
|
||||||
|
this.message = message;
|
||||||
|
status = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Jwt2(String name, Boolean status, String message){
|
||||||
|
this.name = name;
|
||||||
|
this.status = status;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -36,20 +36,26 @@ public class TokenRessource {
|
|||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("generate")
|
@Path("generate")
|
||||||
@Produces("text/plain")
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response getUserName(@CookieParam("jwt") String jwtCookie) throws ParseException {
|
public Response getUserName(@CookieParam("jwt") String jwtCookie) {
|
||||||
Response response = null;
|
Response response = null;
|
||||||
if (jwtCookie == null) {
|
if (jwtCookie == null) {
|
||||||
// Create a JWT token signed using the 'HS256' algorithm
|
// Create a JWT token signed using the 'HS256' algorithm
|
||||||
String newJwtCookie = Jwt.upn("Alice").signWithSecret(secret);
|
String newJwtCookie = Jwt.upn("Alice").signWithSecret(secret);
|
||||||
// or create a JWT token encrypted using the 'A256KW' algorithm
|
// or create a JWT token encrypted using the 'A256KW' algorithm
|
||||||
// Jwt.upn("alice").encryptWithSecret(secret);
|
// Jwt.upn("alice").encryptWithSecret(secret);
|
||||||
return Response.ok("Alice").cookie(new NewCookie("jwt", newJwtCookie)).build();
|
|
||||||
|
return Response.status(Response.Status.CREATED).entity(new Jwt2("Alice")).cookie(new NewCookie("jwt", newJwtCookie)).build();
|
||||||
} else {
|
} else {
|
||||||
// All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized.
|
// All mp.jwt and smallrye.jwt properties are still effective, only the verification key is customized.
|
||||||
JsonWebToken jwt = parser.verify(jwtCookie, secret);
|
try {
|
||||||
|
JsonWebToken jwt = parser.verify(jwtCookie, secret);
|
||||||
|
}
|
||||||
|
catch(ParseException p){
|
||||||
|
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(new Jwt2("Alice", false, p.getMessage())).build();
|
||||||
|
}
|
||||||
// or jwt = parser.decrypt(jwtCookie, secret);
|
// or jwt = parser.decrypt(jwtCookie, secret);
|
||||||
return Response.ok(jwt.getName()).build();
|
return Response.status(Response.Status.OK).entity(new Jwt2(jwt.getName())).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user