RE: [DXP] How to allow guest access to custom service builder method?

Zak Thompson, modified 7 Years ago. Junior Member Posts: 70 Join Date: 6/13/16 Recent Posts
I am trying to call a custom service builder remote method via HTTP as a non-authenticated user, but I always get the following error:

"Access denied to com.client-name.speedtest.service.SpeedTestResultService#addSpeedTestResult"

I have tried adding the "@AccessControlled(guestAccessEnabled = true)" header to both the remote impl class and remote impl method, and neither has done anything.  

What steps do I need to take to allow for unauthenticated access against my custom service builder entity?

Remote Impl Class:
@AccessControlled(guestAccessEnabled = true)
public class SpeedTestResultServiceImpl extends SpeedTestResultServiceBaseImpl {

   @AccessControlled(guestAccessEnabled = true)
   public SpeedTestResult addSpeedTestResult(final double download, final double upload) {

      return this.speedTestResultLocalService.addSpeedTestResult(download, upload);

   }
}


Local Impl Class:
public class SpeedTestResultLocalServiceImpl extends SpeedTestResultLocalServiceBaseImpl {

   public SpeedTestResult addSpeedTestResult(final double download, final double upload) {

      final SpeedTestResult result = createSpeedTestResult(CounterLocalServiceUtil.increment(SpeedTestResult.class.getName()));

      result.setDate(new Date());

      result.setDownloadSpeed(download);

      result.setUploadSpeed(upload);

      result.persist();

      return result;
   }
}


Calling the service from javascript:​​​​​​​
Liferay.Service(
   '/speedtest.speedtestresult/add-speed-test-result',
   {
      download: parseFloat(testResult.download),
      upload: parseFloat(testResult.upload)
   },
   function(obj) {
      console.log(obj);
   }
);
thumbnail
Minhchau Dang, modified 7 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
Zak Thompson

I have tried adding the "@AccessControlled(guestAccessEnabled = true)" header to both the remote impl class and remote impl method, and neither has done anything.

Making sure, did you re-run service builder after adding the annotation? Because the method invocation happens on Spring proxies, it needs to exist on the interface for the annotation lookup in AccessControlAdvice to be able to find it.
Zak Thompson, modified 7 Years ago. Junior Member Posts: 70 Join Date: 6/13/16 Recent Posts
Minhchau Dang
Zak Thompson

I have tried adding the "@AccessControlled(guestAccessEnabled = true)" header to both the remote impl class and remote impl method, and neither has done anything.

Making sure, did you re-run service builder after adding the annotation? Because the method invocation happens on Spring proxies, it needs to exist on the interface for the annotation lookup in AccessControlAdvice to be able to find it.
Hi Minhchau,

I did re-run the service builder after adding the annotations. I ran it again just to be safe, but it still did not work after rebuilding the services and re-deploying. Here is the definition of the service interface class:​​​​​​​
@AccessControlled
@JSONWebService
@OSGiBeanProperties(property =  {
   "json.web.service.context.name=speedtest", "json.web.service.context.path=SpeedTestResult"}, service = SpeedTestResultService.class)
@ProviderType
@Transactional(isolation = Isolation.PORTAL, rollbackFor =  {
   PortalException.class, SystemException.class})
public interface SpeedTestResultService extends BaseService {

   @AccessControlled(guestAccessEnabled = true)
   public SpeedTestResult addSpeedTestResult(double download, double upload);

   /**
   * Returns the OSGi service identifier.
   *
   * @return the OSGi service identifier
   */
   public String getOSGiServiceIdentifier();
}
thumbnail
Suresh Yadagiri, modified 6 Years ago. Junior Member Posts: 29 Join Date: 3/24/14 Recent Posts
Hi , Did you try creating new Service Access Policy on control panel -> Configuration?  We can define list of methods to be public from service class.