2016-04-28 54 views
1

我正在开发一个Java客户端,它使用设置为WSO2 Identity Server中的OAuth的入站身份验证动态创建服务提供者。代码去如下使用Java在OAuth工具中使用OAuth工具创建WSO2 Identity Server中的服务提供者

import java.rmi.RemoteException; 
import java.util.HashMap; 
import java.util.Map; 

import org.apache.axis2.client.Options; 
import org.apache.axis2.client.ServiceClient; 
import org.apache.axis2.context.ConfigurationContext; 
import org.apache.axis2.context.ConfigurationContextFactory; 
import org.apache.axis2.transport.http.HTTPConstants; 
import org.apache.axis2.transport.http.HttpTransportProperties; 
import org.wso2.carbon.authenticator.proxy.AuthenticationAdminStub; 
import org.wso2.carbon.um.ws.api.WSRealmBuilder; 
import org.wso2.carbon.um.ws.api.stub.ClaimValue; 
import org.wso2.carbon.user.core.UserRealm; 
import org.wso2.carbon.user.core.UserStoreManager; 
import org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationConfig; 
import org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationRequestConfig; 
import org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider; 
import org.wso2.carbon.identity.application.mgt.stub.IdentityApplicationManagementServiceStub; 

import org.wso2.carbon.identity.oauth.stub.*; 
import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO; 

public class IdentityClient { 


    private final static String SERVER_URL = "https://localhost:9443/services/"; 

    public static void main(String[] args) throws RemoteException, OAuthAdminServiceException {  

     String appName = "Sample_App_3"; 

     System.setProperty("javax.net.ssl.trustStore", "wso2carbon.jks"); 
     System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon"); 

     try { 

      OAuthAdminServiceStub stub = new OAuthAdminServiceStub(null, 
        SERVER_URL + "OAuthAdminService"); 
      IdentityApplicationManagementServiceStub IAMStub = new IdentityApplicationManagementServiceStub(
        null, SERVER_URL + "IdentityApplicationManagementService"); 
      ServiceClient client = stub._getServiceClient(); 
      ServiceClient IAMClient = IAMStub._getServiceClient(); 
      authenticate(client); 

      OAuthConsumerAppDTO consumerApp = new OAuthConsumerAppDTO(); 
      consumerApp.setApplicationName(appName); 
      consumerApp.setOAuthVersion("OAuth-2.0"); 
      consumerApp.setCallbackUrl("http://localhost:8080/playground2/oauth2client"); 
      consumerApp.setGrantTypes(
        "authorization_code implicit password client_credentials refresh_token " 
          + "urn:ietf:params:oauth:grant-type:saml2-bearer iwa:ntlm"); 

      /* OAuthAdminProxy.registerOAuthApplicationData(consumerApp); */ 
      stub.registerOAuthApplicationData(consumerApp); 
      System.out.println("Application created successfully"); 

      authenticate(IAMClient); 

      InboundAuthenticationRequestConfig iaReqConfig = new InboundAuthenticationRequestConfig(); 
      iaReqConfig.setInboundAuthKey(stub.getOAuthApplicationDataByAppName(appName) 
        .getOauthConsumerKey()); 
      iaReqConfig.setInboundAuthType(stub.getOAuthApplicationDataByAppName(appName) 
        .getOauthConsumerSecret()); 


      InboundAuthenticationRequestConfig[] iaReqConfigList = { iaReqConfig }; 

      InboundAuthenticationConfig ib = new InboundAuthenticationConfig(); 

      ib.setInboundAuthenticationRequestConfigs(iaReqConfigList); 

      ServiceProvider serviceProvider = new ServiceProvider(); 
      serviceProvider.setApplicationName(
        stub.getOAuthApplicationDataByAppName(appName).getApplicationName()); 
      serviceProvider.setInboundAuthenticationConfig(ib); 

      IAMStub.createApplication(serviceProvider); 

      System.out.println("Service Provider created"); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void authenticate(ServiceClient client) { 
     Options option = client.getOptions(); 
     HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator(); 
     auth.setUsername("admin"); 
     auth.setPassword("admin"); 
     auth.setPreemptiveAuthentication(true); 
     option.setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth); 
     option.setManageSession(true); 
    } 

} 

一旦我运行此代码,是获得WSO2身份服务器,我可以在管理控制台中看到创建的服务提供商。相对于服务提供商完成的OAuth配置没有显示出来,只有'配置'链接就是空的。如果我正确理解了WSO2 IS,那么我应该在入站认证配置 - > OAuth/OpenID连接配置下拉菜单下获取使用者密钥和使用者密码。

请帮我做些什么对吗?

回答

3

试着改变你的客户端为波纹管,

import java.rmi.RemoteException; 

import org.apache.axis2.client.Options; 
import org.apache.axis2.client.ServiceClient; 
import org.apache.axis2.transport.http.HttpTransportProperties; 
import org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationConfig; 
import org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationRequestConfig; 
import org.wso2.carbon.identity.application.common.model.xsd.Property; 
import org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider; 
import org.wso2.carbon.identity.application.mgt.stub.IdentityApplicationManagementServiceStub; 

import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceException; 
import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceStub; 
import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO; 

public class IdentityClient { 
    private final static String SERVER_URL = "https://localhost:9443/services/"; 

