java - Consuming Secure RESTful API -
i need build application consumes restful api external company. data consuming sensitive, need https.
my problem able read information outside company without configuring sort of ssl certificate. did not have sort of encryption/decryption in code.
the thing have lead me believe data secure restful call works https
in url , not http
.
i using spring resttemplate consume web services shown below:
list<info> getinfo(map<string, object> urlparammap, httpsession httpsession){ httpentity<string> request = new httpentity<string>(httpsession.gethttpheaders()); resttemplate.setmessageconverters(httpsession.getmessageconverterlist()); responseentity<jsondata> responseentity = resttemplate.exchange("https://outsideorg.com/{parameter}, httpmethod.get, request, jsondata.class, urlparammap); list<info> infolist = responseentity.getbody().getinfolist(); return infolist; }
this of http set up, including authorization , deserialization settings:
public class httpsession { static logger log = logger.getlogger(httpsession.class); private httpheaders httpheaders; private list<httpmessageconverter<?>> messageconverterlist; httpheaders gethttpheaders() { return httpheaders; } void sethttpheaders(httpheaders httpheaders) { this.httpheaders = httpheaders; } list<httpmessageconverter<?>> getmessageconverterlist() { return messageconverterlist; } void setmessageconverterlist(list<httpmessageconverter<?>> messageconverterlist) { this.messageconverterlist = messageconverterlist; } void createheaders(){ httpheaders httpheaders = new httpheaders(); try { file xmlfile = new file("c:/users/user/docs/restauth.xml"); documentbuilderfactory documentbuilderfactory = documentbuilderfactory.newinstance(); documentbuilder documentbuilder = documentbuilderfactory.newdocumentbuilder(); document document = documentbuilder.parse(xmlfile); string username = document.getelementsbytagname("username").item(0).gettextcontent(); string password = document.getelementsbytagname("password").item(0).gettextcontent(); string authorization = username + ":" + password; byte[] encodedauthorization = base64.encodebase64(authorization.getbytes(charset.forname("us-ascii"))); string authorizationheader = "basic " + new string(encodedauthorization); httpheaders.set("authorization", authorizationheader); httpheaders.setaccept(arrays.aslist(mediatype.application_json)); }catch (parserconfigurationexception | saxexception | ioexception e) { log.error("there error setting credentials.", e); } this.httpheaders = httpheaders; } void setupmessageconverters(){ list<httpmessageconverter<?>> messageconverterlist = new arraylist<>(); mappingjackson2httpmessageconverter messageconverter = new mappingjackson2httpmessageconverter(); objectmapper objectmapper = new objectmapper(); objectmapper.enable(deserializationfeature.accept_single_value_as_array, deserializationfeature.accept_empty_string_as_null_object); messageconverter.setobjectmapper(objectmapper); messageconverterlist.add(messageconverter); this.messageconverterlist = messageconverterlist; }
i confused need include ssl certificate, if 1 needed, , encrypt/decrypt data. problem external site returning information seemingly unencrypted without asking ssl certificate?
i new ssl , relatively new rest in general, forgive ignorance on topic. can offered or resources can directed towards appreciated!
the jre contains trust store certificates of public cas. if company uses certificate, signed 1 of trusted cas, jre can establish ssl connection without further configuration. otherwise error message, certificate not trusted.
if specify https url, jre try setup encrypted connection. if doesn't work, exception.
Comments
Post a Comment