当前位置: 首页 > article >正文

如果通过认证方式调用Sf的api

 

导读

OAuth 2.0:是一个开放的授权框架,当用户想要访问Service Provider提供的资源时,OAuth客户端可以从IdP(Identity Provider)获得授权而不需要获取用户名和密码就可以访问该资源题。

作者:vivi,来源:osinnovation

图片

原理分析:在原来SuccessFactors odata api是可以通过账户密码授权的模式去查询,这种方式虽然便捷,但是安全性不好,所以SAP开始从认证模式,上图就是认证模式的一个流程

① 在successfactors系统中注册oauth2的客户端程序,这个程序有两个东西很重要,一个是509的加密文件,一个是api的key,这两个地方在生成smal的时候需要用到。

②如何产生smal,原来是可以通过oauth/idp的方式生成smal,但是也是因为安全问题,也将在2025年到期,所以需要通过sap的工具完成,可以通过:3031657 - How to generate SAML assertion using SAP-provided offline tool - SAP SuccessFactors,具体的操作下面有详细的步骤。

③ 通过步骤2可以生成smal的数据,需要根据smal的数据到/oauth/token抓取到token的数据,token就是我们敲门的钥匙,原来访问是通过账号密码,现在就是携带token数据替代账号密码。

④ 用获取的token去调用具体的服务地址,例如https://api55preview.sapsf.eu/odata/v2/User?top=2&$format=json

下面是系统具体实现步骤:

1. 登录SF实例,Manage OAuth2 Client Applications下注册OAuth客户端应用

图片

图片

图片

字段填写说明:

字段

说明

发布者

SuccessFactors

通用名称

证书有效的名称或IP地址

组织(非必填)

颁发证书的实体

组织单位(非必填)

颁发证书的实体的组织单位

位置(非必填)

颁发证书的实体的位置名称

州/省(非必填)

颁发证书的实体的州或省的名称

国家(非必填)

要向其颁发证书的实体的国家名称

有效期

X.509证书有效的天数

图片

下载证书,并注册,注册之后系统会生成api的key,注意这个api的key和509的正式下面有用。

图片

生成的.509证书是有公钥与私钥,私钥我们后面生成saml的时候需要使用,这里复制的时候不要复制所有,只是复制私钥即可。

图片

因为使用在Postman工具中配置生成SAML断言(assertion) 已经废弃,所以今天我们使用的是SAP的工具帮我们生成的smal。

2 现在需要通过最新sap工具生成saml断言,需要下载的工具是maven与jdk,注意jdk的版本

STEP 1: DOWNLOADING MAVEN FILE(下载mavne工具,注意版本,这个版本搭配的jdk是1.8)

  • Download the file from Downloading Apache Maven and unzip it to your local drive.
    In the example below, the folder has been extracted to the C drive:

     

    图片

      
     

STEP 2: VALIDATING JAVA JDK COMPATIBILITY AND MAINTAINING ENVIRONMENT VARIABLES(jdk与mavne的环境变量设置,如果已经设置可以忽略)

  1. At your computer: Click on File Explorer > This PC (right button) > Properties > Advanced System Settings > Environment Variables;

  2. At System Variables, ensure JAVA_HOME variable is set and points to the path of your JDK installation;

  3. At User Variables, click on new and set the path of the "bin" folder (from your JDK installation folders) to the variable "PATH" as shown below:

     

    图片


     

