You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Prasanna Pati <pr...@persistent.com> on 2018/05/26 18:09:12 UTC

401 Unauthorized Exception restful webservice Linux OS, Tomcat Server

We are trying to access the Lithium Rest Api (its a https site) through Spring Rest Template as below

       `String plainCreds = lswUserName + ":" + lswPassword;

byte[] plainCredsBytes = plainCreds.getBytes();

byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);

String base64Creds = new String(base64CredsBytes);

HttpHeaders headers = new HttpHeaders();

headers.add("Authorization", "Basic " + base64Creds);



HttpEntity<String> request = new HttpEntity<String>(headers);

    ResponseEntity<String> sRawResp = restTemplate.exchange(completeURL.toString(), HttpMethod.GET, request, String.class);'

Above code always gives org.springframework.web.client.HttpClientErrorException: 401 Unauthorized Exception

There were no issues when invoking the same web service through postman, however whenever accessed through java code, getting the above 401 unauthorized exception

Previously I faced this issue with Windows OS, I struggled a lot and got the solution. The Soultion was, In Tomcat/bin folder I have added below entry related to proxy in to catalina.bat file set "JAVA_OPTS=%JAVA_OPTS% -Dhttp.proxyHost=hy**1.*****.co.in -Dhttp.proxyPort=8080 "

This resolved the issue in Windows OS. Now we moved the entire setup to Linux OS and now i get the same error. Here in linux i don't have any proxy set. checked through wget http://www.google.com<http://www.google.com/> (Here no proxy details displayed). In this case how to resolve this issue in Linux when no proxy is set.

Please Help.

DISCLAIMER
==========
This e-mail may contain privileged and confidential information which is the property of Persistent Systems Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Ltd. does not accept any liability for virus infected mails.

Re: 401 Unauthorized Exception restful webservice Linux OS, Tomcat Server

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Prasanna,

On 5/26/18 2:09 PM, Prasanna Pati wrote:
> We are trying to access the Lithium Rest Api (its a https site)
> through Spring Rest Template as below
> 
> `String plainCreds = lswUserName + ":" + lswPassword;
> 
> byte[] plainCredsBytes = plainCreds.getBytes();
> 
> byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
> 
> String base64Creds = new String(base64CredsBytes);
> 
> HttpHeaders headers = new HttpHeaders();
> 
> headers.add("Authorization", "Basic " + base64Creds);
> 
> 
> 
> HttpEntity<String> request = new HttpEntity<String>(headers);
> 
> ResponseEntity<String> sRawResp =
> restTemplate.exchange(completeURL.toString(), HttpMethod.GET,
> request, String.class);'
> 
> Above code always gives
> org.springframework.web.client.HttpClientErrorException: 401
> Unauthorized Exception
> 
> There were no issues when invoking the same web service through
> postman, however whenever accessed through java code, getting the
> above 401 unauthorized exception
> 
> Previously I faced this issue with Windows OS, I struggled a lot
> and got the solution. The Soultion was, In Tomcat/bin folder I have
> added below entry related to proxy in to catalina.bat file set
> "JAVA_OPTS=%JAVA_OPTS% -Dhttp.proxyHost=hy**1.*****.co.in
> -Dhttp.proxyPort=8080 "
> 
> This resolved the issue in Windows OS. Now we moved the entire
> setup to Linux OS and now i get the same error. Here in linux i
> don't have any proxy set. checked through wget
> http://www.google.com<http://www.google.com/> (Here no proxy
> details displayed). In this case how to resolve this issue in Linux
> when no proxy is set.
> 
> Please Help.

If you are getting a 401 response, it's probably not because you can't
make a connection. More likely is that you haven't implemented the
credential-munging properly. Your code looks okay to be, but you may
be missing:

1. The correct character encoding. Is the server passing a "charset"
parameter with the WWW-Authenticate header?

2. Does the username contain a colon? (That's not allowed per spec)

3. Does your Base64.encodeBase64 class implement RFC4648 faithfully?

There are some examples in RFC7617[1] with both plaintext and encoded
credentials. You might want to validate that your implementation
reproduces those examples correctly.

- -chris

[1] https://tools.ietf.org/html/rfc7617
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlsNWgcACgkQHPApP6U8
pFgZPw//X5bm1O6vUGM+fVzSIExQjRqVHU69IWiI2DC+1ygjI+IWTx2wNnPxgMVy
iejon1xmwplcJz2GXuilAFMNpVzioVY8Z40dFjm/eNzJu7NvXL9t0vd8NFRZnmuF
GLXRMTBDvNLeVGgIX5DhDZwJk29ANfwgORuvg5vhgK5HzmR5O+lo8eeLMmh1BUO2
iywoOS06y+GnHOEvSNz4F/3zwOoBRrxqj4Q0dQ0rEWoNBwax0bNWlRDgne8t5dMD
FATiANojwig0yNMjIIs36qHztej0twBL6JAlWdmlE6MxjxPtw3SFYovqPiM0pw6X
e+WV1RnQFYAT4jCHX5D+j3E0WntTuSUZfhaml9j9MN2nGrTv7WlOnjUMdOeZBNNa
CT3mWH2pLYP1y27XDXWIYORPTo05cvIp//7jcTmTcnrEMvymsqij7BykTos6ZUnl
kK/9Q6rLyU5zvtW973wSKOb9K9gfiGIMDJ6/cxFrs/RQOFeazhV9j6oqAzRz7dQp
nTz1L2/ghEexBfB87E5np5QQ83fF5QUAqMMuCHCU4nWxoEU64w1CVuj0crXWfexG
C31fedUG21EUvfsBeDVGxs6IVaesWg1zWUlSlhW93K71E+zGT2kplt5JJM0YwPNC
hesuxVhgrteGx+J/ynGqu7/KNpWwKkuImmbuPIN9spBQvXND7W4=
=6G5J
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org