当前位置:网站首页>11-security认证.md
11-security认证.md
2022-08-03 00:34:00 【张 邵】
我们需要自定义一个过滤器,这个过滤器会去获取请求头中的token,对token进行解析取出其中的userid。
使用userid去redis中获取对应的LoginUser对象。
然后封装Authentication对象存入SecurityContextHolder
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
@Autowired
private RedisCache redisCache;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//获取token
String token = request.getHeader("token");
if (!StringUtils.hasText(token)) {
//放行
filterChain.doFilter(request, response);
return;
}
//解析token
String userid;
try {
Claims claims = JwtUtil.parseJWT(token);
userid = claims.getSubject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("token非法");
}
//从redis中获取用户信息
String redisKey = "login:" + userid;
LoginUser loginUser = redisCache.getCacheObject(redisKey);
if(Objects.isNull(loginUser)){
throw new RuntimeException("用户未登录");
}
//存入SecurityContextHolder
//TODO 获取权限信息封装到Authentication中
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(loginUser,null,null);
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
//放行
filterChain.doFilter(request, response);
}
}
/** * @Author 三更 B站: https://space.bilibili.com/663528522 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Autowired
JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
//关闭csrf
.csrf().disable()
//不通过Session获取SecurityContext
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
// 对于登录接口 允许匿名访问
.antMatchers("/user/login").anonymous()
// 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated();
//把token校验过滤器添加到过滤器链中
http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
边栏推荐
- DB2数据库-获取表结构异常:[jcc][t4][1065][12306][4.26.14]CharConvertionException ERRORCODE=-4220,SQLSTATE=null
- 如何快速对接淘宝开放平台API接口(淘宝店铺订单明文接口,淘宝店铺商品上传接口,淘宝店铺订单交易接口)
- NVM和NRM
- 麒麟信安邀您抢先看 | openEuler 志高远,开源汇智创未来-开放原子全球开源峰会欧拉分论坛最详细议程出炉
- 【mysql知识点整理】--- order by 、group by 出现Using filesort原因详解
- 236. 二叉树的最近公共祖先
- 2149. 按符号重排数组
- 做快乐的事情
- UVM中SVA使用指南
- 【问题征集】向 iPod 之父、iPhone 联合设计者、Google Nest 创始人 Tony Fadell 提问啦
猜你喜欢

8 个常用的 Wireshark 使用技巧,一看就会

RollBack Rx Professional RMC 安装教程

开源聚力,共创未来 | 麒麟信安祝贺openKylin首个体验版正式发布!

从 npm 切换到 pnpm,真香!
![[NCTF2019]SQLi-1||SQL注入](/img/18/6483cd9d5d2722860652fea193c13a.png)
[NCTF2019]SQLi-1||SQL注入

JSP第一篇 -----JSP九大内置对象(隐式对象)和四大域对象

【系统架构设计师】第三章 数据库系统

定了!8月起,网易将为本号粉丝提供数据分析培训,费用全免!

Auto.js special positioning control method cannot perform blocking operations on the ui thread, please use setTimeout instead

【飞控开发高级教程2】疯壳·开源编队无人机-遥控整机代码走读、编译与烧写
随机推荐
风电场运营实践 | 麒麟信安助力国华投资山东公司集控中心实现安全智慧化运营
中科磁业IPO过会:年营收5.5亿 吴中平家族持股85%
Linear DP
如何突破测试/开发程序员思维?一种不一样的感觉......
JSP第一篇 -----JSP九大内置对象(隐式对象)和四大域对象
Understand the next hop address in the network topology in seconds
向往的生活
flutter 时间戳转日期
阿里云增强版实人认证--银行卡要素核验
【多线程】线程与进程、以及线程进程的调度
接口流量突增,如何做好性能优化?
【软考 系统架构设计师】软件架构设计① 软件架构的概念
鲲鹏devkit开发套件
Greenplum数据库故障分析——can not listen port
Jmeter二次开发实现rsa加密
稳压电源: 电路图及类型
Flink / Scala - 使用 CountWindow 实现按条数触发窗口
嵌入式开发:嵌入式基础——’ ’和” ”的区别
C语言:链表
几种常见的跨域解决方法