基于JWT的认证方案
#5429
Replies: 3 comments 2 replies
-
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks for your work, but I think yaml format interface is better, and I think this interface description is not align to RESTful API design principles. |
Beta Was this translation helpful? Give feedback.
1 reply
-
HTTP/1.1 401 Unauthorized { this should not have "code" field neither |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
在openApi和portal接口请求统一的需求下,将portal改造为通过token的方式实现认证。
经讨论,拟选用如下方案:
完全自定义登录接口,通过 Controller 实现认证与 JWT 生成。
接口
url:/api/refresh-token
method:POST
content-type:application/json
成功响应:
流程图
自定义登录接口 + JWT
1. 禁用默认 formLogin,配置 WebSecurity
在auth 和 ldap 认证模式下要禁用默认的 formLogin,通过自定义登录接口登录
oicd 略有差异,OIDC完成身份验证后,生成内部JWT用于业务接口鉴权。这可以在默认OAuth2Login流程中扩展实现,无需完全重写登录流程。主要是在OIDC认证成功后,拦截用户信息并生成JWT返回
2. 自定义登录接口 Controller
3. 实现 JWT 工具类
处理 JWT 生成、验证和解析:
4. 实现 JWT 认证过滤器
拦截请求并验证 JWT,设置认证信息到
SecurityContext
:5. UserInfoHolder 和 LogoutHandler
自定义的
JwtAuthenticationFilter
在请求来解析用户信息后,依旧会调用SecurityContextHolder.getContext().setAuthentication(authentication);
,所以UserInfoHolder
从SecurityContextHolder
中获取数据,并不影响,无要调整。LogoutHandler
要清除cookie中的长时效tokenBeta Was this translation helpful? Give feedback.
All reactions