如何深度总结keycloak认证与校验的原理和应用?
摘要:主要内容 密码认证 授权码认证 哪个client申请的code,使用哪个client获取token 客户端认证 在kc开通了服务端帐号之后,可通过client_id和client_secret来获取token,与用户无关,无刷新token机
主要内容
密码认证
授权码认证 哪个client申请的code,使用哪个client获取token
客户端认证 在kc开通了服务端帐号之后,可通过client_id和client_secret来获取token,与用户无关,无刷新token机制
自动触发社区认证 当用户在社区网站上登录后,访问这个地址可自动登录网站
验证token是否在线 可使用任意client来验证所有token的在线性
refresh_token刷新token 哪个client生成的token,就用哪个client去刷新
登出/注销 官方post方式和浏览器302方式
密码认证
POST /auth/realms/your-realm/protocol/openid-connect/token
请求体 x-www-form-urlencoded
grant_type:password
username:test
password:123456
client_secret:ec0fd1c6-68b0-4c39-a9fa-c3be25c8ef01
client_id:democlient
响应
{
"access_token": "*.*.*",
"expires_in": 3000,
"refresh_expires_in": 1800,
"refresh_token": "*.*.*",
"token_type": "bearer",
"not-before-policy": 1619512543,
"session_state": "765969ec-94da-4edb-9dcb-e15ea3e0ad3b",
"scope": "roles email profile"
}
授权码认证
注意的几点
code时的redirect_uri和authorization_code的需要是一致的,不一致的错误
{
"error": "invalid_grant",
"error_description": "redirect_uri error"
}
code时的client_id和client_secret和authorization_code的需要是一致的,不一致的错误,这块我已经修改了源码,去掉了这个限制
{
"error": "invalid_grant",
"error_description": "Auth error"
}
code的组成
它由3部分组成,中间使用.分割,第一部分是UUID,第二部分是用户会话ID【session_state】,第三部分是客户端ID,例如:
5c33f9a2-cbf4-4804-a8ee-e2d076eda2d6.40be5301-f41b-4f0d-97e7-d2074db2801c.ff591897-7654-460e-9c19-8e8f92117768
请求code
GET /auth/realms/your-realm/protocol/openid-connect/auth
QUERY
client_id:democlient
scope:openid
response_type:code
client_secret:ec0fd1c6-68b0-4c39-a9fa-c3be25c8ef01
redirect_uri:http://localhost:9090/callback
跳转到kc的登录页,完成用户名和密码的登录
登录成功之后,跳回callback删除,在url参数上带上了code
请求token
POST /auth/realms/your-realm/protocol/openid-connect/token
请求体 x-www-form-urlencoded
grant_type:authorization_code
code:68058719-add6-4b40-ab96-8e71af03827a.7a31b1a9-c3e8-46d4-b8cc-345012fcf4a2.25e52f60-5991-43dd-9108-873f60af385d
client_id:democlient
client_secret:ec0fd1c6-68b0-4c39-a9fa-c3be25c8ef01
scope:openid
redirect_uri:http://localhost:9090/callback
响应
{
"access_token": "*.*.*",
"expires_in": 3000,
"refresh_expires