STEP 3: VALIDATING JDK INSTALLATION(查看jdk是否安装成功与mavne环境变量设置是否成功)

  1. Go to Command Prompt and execute the command below:
    echo %JAVA_HOME%

  2. If it prints the folder path of the JDK installation, it means the installation was successful:

     

    图片

    STEP 4: VALIDATING MAVEN INSTALLATION

    检查maven是否安装成功

    1. Open the Command Prompt and execute the command below:
      mvn -v

       

      图片




      This means Maven installation is successful. Now we are good to proceed with generating SAML assertion using SAP provided offline tool.
       

    STEP 5: GENERATING SAML ASSERTION(需要到note:3031657下载附件)

    1. Go to attachment section of this KBA and download the zip file;

    2. Extract folder from zip file to your local drive;

    3. Go to the folder and open "SAMLAssertion.properties" file;

       

      图片



       

      图片


       

    4. Fill the data there:

      • If you use this field, you shouldn't use the "userId" field, leaving it blank.

      • If you use this field, you shouldn't use the "userName" field, leaving it blank.

      • tokenUrl: your API server endpoint from guide page List of SAP SuccessFactors API Servers followed by "/oauth/token".

      • clientId: API Key which you received while registering the client in SF.

      • userId: the userId of the API user.

      • userName: the username of the API user.

      • privateKey: X.509 private key.

      • expireInMinutes: SAML assertions usually expires in 5-10 minutes. Anyway, here you should set it according your business requirements.

    5. See the sample file below:(附件SAMLAssertion.properties需要天虹红色标记的地方)

       

      图片

    1. Save the file and close.

    2. Open a new Command Prompt window and go to the directory where folder was extracted:(保存以后可以在文件所在的文件夹上面输入CMD命令,就会进入DOS黑白指令的的地方)

       

      图片

    1. 图片

    2. Execute the command below:
      mvn compile exec:java -Dexec.args="SAMLAssertion.properties"(执行下这个脚本)



      系统就会有下面的

      图片



       

    3. Once completed, it would generate SAML assertion. Copy this and store it securely in your local drive.(如果看到build success,说明smal生成成功)

       

      图片

    3 使用SAML断言生成用户Token

    Headers: Content-Type: application/x-www-form-urlencoded

    Body(raw):client_id=xxx&user_id=xxx&token_url=xxx&private_key, client_id就是上面生成的API Key,user_id是SF账户的ID,token_url是SF的Endpoint+/outh/token, private_key是下载的证书文件中private部分内容,也就是上图build success上面那部分

    图片

    图片

    4 最后一步就是通过生成的Token来调用API Entity

    https://api55preview.sapsf.eu/odata/v2/User?top=2&$format=json

    Authorization:Bearer 后面是token信息

    图片


    http://www.kler.cn/a/530181.html

    相关文章:

  1. JAVA安全—反射机制攻击链类对象成员变量方法构造方法
  2. c++ stl 遍历算法和查找算法
  3. 【前端知识】常用CSS样式举例
  4. 消息队列应用示例MessageQueues-STM32CubeMX-FreeRTOS《嵌入式系统设计》P343-P347
  5. 如何配置Java JDK
  6. 理解 InnoDB 如何处理崩溃恢复
  7. 动态函数调用和模拟
  8. CNN的各种知识点(一):卷积神经网络CNN通道数的理解!
  9. BitLocker技巧与经验
  10. 基于 YOLOv8+PyQt5 界面自适应的无人机红外目标检测系统项目介绍框架
  11. 在C语言多线程环境中使用互斥量
  12. Ollama 介绍,搭建本地 AI 大模型 deepseek,并使用 Web 界面调用
  13. 让banner.txt可以自动读取项目版本
  14. Rust 中的注释使用指南
  15. 【hot100】刷题记录(11)-搜索二维矩阵 II
  16. AI源码加训练
  17. 如何配置Java JDK
  18. 8.原型模式(Prototype)
  19. 代码随想录算法训练营第十六天| 二叉树4
  20. 【论文复现】基于Otsu方法的多阈值图像分割改进鲸鱼优化算法
  21. LLMs之OpenAI o系列:OpenAI o3-mini的简介、安装和使用方法、案例应用之详细攻略
  22. 【每天学习一点点】通过使用@property装饰器来创建一个属性的getter和setter方法
  23. 【周易哲学】生辰八字入门讲解(八)
  24. STM32 DMA数据转运
  25. leetcode 930. 和相同的二元子数组
  26. 【人工智能】使用Python和Hugging Face构建情感分析应用:从模型训练到Web部署