Confluence Custom Seraph Authenticator中的RequestFactory为null

时间:2017-12-16 13:34:00

标签: atlassian-plugin-sdk

这是我在Atlassian forums

上提出的问题的副本

我一直在尝试使用var csv = require("fast-csv"); var path = require('path'); //Here file implies "example.csv" csv.fromPath(file[0].path, { headers:true //objectMode: true }) .on("json", function (data) { Survey.push(data); }) .on("end", function () { console.log('data is added'); }); 中的Seraph与我的公司一起为SSO制作我自己的自定义身份验证插件(但是我迟迟不会这样做)但我不能这样做似乎进行远程服务呼叫,因为我找不到Confluence

以下是我迄今采取的粗略步骤:

  1. 下载/设置SDK
  2. 创建Confluence插件
  3. 扩展ConfluenceAuthenticator类并实现我自己的逻辑
  4. 现在我试图获得对RequestFactory SAL的引用并尝试过,

    1. 带注释的依赖注入
    2. Dependency injection with contructor
    3. RequestFactory SAL
    4. ComponentLocator xml标记(文档已过期且无效)
    5. 所以,我的问题是,如何在Seraph中进行远程调用component-import?{/ p>

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

      ComponentLocator

      ... 135更多

      这是迄今为止的代码

      定位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)

      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

      但是,就像我说的那样,总是返回null也试过ComponentLocator.getComponent(RequestFactory.class); 并且它也返回null

      修改

      重新阅读Seraph article,我意识到Seraph无法使用依赖注入,并且在寻找其他组件时会有一些注意事项(如PluginSettingsFactory.class)。

      导致

      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);
      

1 个答案:

答案 0 :(得分:0)

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

所以,对于那些看这个的人来说有一些东西:

  1. Seraph需要任何java jar,而不是特定的atlassian-plugin
  2. 您可以使用任何希望与外界沟通的方法
  3. 这样说,您可以使用HttpUrlConnectionJava中的内置版本。它的超级使用但完美。如果有人找到这个帖子,我会放弃在SAL内使用seraph的想法

    这是一个开始的例子:

    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://codereview.stackexchange.com/q/185129/157474

    但是你可以在网上找到很多关于如何使用这个

    的例子