java - Manipulate InputStream of ContainerRequestFilter -
i have encrypted string being sent client. trying intercept string using containerrequestfilter decrypt , set inputstream again can used jackson map pojo.
illustration:
my resource
@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; } }
my filter
@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); } }
i understand once inputstream utilized cannot used again. using requestcontext.setentitystream(stream); trying set inputstream again utilized jackson.
inspite of still unable person object in resource. decryption working fine have tested using debugger.
i following error: 415: unsupported media type
edit 1: using adavanced rest client hit url
header:
authorization: basic zxlkagryum9im0pwzw1gmgfxoxvjam9pwvcxcgrdsxnjbuzzwnljnklrafrnaluysw4wlmuzmc5mlutmouxqnjfsq21bektob2ltr0v4bej3rxrumxhrr3a3bupizmfsev9fonbhc3m=
raw payload:
eyjhbgcioijiuzi1nij9.eyjuyw1lijoiqw1pdcisimfnzsi6mjj9.-ro6yhyj--3zzvcahfw1hf-s533foyy6vvauyrh3q9g
the payload encrypted using jwt:
jwts.builder().setpayload(new gson().tojson(new person("amit",22))).signwith(signaturealgorithm.hs256, key).compact();
your request payload not json. it's jwt token contains json encoded base64. it's piece of text. hence, content-type
of request should text/plain
instead of application/json
:
post /api/auth http/1.1 host: example.org content-type: text/plain eyjhbgcioijiuzi1nij9.eyjuyw1lijoiqw1pdcisimfnzsi6mjj9.-ro6yhyj--3zzvcahfw1hf-s533foyy6vvauyrh3q9g
your filter modifies payload of request: filter gets jwt token request payload, gets token payload, decodes token payload json string , sets json string request payload.
after executing of filter, request contain json string , not piece of text. hence, after that, content-type
of request should modified application/json
. achieved following lines:
requestcontext.getheaders().remove(httpheaders.content_type); requestcontext.getheaders().add(httpheaders.content_type, mediatype.application_json);
to ensure filter executed before resource matching, annotate filter @prematching
.
and don't forget annotate resource method @consumes(mediatype.application_json)
.
Comments
Post a Comment