Whenever we are calling Liferay JSON web services we can
find Authenticated Access required.
Even we send user name password still we can see the above
exception. Generally this kind of exception we can when use apache http client
to call Liferay JSON web services.
Generally this exception will encounter due to following reasons
- If the Liferay Web services not enable to Guest user Access
- When permission checker object is Null
- When users not sign in the permission checker
The following is Liferay portal java class you can find the code
public class AuthenticatedAccessControlPolicy extends
BaseAccessControlPolicy {
@Override
public void
onServiceRemoteAccess(
Method
method, Object[] arguments,
AccessControlled
accessControlled)
throws
SecurityException {
PermissionChecker
permissionChecker =
PermissionThreadLocal.getPermissionChecker();
if
(!accessControlled.guestAccessEnabled() &&
((permissionChecker
== null) || !permissionChecker.isSignedIn())) {
throw
new SecurityException("Authenticated access required");
}
}
}
|
Even we use Admin User Name and Credentials to access these
web services we can see Authenticated Access required.
Solution:
We need to use Basic
Authorization heard in the URL
request so that we can successfully call the Liferay JSON web services.
We need to set header to httpPost or HttpGet
Request so that we can call these web services. When we set Authorization header basic then we need to encode credentials
with Base64 encoding format
Example:
Base64 b = new Base64();
String encoding = b.encodeAsString(new
String("test@liferay.com:test").getBytes());
|
Set Basic Authorization header to Http Post/Http Get request as
follows
HttpPost post = new
HttpPost("/LiferayJSONWebservices-portlet/api/jsonws/employee/get-employee");
Base64 b = new Base64();
String encoding = b.encodeAsString(new
String("test@liferay.com:test").getBytes());
post.setHeader("Authorization",
"Basic " + encoding);
|
Consume Liferay
Plugin Portlet JSON web services Http Client
package com.meera.liferay;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import
org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.
import
org.apache.http.message.BasicNameValuePair;
import
org.apache.http.protocol.BasicHttpContext;
public class LiferayWebserviceClient {
public static void main(String[] args) throws
ClientProtocolException,
IOException
{
ArrayList<HashMap<String,
String>> mylist = new ArrayList<HashMap<String, String>>();
HttpHost
targetHost = new HttpHost("localhost", 8080, "http");
BasicHttpContext
ctx = new BasicHttpContext();
// Plugin Context Use for Liferay
6.1
HttpPost
post = new HttpPost("/LiferayJSONWebservices-portlet/api/jsonws/employee/get-employee");
Base64
b = new Base64();
String
encoding = b.encodeAsString(new String("test@liferay.com:test").getBytes());
post.setHeader("Authorization", "Basic
" + encoding);
List<NameValuePair>
params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("emplyeeId", "30722"));
UrlEncodedFormEntity
entity = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(entity);
HttpResponse
resp = httpclient.execute(targetHost, post, ctx);
resp.getEntity().writeTo(System.out);
httpclient.getConnectionManager().shutdown();
}
}
|
Consume Liferay Portal
Services Using Http Client
package com.meera.liferay;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import
org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import
org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.
import org.apache.http.protocol.BasicHttpContext;
public class LiferayWebserviceClient {
public static void main(String[] args) throws
ClientProtocolException, IOException {
HttpHost
targetHost = new HttpHost("localhost", 8080, "http");
BasicHttpContext
ctx = new BasicHttpContext();
// Plugin Context Use for Liferay
6.1
HttpPost
post = new HttpPost("/api/jsonws/country/get-countries");
Base64
b = new Base64();
String encoding = b.encodeAsString(new String("test@liferay.com:test").getBytes());
post.setHeader("Authorization", "Basic " + encoding);
List<NameValuePair>
params = new ArrayList<NameValuePair>();
//params.add(new
BasicNameValuePair("emplyeeId", "30722"));
UrlEncodedFormEntity
entity = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(entity);
HttpResponse
resp = httpclient.execute(targetHost, post, ctx);
resp.getEntity().writeTo(System.out);
httpclient.getConnectionManager().shutdown();
}
}
|
Required Jar Files
commons-codec.jar
httpclient.jar
httpcore.jar
commons-logging.jar
|
Download Example
and Required Jar files