shiro登录后 返回旧地址_Spring Security 实战干货:登录后返回 JWT Token

我是 码农小胖哥。天天有编程干货分享。觉得写的不错。点个赞,转发一下,关注一下。本文为个人原创文章,转载请注明出处,非法转载抄袭将追究其责任。

56e9c660ea60a91b031451bdacf9d5bd.png

1. 前言

欢迎阅读 https://www.felord.cn/categories/spring-security/,https://www.felord.cn/spring-security-custom-jwt.html 我们实现了 JWT 工具。本篇我们将一起探讨如何将 JWTSpring Security 结合起来,在认证成功后不再跳转到指定页面而是直接返回 JWT Token 。 本文的DEMO 可通过文末的方式获取

2. 流程

JWT 适用于前后端分离。我们在登录成功后不在跳转到首页,将会直接返回 JWT Token 对(DEMO中为JwtTokenPair),登录失败后返回认证失败相关的信息。

3. 实现登录成功/失败返回逻辑

如果你看过 https://www.felord.cn/spring-security-login.html 将非常容易理解下面的做法。

3.1 AuthenticationSuccessHandler 返回 JWT Token

AuthenticationSuccessHandler 用于处理登录成功后的逻辑,我们编写实现并注入 Spring IoC 容器:

 /** * 处理登录成功后返回 JWT Token 对. * * @param jwtTokenGenerator the jwt token generator * @return the authentication success handler */ @Bean public AuthenticationSuccessHandler authenticationSuccessHandler(JwtTokenGenerator jwtTokenGenerator) { return (request, response, authentication) -> { if (response.isCommitted()) { log.debug("Response has already been committed"); return; } Map map = new HashMap<>(5); map.put("time
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值