导读
OAuth 2.0:是一个开放的授权框架,当用户想要访问Service Provider提供的资源时,OAuth客户端可以从IdP(Identity Provider)获得授权而不需要获取用户名和密码就可以访问该资源题。
作者:vivi,来源:osinnovation
https://i-blog.csdnimg.cn/img_convert/ed60da04284e9018a0ef011d09b6ecce.png" width="1080" />
原理分析:在原来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客户端应用
https://i-blog.csdnimg.cn/img_convert/9e29cd679235e8761bdc44d0f073b5c2.png" width="1080" />
https://i-blog.csdnimg.cn/img_convert/2f3bdf8aad8a6187be88e4eeb948d54c.png" width="1080" />
https://i-blog.csdnimg.cn/img_convert/414ec9dbe87751b411e633995d7c9cd0.png" width="1080" />
字段填写说明:
字段 | 说明 |
发布者 | SuccessFactors |
通用名称 | 证书有效的名称或IP地址 |
组织(非必填) | 颁发证书的实体 |
组织单位(非必填) | 颁发证书的实体的组织单位 |
位置(非必填) | 颁发证书的实体的位置名称 |
州/省(非必填) | 颁发证书的实体的州或省的名称 |
国家(非必填) | 要向其颁发证书的实体的国家名称 |
有效期 | X.509证书有效的天数 |
https://i-blog.csdnimg.cn/img_convert/17c748b0e8685eb8145ade2b98cdca29.png" width="1080" />
下载证书,并注册,注册之后系统会生成api的key,注意这个api的key和509的正式下面有用。
https://i-blog.csdnimg.cn/img_convert/ae0c9a4625627bf99101339a41f8730e.png" width="1080" />
生成的.509证书是有公钥与私钥,私钥我们后面生成saml的时候需要使用,这里复制的时候不要复制所有,只是复制私钥即可。
https://i-blog.csdnimg.cn/img_convert/949fa71095c15bd4d8c97f5b61c0df34.png" width="1080" />
因为使用在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:
https://i-blog.csdnimg.cn/img_convert/9f01ba6ff1a84cc8a56799715c0926ce.png" width="1080" />
STEP 2: VALIDATING JAVA JDK COMPATIBILITY AND MAINTAINING ENVIRONMENT VARIABLES(jdk与mavne的环境变量设置,如果已经设置可以忽略)
-
At your computer: Click on File Explorer > This PC (right button) > Properties > Advanced System Settings > Environment Variables;
-
At System Variables, ensure JAVA_HOME variable is set and points to the path of your JDK installation;
-
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:
https://i-blog.csdnimg.cn/img_convert/6b1b0bead9e4818b768bff8ffb7cde74.png" width="852" />
STEP 3: VALIDATING JDK INSTALLATION(查看jdk是否安装成功与mavne环境变量设置是否成功)
-
Go to Command Prompt and execute the command below:
echo %JAVA_HOME% -
If it prints the folder path of the JDK installation, it means the installation was successful:
https://i-blog.csdnimg.cn/img_convert/6c175ab34a412a0b8a0a686acbf180df.png" width="931" />
STEP 4: VALIDATING MAVEN INSTALLATION
检查maven是否安装成功
-
Open the Command Prompt and execute the command below:
mvn -v
https://i-blog.csdnimg.cn/img_convert/e2b4fca1155099c42fd124d7bd4d6fe9.png" width="1080" />
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下载附件)
-
Go to attachment section of this KBA and download the zip file;
-
Extract folder from zip file to your local drive;
-
Go to the folder and open "SAMLAssertion.properties" file;
https://i-blog.csdnimg.cn/img_convert/59912558b2cead8a19c481a3a6a18251.png" width="1003" />
https://i-blog.csdnimg.cn/img_convert/7fccc41c13e65d8242c75611d2b68e7c.png" width="563" />
-
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.
-
-
See the sample file below:(附件SAMLAssertion.properties需要天虹红色标记的地方)
https://i-blog.csdnimg.cn/img_convert/7837210a7a3244e49cec0b5a82a2aad0.png" width="1080" />
-
Save the file and close.
-
Open a new Command Prompt window and go to the directory where folder was extracted:(保存以后可以在文件所在的文件夹上面输入CMD命令,就会进入DOS黑白指令的的地方)
https://i-blog.csdnimg.cn/img_convert/6fd044bf01afea7b14e9343fe4309db9.png" width="1080" />
-
https://i-blog.csdnimg.cn/img_convert/e2f60b23df2178b2635f770f3535f2ca.png" width="1080" />
-
Execute the command below:
mvn compile exec:java -Dexec.args="SAMLAssertion.properties"(执行下这个脚本)
系统就会有下面的https://i-blog.csdnimg.cn/img_convert/a9260971a5dcb5aba8a27d942ac5ca16.png" width="1080" />
-
Once completed, it would generate SAML assertion. Copy this and store it securely in your local drive.(如果看到build success,说明smal生成成功)
https://i-blog.csdnimg.cn/img_convert/53e316cec0a89e9af8845b44808fea2c.png" width="1080" />
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上面那部分
https://i-blog.csdnimg.cn/img_convert/82a0447c9b6db39a6b7fdf0af77a4651.png" width="1080" />
https://i-blog.csdnimg.cn/img_convert/8fe626b7f93968d56efb9457c2b6d47a.png" width="1080" />
4 最后一步就是通过生成的Token来调用API Entity
https://api55preview.sapsf.eu/odata/v2/User?top=2&$format=json
Authorization:Bearer 后面是token信息
https://i-blog.csdnimg.cn/img_convert/ab1a17bda21c8900d1fd451982eebca3.png" width="1080" />