当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(上)

Servlet——请求(request)与响应(response)

236. 二叉树的最近公共祖先

async-await

文树勋率长沙市人大常委会主任会议成员莅临麒麟信安调研数字经济发展情况

v-if条件判断及v-show

Day117. Shangyitong: Generate registered order module

Greenplum数据库故障分析——can not listen port

【遥控器开发基础教程4】疯壳·开源编队无人机-SPI(OLED)

【多线程】Thread类的基本用法
随机推荐
【mysql知识点整理】--- order by 、group by 出现Using filesort原因详解
10. SAP ABAP OData 服务如何支持修改(Update)操作
中科磁业IPO过会:年营收5.5亿 吴中平家族持股85%
Oracle 暴跌,倒下了!
【软考 系统架构设计师】软件架构设计① 软件架构的概念
投资的思考
智能合约安全-可重入攻击(SW107-Reentrancy)
[NCTF2019]SQLi-1||SQL注入
letcode 第20题-有效的括号
RollBack Rx Professional RMC 安装教程
德邦科技通过注册:年营收5.8亿 国家集成电路基金为大股东
SAP ABAP Gateway Client 里 OData 测试的 PUT, PATCH, MERGE 请求有什么区别
精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
dataBinding的import导入
【飞控开发高级教程1】疯壳·开源编队无人机-飞控整机代码走读、编译与烧写
【SQL】—数据库操作、表操作
DB2数据库-获取表结构异常:[jcc][t4][1065][12306][4.26.14]CharConvertionException ERRORCODE=-4220,SQLSTATE=null
基于rt-thread studio的STM32裸机开发——LED
可编程逻辑控制器(PLC) : 基础、类型和应用
【遥控器开发基础教程5】疯壳·开源编队无人机-SPI(2.4G 双机通信)