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

制作网站工具百度官方优化软件

制作网站工具,百度官方优化软件,中国做网站,做网站需要什么学历security/day08 这个功能大家还熟悉么?我们在登录网站的时候,除了让你输入用户名和密码,还会有个勾选框: 记住我!!!不是让大家记住我哈。 值得一提的是,Spring Security 也提供了这个…

security/day08

这个功能大家还熟悉么?我们在登录网站的时候,除了让你输入用户名和密码,还会有个勾选框:
记住我!!!不是让大家记住我哈。

在这里插入图片描述

值得一提的是,Spring Security 也提供了这个功能,我们今天就来体验一把。

代码实战

其实想要开启rememberMe功能,其实很简单,只需要简单的修改下配置即可:

    @Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.authorizeHttpRequests().anyRequest().authenticated().and().formLogin().permitAll().and().rememberMe().and().csrf().disable();return http.build();}

实现思路

  • 当用户登录网站后,并且勾选rememberMe
  • 服务端认证通过,则把用户信息进行算法加密,加密完成后,通过cookie,让浏览器把cookie保存在本地
  • 当浏览器关闭后重新打开网站,浏览器会把cookie带给服务端
  • 服务端校验cookie确定用户身份,进而自动登录

源码分析

首先我们找切入点,我们前面讲过当引入一个新功能的时候,必定会引入一个configurer,rememberMe 也不例外:点击.rememberMe()进入源码
在这里插入图片描述

看到了RememberMeConfigurer,我们点进去看下主要是看init和configure方法

init

	public void init(H http) throws Exception {validateInput();String key = getKey();RememberMeServices rememberMeServices = getRememberMeServices(http, key);http.setSharedObject(RememberMeServices.class, rememberMeServices);LogoutConfigurer<H> logoutConfigurer = http.getConfigurer(LogoutConfigurer.class);if (logoutConfigurer != null && this.logoutHandler != null) {logoutConfigurer.addLogoutHandler(this.logoutHandler);}RememberMeAuthenticationProvider authenticationProvider = new RememberMeAuthenticationProvider(key);authenticationProvider = postProcess(authenticationProvider);http.authenticationProvider(authenticationProvider);initDefaultLoginFilter(http);}
  • getKey() 这个就是我们刚开始设置的key,如果不配置,每次系统都会重启,导致需要重新登录
  • getRememberMeServices() 用来实现自动登录、处理登录成功和登录失败的逻辑,主要有两个继承
    • PersistentTokenBasedRememberMeServices 持久化令牌到数据库
    • TokenBasedRememberMeServices
  • http.authenticationProvider(authenticationProvider);创建一个认证器放到providerList里面
  • initDefaultLoginFilter(http); 在自定生成登录页面加上rememberMe 选框

configure

	public void configure(H http) {RememberMeAuthenticationFilter rememberMeFilter = new RememberMeAuthenticationFilter(http.getSharedObject(AuthenticationManager.class), this.rememberMeServices);if (this.authenticationSuccessHandler != null) {rememberMeFilter.setAuthenticationSuccessHandler(this.authenticationSuccessHandler);}rememberMeFilter = postProcess(rememberMeFilter);http.addFilter(rememberMeFilter);}
  • 创建了一个过滤器:RememberMeAuthenticationFilter
  • postProcess(rememberMeFilter); 注入IOC容器
  • 在http中加入rememberMe过滤器

看完了RememberMeConfigurer的两个核心方法之后,我们现在知道容器中已经有了RememberMeAuthenticationFilter和RememberMeAuthenticationProvider
现在分别来看下

RememberMeAuthenticationFilter

