generate token with cookie and json response
This commit is contained in:
parent
402a3100d0
commit
19c99ade07
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
|
||||
@Path("generate")
|
||||
@Produces("text/plain")
|
||||
public Response getUserName(@CookieParam("jwt") String jwtCookie) throws ParseException {
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getUserName(@CookieParam("jwt") String jwtCookie) {
|
||||
Response response = null;
|
||||
if (jwtCookie == null) {
|
||||
// Create a JWT token signed using the 'HS256' algorithm
|
||||
String newJwtCookie = Jwt.upn("Alice").signWithSecret(secret);
|
||||
// or create a JWT token encrypted using the 'A256KW' algorithm
|
||||
// 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 {
|
||||
// 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);
|
||||
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