我有一个加密字符串由客户端发送。我试图拦截字符串使用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