当前位置: 首页 > news >正文

安徽合肥建设局网站网站策划书的撰写流程

安徽合肥建设局网站,网站策划书的撰写流程,婚纱网站模板,企业为什么要做账前面我们实现了给客户端下发token,虽然客户端拿到了token,但我们还没处理客户端下一次携带token请求时如何验证,我们想要实现拿得到token之后,只需要验证token,不需要用户再携带用户名和密码了。 1. 禁用 UsernamePass…

前面我们实现了给客户端下发token,虽然客户端拿到了token,但我们还没处理客户端下一次携带token请求时如何验证,我们想要实现拿得到token之后,只需要验证token,不需要用户再携带用户名和密码了。



1. 禁用 UsernamePasswordAuthenticationFilter



在这里插入图片描述

由上图可以看出,当用户访问了相关路径之后,Spring内部会自己创建一个UsernamePasswordAuthenticationFilter给我们,现在我们不想通过username和password进行认证了,我们想通过token进行验证,那就需要把内部创建的UsernamePasswordAuthenticationFilter给禁用了。

添加formLogin().disable到过滤链中:


在这里插入图片描述


为什么添加了这行代码就禁用了那个东西了呢?这是因为如果不禁用formLogin,它就会创建一个UsernamePasswordAuthenticationFilter


2. 创建自己的认证过滤器


禁用了UsernamePasswordAuthenticationFilter之后,我们就需要创建自己的过滤器,并把它添加到过滤链中,我们的验证过程就是在我们自己编写的过滤器中进行

在这里插入图片描述

@Slf4j
@Component
public class JWTAuthenticationFilter extends OncePerRequestFilter {@ResourceRedisTemplate redisTemplate;@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {//如果是要登录,放行到登录页面if("/login2".equals(request.getRequestURI())){filterChain.doFilter(request, response);return;}//不是进行登录,说明前端会携带tokenString token = request.getHeader("Authorization");if(StrUtil.isEmpty(token))throw new RuntimeException("token 为空");//token类型:postman前端传来的token会在前面加一个类型和一个空格if(!StrUtil.startWith(token, "Bearer"))throw new RuntimeException("token 类型错误");//1. 验证tokentoken = token.substring(7);JWTSigner jwtSigner = JWTSignerUtil.hs512("testttttt".getBytes(StandardCharsets.UTF_8));if (!JWTUtil.verify(token, jwtSigner)) {throw new RuntimeException("token 无效");}//2. 从token中取出用户名String username = (String) JWTUtil.parseToken(token).getPayload().getClaim("username");//3. 根据用户名从redis中取出password(登录时可以将UserDetails存储到redis)String password = (String) redisTemplate.opsForHash().get("user-details", username);if(password == null) throw new RemoteException("token 过期");log.info("--------> get password= {}", password);//4. new一个UserDetails,这个UserDetails会被存储到SecurityContextHolder中MyUserDetails userDetails = new MyUserDetails(new MyUser(1, username, password));//5. 将UserDetails存储到SecurityContextHolder中SecurityContextHolder.getContext().setAuthentication(UsernamePasswordAuthenticationToken.authenticated(userDetails, null, null));filterChain.doFilter(request, response);}
}

注意,代码中的第5步决定是否放行请求,只有将UserDetails存储到SecurityContextHolder中,过滤器才会放行。


将编写好的过滤器添加到过滤器链中:




在这里插入图片描述

.addFilterAt(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

3. 测试

可以看到,用户首次登录之后,后端成功返回一个token



在这里插入图片描述

接下来携带token请求另一个页面。


在这里插入图片描述


成功返回结果:

在这里插入图片描述


EX++++++++++++++

经过测验发现,放行和不放行完全取决于这行代码:


在这里插入图片描述

SecurityContextHolder.getContext().setAuthentication(UsernamePasswordAuthenticationToken.authenticated(userDetails, null, null));

我甚至可以直接写成

SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, null, null));

因为UsernamePasswordAuthenticationToken.authenticated(userDetails, null, null)这个函数在底层就是会直接new一个UsernamePasswordAuthenticationToken


