java - Customize Spring Oauth Error -
i've customize spring oauth 2 response. :
{ "error": "invalid_token", "error_description": "invalid access token: invalid" }
but need additional data like:
{ "status": -5, "error": "invalid_token", "error_description": "invalid access token: invalid", "errors":[ {"message":"clear message customer"} ] }
how customize requested message?
i tried this:
my spring servlet xml:
<bean id="oauthaccessdeniedhandler" class="org.springframework.security.oauth2.provider.error.oauth2accessdeniedhandler" > <property name="exceptiontranslator" ref="oautherrorhandler" /> </bean> <bean id="oautherrorhandler" class="com.mypath.handler.oautherrorhandler"/>
my translator:
import org.springframework.http.httpheaders; import org.springframework.http.responseentity; import org.springframework.security.oauth2.common.exceptions.oauth2exception; import org.springframework.security.oauth2.provider.error.defaultwebresponseexceptiontranslator; import org.springframework.security.oauth2.provider.error.webresponseexceptiontranslator; public class oautherrorhandler extends defaultwebresponseexceptiontranslator implements webresponseexceptiontranslator { @override public responseentity<oauth2exception> translate(exception e){ responseentity<oauth2exception> responseentity = null; try { responseentity = super.translate(e); } catch (exception e1) { e1.printstacktrace(); } oauth2exception body = responseentity.getbody(); body.addadditionalinformation("status", "-5"); body.addadditionalinformation("errors", "[{\"message\":\""+body.getlocalizedmessage()+"\"}]"); httpheaders headers = new httpheaders(); headers.setall(responseentity.getheaders().tosinglevaluemap()); return new responseentity<>(body, headers, responseentity.getstatuscode()); } }
but response still same
i've solved doing extending oauth2exception , using custom json serializer , deserializer
@org.codehaus.jackson.map.annotate.jsonserialize(using = oauth2exceptionjackson1serializer.class) @org.codehaus.jackson.map.annotate.jsondeserialize(using = oauth2exceptionjackson1deserializer.class) @com.fasterxml.jackson.databind.annotation.jsonserialize(using = oauth2exceptionjackson2serializer.class) @com.fasterxml.jackson.databind.annotation.jsondeserialize(using = oauth2exceptionjackson2deserializer.class) public class customoauthexception extends oauth2exception { private static final long serialversionuid = 124661l; public customoauthexception(string msg) { super(msg); } private string oaut_error_code; private int http_error_code; public customoauthexception(string msg, throwable t) { super(msg, t); } @override public int gethttperrorcode() { return this.http_error_code; } public int gethttp_error_code() { return http_error_code; } public void sethttp_error_code(int http_error_code) { this.http_error_code = http_error_code; } @override public string getoauth2errorcode() { return oaut_error_code; } public void setoaut_error_code(string oaut_error_code) { this.oaut_error_code = oaut_error_code; } }
Comments
Post a Comment