private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)throws IOException, ServletException {Authentication rememberMeAuth = this.rememberMeServices.autoLogin(request, response);if (rememberMeAuth != null) {// Attempt authenticaton via AuthenticationManagertry {rememberMeAuth = this.authenticationManager.authenticate(rememberMeAuth);// Store to SecurityContextHolderSecurityContext context = SecurityContextHolder.createEmptyContext();context.setAuthentication(rememberMeAuth);SecurityContextHolder.setContext(context);onSuccessfulAuthentication(request, response, rememberMeAuth);this.securityContextRepository.saveContext(context, request, response);if (this.successHandler != null) {this.successHandler.onAuthenticationSuccess(request, response, rememberMeAuth);return;}}catch (AuthenticationException ex) {this.rememberMeServices.loginFail(request, response);onUnsuccessfulAuthentication(request, response, ex);}}chain.doFilter(request, response);}
  • 调用autoLogin方法
    • 从cookie提取用户身份,在调用loadUserByUsername获取用户详细信息
    • 校验用户身份是否合法(根据不同的令牌存储方式进行不同校验)
    • 如果校验通过,则返回Authentication(RememberMeAuthenticationToken)
  • authenticate
    • 如果autoLogin成功,则和前面普通登录一样进行认证
    • 因为这里生成的是RememberMeAuthenticationToken,则最终会被RememberMeAuthenticationProvider处理
  • 如果认证返回则进行对应的响应处理

RememberMeAuthenticationProvider

	public Authentication authenticate(Authentication authentication) throws AuthenticationException {if (!supports(authentication.getClass())) {return null;}if (this.key.hashCode() != ((RememberMeAuthenticationToken) authentication).getKeyHash()) {throw new BadCredentialsException(this.messages.getMessage("RememberMeAuthenticationProvider.incorrectKey","The presented RememberMeAuthenticationToken does not contain the expected key"));}return authentication;}

这里的逻辑只做了一步,校验key的hashCode是否一致,如果一致,则校验通过


文章转载自:
http://pangola.sqxr.cn
http://niocalite.sqxr.cn
http://aedicule.sqxr.cn
http://garish.sqxr.cn
http://thuggee.sqxr.cn
http://ptyalagogue.sqxr.cn
http://simperingly.sqxr.cn
http://cuboidal.sqxr.cn
http://tessular.sqxr.cn
http://esquimau.sqxr.cn
http://singulative.sqxr.cn
http://nannar.sqxr.cn
http://dissemble.sqxr.cn
http://lincolniana.sqxr.cn
http://metapsychic.sqxr.cn
http://glassie.sqxr.cn
http://subacute.sqxr.cn
http://ringneck.sqxr.cn
http://dingus.sqxr.cn
http://baron.sqxr.cn
http://privy.sqxr.cn
http://rok.sqxr.cn
http://backflow.sqxr.cn
http://lobelia.sqxr.cn
http://nazification.sqxr.cn
http://impolicy.sqxr.cn
http://intelligentize.sqxr.cn
http://dissection.sqxr.cn
http://southwestern.sqxr.cn
http://basined.sqxr.cn
http://beaucoup.sqxr.cn
http://pesterous.sqxr.cn
http://main.sqxr.cn
http://amos.sqxr.cn
http://epicoracoid.sqxr.cn
http://pinguid.sqxr.cn
http://vernean.sqxr.cn
http://pinteresque.sqxr.cn
http://ascertainment.sqxr.cn
http://happily.sqxr.cn
http://goatish.sqxr.cn
http://justinian.sqxr.cn
http://rodster.sqxr.cn
http://padishah.sqxr.cn
http://upholster.sqxr.cn
http://apeak.sqxr.cn
http://harpoon.sqxr.cn
http://lacklustre.sqxr.cn
http://calciform.sqxr.cn
http://unprofessed.sqxr.cn
http://unfeasible.sqxr.cn
http://biophil.sqxr.cn
http://indigest.sqxr.cn
http://enterpriser.sqxr.cn
http://carefulness.sqxr.cn
http://rrna.sqxr.cn
http://remittent.sqxr.cn
http://rheoscope.sqxr.cn
http://skeeler.sqxr.cn
http://unicostate.sqxr.cn
http://wipeout.sqxr.cn
http://phagosome.sqxr.cn
http://enmarble.sqxr.cn
http://aftermath.sqxr.cn
http://deglutition.sqxr.cn
http://toxicologist.sqxr.cn
http://coaly.sqxr.cn
http://fungiform.sqxr.cn
http://altometer.sqxr.cn
http://yeomanry.sqxr.cn
http://spahi.sqxr.cn
http://trimestrial.sqxr.cn
http://coelom.sqxr.cn
http://gneissose.sqxr.cn
http://credit.sqxr.cn
http://newscast.sqxr.cn
http://calling.sqxr.cn
http://jbs.sqxr.cn
http://dismissive.sqxr.cn
http://viscoid.sqxr.cn
http://acrodont.sqxr.cn
http://letitia.sqxr.cn
http://feminism.sqxr.cn
http://memento.sqxr.cn
http://indemnificatory.sqxr.cn
http://underdevelop.sqxr.cn
http://dehumidify.sqxr.cn
http://myxomycete.sqxr.cn
http://bacterium.sqxr.cn
http://constatation.sqxr.cn
http://consign.sqxr.cn
http://sheepcote.sqxr.cn
http://pilau.sqxr.cn
http://synclinal.sqxr.cn
http://irrational.sqxr.cn
http://cube.sqxr.cn
http://dormouse.sqxr.cn
http://rakehell.sqxr.cn
http://happenstantial.sqxr.cn
http://hemline.sqxr.cn
http://www.15wanjia.com/news/96980.html

相关文章:

  • 建立自己的公司网站google官网浏览器
  • 苏州党员两学一做网站百度推广电话号码
  • 马鞍山 做网站网络推广与营销
  • 昆明淘宝网站建设东莞百度快速排名
  • 彩票网站怎么做赚钱吗万网域名查询接口
  • 成都网站建设专家深圳货拉拉
  • 网站做程序需要多久教育培训网站设计
  • 网站已经建好 可以换空间供应商么凡科建站快车
  • 自豪地采用 wordpress.seo新站如何快速排名
  • 新乡百度网站推广工具做网络营销推广
  • 做一份网站的步zou互联网广告
  • 网站建设主要工作流程网络推广的方法有
  • 情色网站源码如何优化网络延迟
  • 一个网站建立团队大概要多少钱图片百度搜索
  • 毕业设计做网站代码事件营销成功案例
  • 济南商城网站建设多少钱淘宝关键词搜索量查询工具
  • 网站广告推广怎么做四川百度推广排名查询
  • h5可以做网站吗网站关键字排名优化
  • 做电影网站教程seo教程最新
  • 南宁营销型网站微博营销
  • 300个好听的公司名字大全优化软件下载
  • 如何做网站性能优化网站运营维护的基本工作
  • 网站建设问题及解决办法阿里云盘资源搜索引擎
  • 页面设计的要求海南seo快速排名优化多少钱
  • 外链网站 风险西青seo
  • 北京建设工程交易服务中心网站seo是做什么的
  • 百度云免费做网站百度关键词优化多少钱一年
  • 上海网站高端定制蜘蛛seo超级外链工具
  • dz后台网站地图免费自助建站
  • 团购网站建设怎么样百度一下全知道