sso
sso 单点登录
1 单点登录时序图
+-----------------+ +---------------------+ +-----------+
|~* /static/ | |= /auth/verification | |/ |
|= /index_sso.html| |= /butterfly_401 | | | Nginx 配置
|= / | |= /auth/ssologin | | |
+-----------------+ +---------------------+ +-----------+
| | |
+----------+ +---------------+ +--------------+ +----------+ +-----------+
|web browse| |butterfly-admin| |butterfly-auth| |cas-server| |app-backend| 服务
+----------+ +---------------+ +--------------+ +----------+ +-----------+
| | | | |
+-------route------->|/ | | |
|<-------page--------+/index_sso.html | | |
| | | | |
====================================================================not have token
| | | | |
+--V----------------request api---------------------------------------------------->| 1 客户端请求后端接口返回 401
| +-sub request-header not have token--->|(/auth/verification) | |
|<-code=401,targetURL=/auth/ssologin------+ | |
| | | | |
+--window.location.herf=directurl-------->|(/auth/ssologin) | | 2 客户端设置当前页面的 URL 地址为 /auth/ssologin
|<----code=302,Location=cas-server--------+ | |
| | | | |
+-----302 http://cas-server/login login page ----------------->|(/login) | 3 客户端根据 butterfly-auth 返回内容重定向到 cas-server
|<-------------code=302,set Cookie TGT=xxx ---------------------+ |
| | | | |
+-----302 /auth/ssologin?ticket=xxx ----->|(/auth/ssologin) | | 4 客户端根据 cas-server 返回内容重定向到 /auth/ssologin
| | +-------check st----->|(/session/validate)|
| | |<-------st vaild-----+ |
|<--code=302 set Cookie butterfly_token---+ | |
| | | | |
+--302 / ----------->| | | | 5 客户端重新请求首页
|<-------page--------+/index_sso.html | | |
| | | | |
======================================================================== have token
| | | | |
+---V----------------request api--------------------------------------------------->|
| +-sub request-header have token------>|(/auth/verification) | |
|<-------------------response-------------------------------------------------------+2 butterfly-admin 关键点
2.1 nginx auth request
Authentication Based on Subrequest Result(基于子请求结果的认证)
Nginx auth 子请求是通过 HTTP GET 方法发送的,即使原请求是 POST 请求,auth 子请求也会转为 GET 请求,GET 请求没有 body , body 被丢弃。
nginx 配置
2.2 amis 全局适配器
2.3 Token 存储方式
单点登录时,需要跳转到 butterfly-admin 单页之外进行登录,则无法使用拦截器进行拦截,故而需要使用 Cookie
2.4 username 传递
作用:后端服务可以获取到 username 用来鉴权及审计等操作
/auth/verification 认证完成后,返回("username", "meetbill") header
auth_request 返回的 username 头默认不会自动传递给 backend
Nginx 默认只会转发客户端请求的 headers,而
auth_request的响应头(如username)需要手动提取并传递给后端。需要通过
auth_request_set提取该值,然后使用proxy_set_header显式传递给backend。
后端(butterfly_xingqiao) 就可以看到 ("HTTP_X_USERNAME", "meetbill") 的 header。
Last updated