简体   繁体   English

使用Apache CXF为Java中的Azure Pack获取安全令牌

[英]Getting a Security Token for Azure Pack in Java Using Apache CXF

I am trying to write code in Java that can obtain a Security Token from the STS for Azure Pack, which I can then use to authenticate calls to the Azure Pack APIs. 我正在尝试用Java编写代码,以从Azure Pack的STS获取安全令牌,然后将其用于验证对Azure Pack API的调用。 Here is example code that Microsoft provides (which works) for obtaining this token in C#: 这是Microsoft提供的 (适用于)在C#中获取此令牌的示例代码

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

Here is what I currently have in Java, where I am attempting to do the same thing: 这是我目前在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();

I get a 401 Unauthorized HTTP response when running my Java test code: 运行Java测试代码时,我收到401未经授权的HTTP响应:

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

It looks like I'm missing the following pieces of functionality when attempting to recreate what the C# code does, but I can't figure out what the equivalent of the following code would be in Java/using the Apache CXF library: 尝试重新创建C#代码的功能时,似乎缺少以下功能,但是我无法弄清楚Java /使用Apache CXF库中的以下代码等效于什么:

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

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

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

It's also possible I'm doing other things wrong as well. 我也可能在做其他错误的事情。 Any thoughts or suggestions? 有什么想法或建议吗?

Have you tried using management certificates to authenticate your requests instead of security tokens. 您是否尝试过使用管理证书而不是安全令牌来认证您的请求。 https://msdn.microsoft.com/en-us/library/azure/ee460782.aspx#bk_cert has information on how to do it in Azure, but it should not differ much for Azure Pack. https://msdn.microsoft.com/zh-cn/library/azure/ee460782.aspx#bk_cert包含有关如何在Azure中执行此操作的信息,但对于Azure Pack来说应该没有太大区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM