# SAML_Demo **Repository Path**: heweipo_admin/hellosaml-idp ## Basic Information - **Project Name**: SAML_Demo - **Description**: SAML 流程学习 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2024-06-10 - **Last Updated**: 2025-05-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 前言 该项目从 https://github.com/ichengchao/hellosaml-idp fork 而来,然后基于网上学习,补充了 IDPController 和 SPController 记录学习笔记。 ## 1.1 SP 发送 AuthnRequest 的过程 1. SP 请求 `http://localhost:8080/sp/resource` 那么 SP 会生成 `AuthnRequest` 通过 POST 方式请求 IDP 的 `http://localhost:8080/idp/sso` 2. IDP 首先验证 `AuthnRequest` 的签名是否正确,如果不正确,服务端报错:`Signature cryptographic validation not successful`,重定向信息提示页面 error.html 3. IDP 如果验证 `AuthnRequest` 的签名正确,那么重定向到用户名密码认证页 login.html ,需用户输入用户名和密码进行认证,认证通过后进入 IDP 发送 Assertion 的过程 ## 1.1 IDP 发送 Assertion 的过程 1. IDP 展示给用户认证表单,通常有多种认证方式,甚至双因素等,用户输入用户名和密码,发起认证请求 `http://localhost:8080/idp/sso/login` 2. IDP 生成 `Assertion`,然后通过 POST 方式请求 SP 的 `http://localhost:8080/sp/redirect` 3. SP 首先验证 `Assertion` 的签名是否正确,如果不正确,服务端报错:`Signature cryptographic validation not successful`,则显示:认证不受信任,请重新发起认证 4. SP 如果验证 `Assertion` 的签名正确,那么展示 IDP 提供的用户信息,最终需要把用户重定向到最开始的请求 `http://localhost:8080/sp/resource` # 代码 - 监听端口: 8080 - 启动类: `hellosaml.IdPDemoApplication` - 配置类: `hellosaml.CommonConstants` - 首页地址: http://localhost:8080/index.html - meta.xml地址: http://localhost:8080/metaxml 新增 - SP:`http://localhost:8080/sp/resource` 模拟请求 SP 的资源,然后生成 AuthnRequest 重定向到 IDP - IDP:`http://localhost:8080/idp/sso` IDP 接收 SP 的 AuthnRequest,等待用户认证 - IDP:`http://localhost:8080/idp/sso/login` 用户认证完成后,IDP 生成 Assertion 重定向到 SP - SP:`http://localhost:8080/sp/redirect` SP 接收 IDP 的 Assertion,签名校验和信息获取 # 实操 ## 生成证书 ```shell #create the keypair openssl req -new -x509 -days 3652 -nodes -out saml.crt -keyout saml.pem #convert the private key to pkcs8 format openssl pkcs8 -topk8 -inform PEM -outform DER -in saml.pem -out saml.pkcs8 -nocrypt ``` 完成后会生成3个文件: - saml.crt - saml.pem - saml.pkcs8 修改 hellosaml.common.CommonConstants - PUBLIC_KEY_PATH配置成saml.crt的文件的绝对路径,比如: `/Users/charles/config/saml.crt` - PRIVATE_KEY_PATH配置成saml.pkcs8的文件的绝对路径,比如: `/Users/charles/config/saml.pkcs8` ### [阿里云控制台配置](./README_aliyun.md) ### 附加说明 配置类: hellosaml.common.CommonConstants | 参数 | 是否需要调整 | 描述 | | :--------------- | :----------: | :------------------------------------------------------ | | PUBLIC_KEY_PATH | 是 | 公钥文件绝对路径,比如: /Users/charles/config/saml.crt | | PRIVATE_KEY_PATH | 是 | 私钥文件绝对路径,比如: /Users/charles/config/saml.pkcs8 | | IDP_ENTITY_ID | 是 | IDP的唯一标识,比如: chengchao.name/myIdP | SAML 时序图