0

这是我在Atlassian 论坛上提出的问题的副本

我一直在尝试与我的公司一起使用Seraphin为 SSO 制作自己的自定义身份验证插件Confluence(并且将在 Jira 中后期这样做),但我似乎无法进行远程服务调用,因为我找不到RequestFactory SAL.

以下是我到目前为止所采取的粗略步骤:

  1. 下载/设置 SDK
  2. 创建 Confluence 插件
  3. 扩展了 ConfluenceAuthenticator 类并实现了我自己的逻辑

现在我正在尝试参考RequestFactory SAL并尝试过,

  1. 带注解的依赖注入
  2. 使用构造函数进行依赖注入
  3. ComponentLocator
  4. component-importxml 标记(文档已过时且不起作用)

所以,我的问题是,我如何才能访问RequestFactory在 Seraph 中进行远程调用的权限?

使用下面的代码时,如果使用ComponentLocator方法它返回 null,而使用依赖注入时它告诉我无法实例化该类

Caused by: com.atlassian.seraph.config.ConfigurationException: Unable to instantiate class 'com.example.confluence.GluuAuthenticator' : java.lang.InstantiationException: com.example.confluence.MyCustomAuthenticator
        at com.atlassian.seraph.config.SecurityConfigImpl.configureClass(SecurityConfigImpl.java:325)
        at com.atlassian.seraph.config.SecurityConfigImpl.configureAuthenticator(SecurityConfigImpl.java:258)
        at com.atlassian.seraph.config.SecurityConfigImpl.<init>(SecurityConfigImpl.java:194)
        at com.atlassian.seraph.config.SecurityConfigFactory.loadInstance(SecurityConfigFactory.java:56)
        ... 131 more
Caused by: java.lang.InstantiationException: com.example.confluence.MyCustomAuthenticator
        at java.lang.Class.newInstance(Class.java:427)
        at com.atlassian.seraph.config.SecurityConfigImpl.configureClass(SecurityConfigImpl.java:320)
        ... 134 more
Caused by: java.lang.NoSuchMethodException: com.example.confluence.MyCustomAuthenticator.<init>()
        at java.lang.Class.getConstructor0(Class.java:3082)
        at java.lang.Class.newInstance(Class.java:412)

... 135 更多

这是到目前为止的代码

定位Confluence 6.4

import com.atlassian.confluence.user.ConfluenceAuthenticator;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.sal.api.net.Request;
import com.atlassian.sal.api.net.RequestFactory;
import com.atlassian.sal.api.net.ResponseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;

public class MyCustomAuthenticator extends ConfluenceAuthenticator {
    private static final Logger log = LoggerFactory.getLogger(ConfluenceAuthenticator.class);
    private static final String CODE_PARAM_NAME = "code";

    @ComponentImport
    private final RequestFactory<?> requestFactory;

    @Inject
    public MyCustomAuthenticator(@ComponentImport RequestFactory<?> requestFactory) {
        this.requestFactory = requestFactory;
    }

    public Principal getUser(HttpServletRequest request, HttpServletResponse response) {
        log.debug("SMAuthenticator::getUser(HttpServletRequest request, HttpServletResponse response)");
        Principal user = super.getUser(request, response);
        String code = request.getParameter(CODE_PARAM_NAME);

        if (user != null) {
            log.debug("User is already logged in: " +
                    ((user.getName() != null) ? user.getName() : "<None>") );
            return (Principal) user;
        }

        if (code == null) {
            log.debug("User is not logged in, and there is no code found in query params, will redirect to gluu");
            return (Principal) user;
        }


        log.info("User is not logged in, but code is found in the query params. Will start the flow to create the user, " +
                "starting with verifying the code: " + code);

        try {
            String s = requestFactory
                    .createRequest(Request.MethodType.GET, "http://scooterlabs.com/echo")
                    .addBasicAuthentication(CLIENT_ID, CLIENT_SECRET, "fooooo")
                    .execute();
            log.info("results: " + s);
        } catch (ResponseException e) {
            e.printStackTrace();
        }

        return (Principal)user;
    }
}

ComponentLocator方法很简单

ComponentLocator.getComponent(RequestFactory.class);

但就像我说的,总是返回 null 也试过PluginSettingsFactory.class了,它也返回 null


编辑

重读Seraph 文章,我意识到 Seraph 无法使用依赖注入,并且在寻找其他组件(如RequestFactory)时有一些注意事项。

这导致

RequestFactory requestFactory = (RequestFactory)ContainerManager
        .getComponent("requestFactory");

但是,现在错误已更改为

com.atlassian.spring.container.ComponentNotFoundException: Failed to find component: No bean named 'requestFactory' is defined

也试过

RequestFactory requestFactory = (RequestFactory)ContainerManager
        .getInstance()
        .getContainerContext()
        .getComponent(RequestFactory.class);

也没有运气,说找不到

我可以得到其他组件,例如使用此处找到的示例,工作正常

SpaceManager spaceManager = (SpaceManager) ContainerManager.getComponent("spaceManager")
4

1 回答 1

0

我设法找到了与外部服务通信的替代解决方案


所以,对于那些看这个的人来说,有几件事:

  1. Seraph 需要任何 java jar,而不是专门的 atlassian-plugin
  2. 您可以使用任何您希望与外界交流的方式

话虽如此,您可以HttpUrlConnectionJava. 它使用起来超级恶心,但效果很好。如果有人找到这个线程,我会放弃使用SALinside ofseraph

这是一个开始的例子:

try
    {
      // create the HttpURLConnection
      url = new URL(desiredUrl);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      
      // just want to do an HTTP GET here
      connection.setRequestMethod("GET");
      
      // uncomment this if you want to write output to this url
      //connection.setDoOutput(true);
      
      // give it 15 seconds to respond
      connection.setReadTimeout(15*1000);
      connection.connect();

      // read the output from the server
      reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      stringBuilder = new StringBuilder();

      String line = null;
      while ((line = reader.readLine()) != null)
      {
        stringBuilder.append(line + "\n");
      }
      return stringBuilder.toString();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw e;
    }

复制/意大利面

https://alvinalexander.com/blog/post/java/how-open-url-read-contents-httpurl-connection-java

但是你可以在网上找到很多关于如何使用它的例子

于 2018-01-17T14:00:24.350 回答