【问题标题】:Setting custom rest-authenticator in Activiti BPM rest-webapp在 Activiti BPM rest-webapp 中设置自定义 rest-authenticator
【发布时间】:2014-05-12 15:40:53
【问题描述】:

我们想在 Activiti BPM REST-API 5.15 中实现 SSO。我完全按照 Activiti 文档的描述禁用基本身份验证的构建:http://www.activiti.org/userguide/#N12F8B 目标是用我们自己的 SSO-Logic 替换 REST-API 的内置基本身份验证。

因此,我们需要禁用内置的基本身份验证。为了实现这一点,我创建了一个 org.activiti.rest.service.application.ActivitiRestServicesApplication 的子类,它实现了自定义 org.activiti.rest.common.filter.RestAuthenticator 接口的 boolean requestRequiresAuthentication(Request request) 方法。理论上总是返回 false 会禁用基本身份验证。另外,我看了这篇文章:http://forums.activiti.org/content/issue-custom-restauthenticator-using-rest-513

这是我的课:

package org.activiti.rest.service.application;

import org.restlet.Request;
import org.restlet.data.Form;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.activiti.engine.identity.User;
import org.activiti.engine.impl.identity.Authentication;
import org.apache.commons.codec.binary.Base64;

import java.security.Key;
import java.security.MessageDigest;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Date;

import org.activiti.rest.common.api.ActivitiUtil;
import org.activiti.rest.common.filter.RestAuthenticator;

public class CustomActivitiRestServicesApplication extends ActivitiRestServicesApplication implements RestAuthenticator {

    protected String ltpaKey;
    protected String ltpaPassword;

    private static final String AES_DECRIPTING_ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String DES_DECRIPTING_ALGORITHM = "DESede/ECB/PKCS5Padding";
    private static final String LTPA_COOKIE_NAME = "LtpaToken2";
    String ltpaToken = null;

    @Override
    public boolean requestRequiresAuthentication(Request request) {

           //LTPA-Encrypt-Logic
          //Authentication.setAuthenticatedUserId(user.getId());
        return false;
    }

    @Override
    public boolean isRequestAuthorized(Request request) {
        // TODO Auto-generated method stub
        return false;
    }
}

此外,我更改了 activiti-webapp-rest2 的 web.xml,它指向我的自定义实现:

<!-- Restlet adapter -->  
  <servlet>  
    <servlet-name>RestletServlet</servlet-name>  
    <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    <init-param>
      <!-- Application class name -->
      <param-name>org.restlet.application</param-name>
      <param-value>org.activiti.rest.service.application.CustomActivitiRestServicesApplication</param-value>
    </init-param>
  </servlet>

问题是,这没有任何效果。重新部署后,rest-api 仍然希望拥有基本凭据,我不知道,为什么。

感谢任何回复。我用谷歌搜索了很多,但没有成功。

更新:也许,这个类可能有助于设置自定义 REST-Authenticator: org.activiti.rest.common.application.ActivitiRestApplication

你可以在那里找到方法:

// Set authenticator as a NON-optional filter. If certain request require no authentication, a custom RestAuthenticator
// should be used to free the request from authentication.
authenticator = new ChallengeAuthenticator(null, true, ChallengeScheme.HTTP_BASIC,
      "Activiti Realm") {

  @Override
  protected boolean authenticate(Request request, Response response) {

    // Check if authentication is required if a custom RestAuthenticator is set
    if(restAuthenticator != null && !restAuthenticator.requestRequiresAuthentication(request)) {
      return true;
    }

    if (request.getChallengeResponse() == null) {
      return false;
    } else {
      boolean authenticated = super.authenticate(request, response);
      if(authenticated && restAuthenticator != null) {
        // Additional check to see if authenticated user is authorised. By default, when no RestAuthenticator
        // is set, a valid user can perform any request.
        authenticated = restAuthenticator.isRequestAuthorized(request);
      }
      return authenticated;
    }
  }
};
authenticator.setVerifier(verifier);

}

但我仍然不明白如何“设置”我的自定义休息身份验证器。 任何帮助都非常受欢迎 非常感谢,本

【问题讨论】:

    标签: java rest authentication activiti business-process-management


    【解决方案1】:

    我相信您的问题是您仍然在 createInboundRoot() 方法中定义了身份验证过滤器(因为它看起来好像您没有覆盖该方法)。

    @覆盖 公共同步 Restlet createInboundRoot() { 初始化验证();

    Router router = new Router(getContext());
    router.attachDefault(DefaultResource.class);
    RestServicesInit.attachResources(router);
    
    JsonpFilter jsonpFilter = new JsonpFilter(getContext());
    **authenticator.setNext(jsonpFilter);
    jsonpFilter.setNext(router);**
    
    // Get hold of JSONConverter and enable ISO-date format by default
    List<ConverterHelper> registeredConverters = Engine.getInstance().getRegisteredConverters();
    for(ConverterHelper helper : registeredConverters) {
      if(helper instanceof JacksonConverter) {
        JacksonConverter jacksonConverter = (JacksonConverter) helper;
        jacksonConverter.getObjectMapper().configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
      }
    }
    **return authenticator;**
    

    }

    您应该删除身份验证过滤器,或者更好的是,创建您自己的过滤器以与 Websphere 一起使用(因为您使用的是 LTPA 令牌,我假设您使用 Websphere 作为容器)。

    如果我能提供任何进一步的帮助,请告诉我。

    谢谢, 格雷格·哈雷 - BP3

    【讨论】:

    • 感谢您的回答。在 activiti 论坛上,一位开发人员写道:“RestAuthenticator 的自定义实现可以在 org.activiti.rest.common.application.ActivitiRestApplication(或 org.activiti.rest.service.application.ActivitiRestServicesApplication)的实例上设置。”我假设我可以在不实现新过滤器的情况下设置自己的 RestAuthenticator。但是我不知道怎么做。当我理解正确时,我只需在某个类中设置我的自定义实现。
    • 嗨,Ben,毫无疑问,您可以在 ActivitiRestServiceApplication 中定义您的身份验证机制,但即使您这样做了,您也需要删除身份验证过滤器路由。
    • 嗨,格雷格,好的。因此,当我正确理解您时,注释掉 authenticationator.setNext(jsonpFilter); 行就足够了。 jsonpFilter.setNext(路由器);并返回验证器; ?然后创建上面提到的类并修改web.xml指向这个类?
    • 您真正需要的是返回 jsonpFilter ,因为这将绕过身份验证过滤器。不过,我建议进行更彻底的清理。
    • 我只是简单地返回了 jsonpFilter 而没有更改公共同步 Restlet createInboundRoot() 中的其他内容。部署后,REST-API 仍然希望拥有基本凭据(“Activiti Realm”),不同之处在于现在使用更改后的代码,不会接受任何用户名和密码对......
    【解决方案2】:

    我是这样解决的:

    1.) 创建实现方法 public boolean requestRequiresAuthentication(Request request) 的类 org.activiti.rest.common.filter.RestAuthenticatorImpl.java

    2.) 在 org.activiti.rest.common.application.ActivitiRestApplication.java 中设置您的自定义 RestAuthenticator,如下所示:

    在 Constructor public ActivitiRestApplication() 中,添加以下代码行:

    restAuthenticator = new RestAuthenticatorImpl();
    setRestAuthenticator(restAuthenticator);
    

    格雷格,感谢您的建议!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      相关资源
      最近更新 更多