当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢
公司招个程序员,34岁以上两年一跳的不要,开出工资以为看错了
mysql容器数据卷持久化
科捷智能冲刺科创板:年营收12.8亿 顺丰与日日顺是股东
alibaba数据同步组件canal的实践整理
全栈---CORS
心电记录电路设计(框图/波形以及信号放大器的选择)
Auto.js special positioning control method cannot perform blocking operations on the ui thread, please use setTimeout instead
华为防火墙双机热备技术:HRP、VGMP、VRRP,三大技术值得一学!
OpenWRT设置ipv6网络
爆款视频怎么做?这里或许有答案
随机推荐
全栈---Proxy
JSP第一篇 -----JSP九大内置对象(隐式对象)和四大域对象
C语言:链表
为了面试阿里,熬夜肝完这份软件测试笔记后,Offer终于到手了
作业8.2 线程同步互斥机制——互斥锁
从一文中了解SSRF的各种绕过姿势及攻击思路
DB2数据库-获取表结构异常:[jcc][t4][1065][12306][4.26.14]CharConvertionException ERRORCODE=-4220,SQLSTATE=null
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第一节:深度学习概论
async-await
德邦科技通过注册:年营收5.8亿 国家集成电路基金为大股东
Jenkins汉化设置
SAP ABAP Gateway Client 里 OData 测试的 PUT, PATCH, MERGE 请求有什么区别
Auto.js 特殊定位控件方法 不能在ui线程执行阻塞操作,请使用setTimeout代替
Linear DP
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
php提示Array to string conversion
可编程逻辑控制器(PLC) : 基础、类型和应用
科捷智能冲刺科创板:年营收12.8亿 顺丰与日日顺是股东
淘宝商品销量接口/淘宝商品销量监控接口/商品累计销量接口代码对接分享
RollBack Rx Professional RMC 安装教程