gpt4 book ai didi

java - 使用 Apache CXF 获取 Java 中 Azure Pack 的安全 token

转载 作者:太空宇宙 更新时间:2023-11-04 13:19:53 25 4
gpt4 key购买 nike

我正在尝试用 Java 编写代码,以便从 Azure Pack 的 STS 获取安全 token ,然后我可以使用该 token 来验证对 Azure Pack API 的调用。这是example code that Microsoft provides (有效)在 C# 中获取此 token :

        string windowsAuthSiteEndPoint = EnvironmentToUse + ":30072";
var identityProviderEndpoint = new EndpointAddress(new Uri(windowsAuthSiteEndPoint + "/wstrust/issue/windowstransport"));
var identityProviderBinding = new WS2007HttpBinding(SecurityMode.Transport);
identityProviderBinding.Security.Message.EstablishSecurityContext = false;
identityProviderBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;
identityProviderBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;

var trustChannelFactory = new WSTrustChannelFactory(identityProviderBinding, identityProviderEndpoint)
{
TrustVersion = TrustVersion.WSTrust13,
};

var channel = trustChannelFactory.CreateChannel();
var rst = new RequestSecurityToken(RequestTypes.Issue)
{
AppliesTo = new EndpointReference("http://azureservices/AdminSite"),
KeyType = KeyTypes.Bearer,
};

RequestSecurityTokenResponse rstr = null;
SecurityToken token = null;
token = channel.Issue(rst, out rstr);

这是我目前在 Java 中拥有的内容,我正在尝试做同样的事情:

    import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.sts.STSConstants;
import org.apache.cxf.ws.security.SecurityConstants;
import org.apache.cxf.ws.security.tokenstore.SecurityToken;
import org.apache.cxf.ws.security.trust.STSClient;

SpringBusFactory springBusFactory = new SpringBusFactory();
Bus bus = springBusFactory.createBus();

STSClient stsClient = new STSClient(bus);
stsClient.setLocation("https://" + endpoint + ":30072/wstrust/issue/windowstransport");
stsClient.setServiceName("{http://schemas.microsoft.com/ws/2008/06/identity/securitytokenservice}SecurityTokenService");
stsClient.setEndpointName("{http://schemas.microsoft.com/ws/2008/06/identity/securitytokenservice}WS2007HttpBinding_IWSTrust13Sync");
stsClient.setKeyType(STSConstants.BEARER_KEY_KEYTYPE);
stsClient.isEnableAppliesTo();

bus.setProperty(SecurityConstants.STS_CLIENT, stsClient);
bus.setProperty(SecurityConstants.STS_APPLIES_TO, "http://azureservices/AdminSite");

SecurityToken securityToken = stsClient.requestSecurityToken();

运行 Java 测试代码时收到 401 Unauthorized HTTP 响应:

    Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '401: Unauthorized' when communicating with https://endpoint:30072/wstrust/issue/windowstransport

在尝试重新创建 C# 代码的功能时,我似乎缺少以下功能,但我无法弄清楚以下代码在 Java/使用 Apache CXF 库中的等效内容:

1)identityProviderBinding.Security.Message.EstablishSecurityContext = false;

2) IdentityProviderBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;

3) IdentityProviderBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;

也有可能我也做错了其他事情。有什么想法或建议吗?

最佳答案

您是否尝试过使用管理证书而不是安全 token 来验证您的请求。 https://msdn.microsoft.com/en-us/library/azure/ee460782.aspx#bk_cert有关于如何在 Azure 中执行此操作的信息,但对于 Azure Pack 应该没有太大差异。

关于java - 使用 Apache CXF 获取 Java 中 Azure Pack 的安全 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33177339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com