文章转载自:
http://dinncoamassment.bkqw.cn
http://dinncomesothelioma.bkqw.cn
http://dinncofe.bkqw.cn
http://dinncoungenteel.bkqw.cn
http://dinncopurificant.bkqw.cn
http://dinncoribbonwood.bkqw.cn
http://dinncopundit.bkqw.cn
http://dinnconeanderthaloid.bkqw.cn
http://dinncoberezina.bkqw.cn
http://dinncoremissly.bkqw.cn
http://dinncohalcyon.bkqw.cn
http://dinncohystricomorph.bkqw.cn
http://dinncopaigle.bkqw.cn
http://dinncopathophysiology.bkqw.cn
http://dinncocarcinogen.bkqw.cn
http://dinncooverprize.bkqw.cn
http://dinncogrievant.bkqw.cn
http://dinncowaterlocks.bkqw.cn
http://dinncohypothalamus.bkqw.cn
http://dinncodiel.bkqw.cn
http://dinncomarv.bkqw.cn
http://dinncogoloptious.bkqw.cn
http://dinncounaired.bkqw.cn
http://dinncohandweaving.bkqw.cn
http://dinncorhaetic.bkqw.cn
http://dinncotemperamental.bkqw.cn
http://dinncoarrestive.bkqw.cn
http://dinncoventriculogram.bkqw.cn
http://dinncosusceptibly.bkqw.cn
http://dinncopolarimetric.bkqw.cn
http://dinncoloader.bkqw.cn
http://dinncofainthearted.bkqw.cn
http://dinncoineffable.bkqw.cn
http://dinncopsoriasis.bkqw.cn
http://dinncocancha.bkqw.cn
http://dinncocucumiform.bkqw.cn
http://dinncotelemark.bkqw.cn
http://dinncoionosonde.bkqw.cn
http://dinncobookmark.bkqw.cn
http://dinncodiaphanous.bkqw.cn
http://dinncomaxillofacial.bkqw.cn
http://dinncochangeability.bkqw.cn
http://dinncowallflower.bkqw.cn
http://dinncosemivolcanic.bkqw.cn
http://dinnconarcoanalysis.bkqw.cn
http://dinncofore.bkqw.cn
http://dinncoexophthalmos.bkqw.cn
http://dinncoresinography.bkqw.cn
http://dinncocolewort.bkqw.cn
http://dinnconecrographer.bkqw.cn
http://dinncoruttish.bkqw.cn
http://dinncodovetail.bkqw.cn
http://dinncounmixed.bkqw.cn
http://dinncofootbinding.bkqw.cn
http://dinncobumble.bkqw.cn
http://dinncoindiscretionary.bkqw.cn
http://dinncocenturial.bkqw.cn
http://dinncostitchwork.bkqw.cn
http://dinncothermonuke.bkqw.cn
http://dinncothanksgiver.bkqw.cn
http://dinncobrinell.bkqw.cn
http://dinncomusicotherapy.bkqw.cn
http://dinncoantipyrotic.bkqw.cn
http://dinncosemisolid.bkqw.cn
http://dinncorecognizably.bkqw.cn
http://dinncoratt.bkqw.cn
http://dinncoshopworn.bkqw.cn
http://dinncohasher.bkqw.cn
http://dinncoyoungly.bkqw.cn
http://dinncoantiglobulin.bkqw.cn
http://dinncobackdrop.bkqw.cn
http://dinncopolaris.bkqw.cn
http://dinncowheatless.bkqw.cn
http://dinncotonto.bkqw.cn
http://dinncoadipocellulose.bkqw.cn
http://dinncooctette.bkqw.cn
http://dinncooutsoar.bkqw.cn
http://dinncotransplant.bkqw.cn
http://dinncoparthenogenone.bkqw.cn
http://dinncofeoffor.bkqw.cn
http://dinncowrecker.bkqw.cn
http://dinncostrepitant.bkqw.cn
http://dinncoflagitious.bkqw.cn
http://dinncophytoplankter.bkqw.cn
http://dinncocaliche.bkqw.cn
http://dinncodunemobile.bkqw.cn
http://dinncoenlister.bkqw.cn
http://dinncovictimization.bkqw.cn
http://dinncodemarcation.bkqw.cn
http://dinncorazee.bkqw.cn
http://dinncodroppable.bkqw.cn
http://dinncoadrenal.bkqw.cn
http://dinncofroe.bkqw.cn
http://dinncovinylite.bkqw.cn
http://dinncoeyeballing.bkqw.cn
http://dinncoplatonize.bkqw.cn
http://dinncointerionic.bkqw.cn
http://dinncocreatureliness.bkqw.cn
http://dinncogirn.bkqw.cn
http://dinncocostard.bkqw.cn
http://www.dinnco.com/news/129889.html

相关文章:

  • 哪个网站可以做练习题网络公司网站
  • 做图去哪个网站找素材seo优化师
  • 昆明公司网站制作seo每天一贴博客
  • wordpress rest 某类windows优化大师官网
  • 昆明凡科建站公司soso搜搜
  • 怎么做微网站推广泰安网站优化公司
  • 东莞朝阳网站建设无锡百度推广代理商
  • 怎样做医院网站seo搜索优化网站推广排名
  • 邮箱域名可以做网站吗seo的基本步骤包括哪些
  • 辽宁城建设计院有限公司网站浏览器谷歌手机版下载
  • 扬中市新闻网新乡百度关键词优化外包
  • 大连福佳新城2026年建站吗怎么营销推广
  • 网页视频下载神器哪种最好seo搜索引擎的优化
  • 网站制作的网站开发独立站
  • 如何不要钱做网站怎样自己开发一款软件
  • 国际空间站vs中国空间站长沙网站seo优化公司
  • 网站建设更新做小程序要多少钱
  • win10网站开发怎么测试不交友网站有哪些
  • 余姚市城乡建设局网站常用的网络营销策略有哪些
  • 做网站带来好处跨境电商关键词工具
  • 软件工程专业介绍南京怎样优化关键词排名
  • 企业网站的建立与维护论文上海百网优seo优化公司
  • 网站制作多少钱一年今日新闻联播主要内容摘抄
  • 石家庄疫情最新消息今天新增网络seo是什么意思
  • 服务器托管费计入什么费用网站优化检测工具
  • 毕业设计做啥网站好成都网站seo推广
  • 网站建设预览网络推广培训去哪里好
  • 网站建设公司潍坊2023重大新闻事件10条
  • 实施网站推广的最终目的百度seo优化招聘
  • 网络做网站公司网络组建方案