简体   繁体   English

使用Java在具有OAuth功能的WSO2 Identity Server中创建服务提供者

[英]Creating service provider in WSO2 Identity Server with OAuth facility using Java

I am developing a Java client which creates the service provider dynamically with Inbound Authentication set to OAuth in WSO2 Identity Server. 我正在开发一个Java客户端,该客户端将在WSO2 Identity Server中将“入站身份验证”设置为OAuth的情况下动态创建服务提供商。 The code goes as follows 代码如下

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

}

Once I run this code, the service provider is getting created in the WSO2 Identity Server which I could see in the management console. 运行此代码后,将在管理控制台中看到的WSO2 Identity Server中创建服务提供者。 The OAuth configuration which has been done vis-a-vis the service provider is not showing up and it is empty with just a 'configure' link. 相对于服务提供商已完成的OAuth配置未显示,仅显示“配置”链接为空。 Had I understood WSO2 IS properly then I should be getting the consumer key and consumer secret under Inbound Authentication Configuration --> OAuth/OpenID Connect Configuration drop down. 如果我了解WSO2是否正确,则应该在“入站身份验证配置”->“ OAuth / OpenID连接配置”下拉列表中获取使用者密钥和使用者秘密。

Please help me in what should be done right ? 请帮助我正确的做法?

Try changing your client as bellow, 尝试将您的客户更改为波纹管,

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

Problem is createApplication does not save the configurations other than the name and the description. 问题是createApplication不会保存名称和描述以外的配置。 You have to call updateApplication to save other application configurations. 您必须调用updateApplication来保存其他应用程序配置。

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

相关问题 302在WSO2 Identity Server中创建服务提供程序时出错 - 302 Error while creating service provider in WSO2 Identity Server OAuth 2.0 使用 Spring Security + WSO2 身份服务器 - OAuth 2.0 using Spring Security + WSO2 Identity Server wso2身份服务器oauth userinfo没有角色 - wso2 identity server oauth userinfo no role java.net.SocketTimeoutException:WSO2 Identity Server OAuth令牌验证上的读取超时 - java.net.SocketTimeoutException: Read timed out on WSO2 Identity Server OAuth Token Validation 无法使用AuthenticationAdmin API身份验证为WSO2 Identity Server 5.1.0创建Java客户端 - Unable to create a java client to WSO2 Identity Server 5.1.0 using AuthenticationAdmin API Authentication WSO2 身份服务器 - Carbon 无法执行 Java - WSO2 Identity Server - Carbon cannot execute Java WSO2 Identity Server是否支持Java 5(JDK 1.5)? - Will WSO2 Identity Server support Java 5( JDK 1.5)? 在 WSO2 身份服务器 (wso2is-km-5.9.0) 中创建工作流时出错 - Error while creating Workflow in WSO2 Identity Server(wso2is-km-5.9.0) wso2身份服务器oauth2请求返回HTTP错误415 - wso2 identity server oauth2 request returns HTTP error 415 如何使用 oauth2 wso2 身份服务器获取授权码以关闭登录页面 - How to get authorize code to dismiss login page with oauth2 wso2 identity server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM