keycloak测试11.0.2
- Password
- Client credentials
- Introspection
- Authorization code
Password
应用于后端处理:
set KEYCLOAK_HOST=http://keycloak.demofor.com.cn/
set KEYCLOAK_REALM=keycloak-learn
set CLIENT_ID=chapter-1
set CLIENT_SECRET=149240f3-31c6-46dc-bab4-0b53c0ee13b9
关于client_secret的获取截图如下:
改成一行命令:
set KEYCLOAK_HOST=keycloak.demofor.com.cn && set KEYCLOAK_REALM=keycloak-learn && set CLIENT_ID=chapter-1 && set CLIENT_SECRET=149240f3-31c6-46dc-bab4-0b53c0ee13b9
在windows 需要把 \ 换成^,还有变量$ 改成%号 ,比如:$a 改成%a%
下面是访问方法:
curl --request POST ^
–url http://keycloak.demofor.com.cn/auth/realms/keycloak-learn/protocol/openid-connect/token ^
–header ‘Content-Type: application/x-www-form-urlencoded’ ^
–data grant_type=password ^
–data client_id=chapter-1 ^
–data client_secret=149240f3-31c6-46dc-bab4-0b53c0ee13b9 ^
–data username=user ^
–data password=1 ^
–data scope=openid
效果图如下:
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/1b204b105975415d86eb17575d18c9d3.png
返回的数据截图如下:
获取到token,需要使用工具返解jwt 才能看到数据
获取access_token:
效果如下:
Client credentials
curl --request POST ^
–url http://keycloak.demofor.com.cn/auth/realms/keycloak-learn/protocol/openid-connect/token ^
–header ‘Content-Type: application/x-www-form-urlencoded’ ^
–data grant_type=client_credentials ^
–data client_id=chapter-1 ^
–data client_secret=149240f3-31c6-46dc-bab4-0b53c0ee13b9
输入截图如下:
返回信息如下:
Introspection
token 解码 因为这个token 有有效期,可以使用password 请求获取到的token ,再来请求curl--request POST ^ --url http://keycloak.demofor.com.cn/auth/realms/keycloak-learn/protocol/openid-connect/token/introspect ^ --header'Content-Type: application/x-www-form-urlencoded'^ --dataclient_id=chapter-1 ^ --dataclient_secret=149240f3-31c6-46dc-bab4-0b53c0ee13b9 --datatoken=password 返回的token截图如下;
使用的工具为:bruno
Authorization code
这个需要在这里流程
先请求获取,登录后,获取到code,
开启一个9000 服务端,实现/,和/callback
http://keycloak.demofor.com.cn/auth/realms/keycloak-learn/protocol/openid-connect/auth?client_id=chapter-1&response_type=code&redirect_uri=http://localhost:9000/callback&scope=openid&client_secret=149240f3-31c6-46dc-bab4-0b53c0ee13b9发送请求:在地址栏输入:http://localhost:9000/,点击access code ,
跳转到登录界面,输入用户名与密码,截图如下:
再使用这个code ,请求:
curl --request POST ^
–url http://keycloak.demofor.com.cn/auth/realms/keycloak-learn/protocol/openid-connect/token ^
–header ‘Content-Type: application/x-www-form-urlencoded’ ^
–data grant_type=authorization_code ^
–data redirect_uri=http://localhost/callback ^
–data code=d16a190c-44d9-432a-a809-459fdb37bb26.ee51d9d4-63b4-422e-a9c4-00a41fd19183.00a1f5fc-e14a-4309-92f2-67c3305fc275 ^
–data client_id=chapter-1 ^
–data client_secret=149240f3-31c6-46dc-bab4-0b53c0ee13b9
参考文章地址: 我们选择OpenID协议中的 http://www.keycloak.org/docs/latest/securing_apps/index.html#openid-connect-3 OpenID协议中的Spring Boot Adapterhttp://www.keycloak.org/docs/latest/securing_apps/index.html#_spring_boot_adapter Spring Security比较熟悉 http://www.keycloak.org/docs/latest/securing_apps/index.html#_spring_security_adapter