Adapt to catch InvalidJwtException (?)

This commit is contained in:
jeffcheasey88 2023-09-18 12:03:26 +02:00
parent 06ac691747
commit b58934cff8
2 changed files with 10 additions and 8 deletions

View file

@ -24,7 +24,6 @@ public class Client<U extends User> extends Thread{
Context context = null; Context context = null;
try{ try{
String[] headers = reader.readLine().split("\\s"); String[] headers = reader.readLine().split("\\s");
System.out.println(Arrays.toString(headers));
reader.readHeaders(); reader.readHeaders();
RequestType type = RequestType.valueOf(headers[0]); RequestType type = RequestType.valueOf(headers[0]);
@ -33,7 +32,7 @@ public class Client<U extends User> extends Thread{
writer.flush(); writer.flush();
writer.close(); writer.close();
}catch(InvalidJwtException e){ }catch(InvalidJwtException e){
e.printStackTrace(); // e.printStackTrace();
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
if(context != null && context.getResponseCode() == 0){ if(context != null && context.getResponseCode() == 0){
@ -47,17 +46,19 @@ public class Client<U extends User> extends Thread{
if(context != null) logger.setValue(context); if(context != null) logger.setValue(context);
} }
private User isLogin(Router<U> router, RequestType type, HttpReader reader) throws Exception{ private User isLogin(Router<U> router, RequestType type, HttpReader reader) throws InvalidJwtException{
String auth = reader.getHeader("Authorization"); String auth = reader.getHeader("Authorization");
if(auth == null) return null; if(auth == null) return null;
auth = auth.substring(7); auth = auth.substring(7);
try{ try{
return this.router.getUser(auth); return this.router.getUser(auth);
}catch(Exception e){ }catch(InvalidJwtException e){
e.printStackTrace(); e.printStackTrace();
writer.response(401, router.getDefaultHeaders(type)); try{
writer.flush(); writer.response(401, router.getDefaultHeaders(type));
writer.close(); writer.flush();
writer.close();
}catch(Exception ex){}
throw e; throw e;
} }
} }

View file

@ -16,6 +16,7 @@ import org.jose4j.jwk.RsaJwkGenerator;
import org.jose4j.jws.AlgorithmIdentifiers; import org.jose4j.jws.AlgorithmIdentifiers;
import org.jose4j.jws.JsonWebSignature; import org.jose4j.jws.JsonWebSignature;
import org.jose4j.jwt.JwtClaims; import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.jose4j.jwt.consumer.JwtConsumer; import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder; import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.jose4j.lang.JoseException; import org.jose4j.lang.JoseException;
@ -107,7 +108,7 @@ public class Router<U extends User>{
return this; return this;
} }
public U getUser(String token) throws Exception{ public U getUser(String token) throws InvalidJwtException{
return this.userCreator.apply(this.jwtConsumer.processToClaims(token)); return this.userCreator.apply(this.jwtConsumer.processToClaims(token));
} }