    public static void main(String[] args) throws RemoteException, OAuthAdminServiceException { 

     String appName = "Sample_App_5"; 
     String appDescription = "Test description"; 

     System.setProperty("javax.net.ssl.trustStore", "wso2carbon.jks"); 
     System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon"); 

     try { 

      OAuthAdminServiceStub stub = new OAuthAdminServiceStub(null, 
        SERVER_URL + "OAuthAdminService"); 
      IdentityApplicationManagementServiceStub IAMStub = new IdentityApplicationManagementServiceStub(
        null, SERVER_URL + "IdentityApplicationManagementService"); 
      ServiceClient client = stub._getServiceClient(); 
      ServiceClient IAMClient = IAMStub._getServiceClient(); 
      authenticate(client); 

      authenticate(IAMClient); 
      ServiceProvider serviceProvider = new ServiceProvider(); 
      serviceProvider.setApplicationName(appName); 
      serviceProvider.setDescription(appDescription); 
      IAMStub.createApplication(serviceProvider); 

      OAuthConsumerAppDTO consumerApp = new OAuthConsumerAppDTO(); 
      consumerApp.setApplicationName(appName); 
      consumerApp.setOAuthVersion("OAuth-2.0"); 
      consumerApp.setCallbackUrl("http://localhost:8080/playground2/oauth2client"); 
      consumerApp.setGrantTypes(
        "authorization_code implicit password client_credentials refresh_token " 
          + "urn:ietf:params:oauth:grant-type:saml2-bearer iwa:ntlm"); 

      /* OAuthAdminProxy.registerOAuthApplicationData(consumerApp); */ 
      stub.registerOAuthApplicationData(consumerApp); 
      System.out.println("Application created successfully"); 
      System.out.println(stub.getOAuthApplicationDataByAppName(appName).getOauthConsumerKey()); 

      authenticate(IAMClient); 

      InboundAuthenticationRequestConfig iaReqConfig = new InboundAuthenticationRequestConfig(); 
      iaReqConfig.setInboundAuthKey(stub.getOAuthApplicationDataByAppName(appName).getOauthConsumerKey()); 
      iaReqConfig.setInboundAuthType("oauth2"); 

      Property property = new Property(); 
      property.setName("oauthConsumerSecret"); 
      property.setValue(stub.getOAuthApplicationDataByAppName(appName).getOauthConsumerSecret()); 
      Property[] properties = { property }; 
      iaReqConfig.setProperties(properties); 

      InboundAuthenticationRequestConfig[] iaReqConfigList = { iaReqConfig }; 

      InboundAuthenticationConfig ib = new InboundAuthenticationConfig(); 

      ib.setInboundAuthenticationRequestConfigs(iaReqConfigList); 

      serviceProvider = IAMStub.getApplication(appName); 
      serviceProvider.setApplicationName(
        stub.getOAuthApplicationDataByAppName(appName).getApplicationName()); 
      serviceProvider.setInboundAuthenticationConfig(ib); 

      IAMStub.updateApplication(serviceProvider); 

      System.out.println("Service Provider created"); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void authenticate(ServiceClient client) { 
     Options option = client.getOptions(); 
     HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator(); 
     auth.setUsername("admin"); 
     auth.setPassword("admin"); 
     auth.setPreemptiveAuthentication(true); 
     option.setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth); 
     option.setManageSession(true); 
    } 
} 

问题是createApplication不保存较名称和描述其他的配置。您必须调用updateApplication来保存其他应用程序配置。

+0

试过你的解决方案..但仍然没有得到消费者的密钥和消费者的秘密如预期。 –

+0

我编辑了客户端代码。使用IS 5.1.0 –

+0

进行了测试,现在它的工作非常完美。非常感谢您的及时帮助。我的目标是使5.0.0发生这种情况。无论如何,我将移动到5.1.0。 –