我有一個加密字符串由客戶端發送。我試圖攔截字符串使用ContainerRequestFilter然後解密它並再次設置InputStream,以便它可以使用Jackson映射到一個POJO。處理ContainerRequestFilter的InputStream
插圖:
我的資源
@Path("auth")
public class AuthResource {
@POST
public Response testResource(@Auth AuthUser auth, Person person) {
System.out.println("Recieved Resource:: "+ new Gson().toJson(person));
return null;
}
}
Person.java
public class Person {
private String name;
private int age;
public Person() {};
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
我篩選
@Provider
public class MyFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
InputStream inputStream = requestContext.getEntityStream();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, "UTF-8");
String theString = writer.toString();
String decryptedMessage = "";
try {
decryptedMessage = JwtToken.decryptPayload(theString);
System.err.println("Decrypted Message: "+decryptedMessage);
} catch (Exception e) {
e.printStackTrace();
}
InputStream stream = new ByteArrayInputStream(decryptedMessage.getBytes(StandardCharsets.UTF_8));
requestContext.setEntityStream(stream);
}
}
我明白,一旦被利用的InputStream它不能使用再次。但是使用requestContext.setEntityStream(stream);我正在嘗試再次設置InputStream以供Jackson使用。
儘管如此,我仍然無法獲取我的資源中的人物。解密工作正常,因爲我使用調試器進行了測試。
我收到以下錯誤:415:不支持的媒體類型
編輯1:我使用Adavanced REST客戶端打網址
頭:
authorization: Basic ZXlKaGRYUm9iM0pwZW1GMGFXOXVJam9pWVcxcGRDSXNJbUZzWnlJNklraFRNalUySW4wLmUzMC5MLUtmOUxQNjFSQ21Bektob2lTR0V4bEJ3RXRUMXhrR3A3bUpIZmFSeV9FOnBhc3M=
原始負載:
eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiQW1pdCIsImFnZSI6MjJ9.-rO6yhYJ--3ZzVCaHFw1hF-s533foYY6vVAuyRh3Q9g
的有效載荷進行加密只需使用JWT:
Jwts.builder().setPayload(new Gson().toJson(new Person("Amit",22))).signWith(SignatureAlgorithm.HS256, key).compact();
請更新您的問題與您如何擊中網址。你在捲曲嗎?您是否將標題設置爲接受JSON作爲請求(默認爲純文本)。另外,你的資源並不能說明它的期望值(我認爲當你使用jackson時默認爲json,但這只是一個假設)。 – pandaadb
嘗試添加到您的標題:「Content-Type:application/json」 – pandaadb
@pandaadb Content-Type設置爲text/plain。但我不認爲這是這個問題。這個問題與我認爲的InputStream有關。 – amitection