我一直在尝试与我的公司一起使用Seraph
in为 SSO 制作自己的自定义身份验证插件Confluence
(并且将在 Jira 中后期这样做),但我似乎无法进行远程服务调用,因为我找不到RequestFactory SAL
.
以下是我到目前为止所采取的粗略步骤:
- 下载/设置 SDK
- 创建 Confluence 插件
- 扩展了 ConfluenceAuthenticator 类并实现了我自己的逻辑
现在我正在尝试参考RequestFactory SAL
并尝试过,
- 带注解的依赖注入
- 使用构造函数进行依赖注入
ComponentLocator
component-import
xml 标记(文档已过时且不起作用)
所以,我的问题是,我如何才能访问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")