首页 > 作文

SpringSecurity+Redis认证过程小结

更新时间:2023-04-04 10:20:02 阅读: 评论:0

由于今天用curity进行权限管理的时候出现了一些bug,特此发这篇博客来补习一下对springcurity的理解

前言引入

当今市面上用于权限管理的流行的技术栈组合是

ssm+shriospringcloud+springboot+springcurity

这种搭配自然有其搭配的特点,由于springboot的自动注入配置原理,在创建项目时就自动注入管理springcurity的过滤器容器(delegatingfilterproxy),而这个过滤器是整个springrcurity的核心。掌握着springrcurity整个权限认证过程,而springboot很香的帮你将其自动注入了,而用ssm
去整合curity,将会耗用大量的配置文件,不易于开发,而curity的微服务权限方案,更是能和cloud完美融合,于是curity比shrio更强大,功能更齐全。

curity的核心配置赞美老师的名言诗句文件

核心:class curityconfig extends webcurityconfigureradapter

继承了webcurityconfigureradapter后我们关注于configure方法对于在整个安全认证的过程进行相关的配置,当然在配置之前我们先简单了解一下流程

简单的看了整个权限认证的流程,很轻易的总结得出,springcurity核心的就是以下几种配置项了

拦截器(interceptor)过滤器(filter)处理器(handler,异常处理器,登录成功处理器)

那我们就首先通过配置来完成认500亩是多少平方米证过程吧!!!!

curity的认证过程

假设我们要实现一下的认证功能

1. 是登录请最大无关组求

我们需要先判断验证码是否正确(验证码过滤器,通过addfilerbefore实现前置拦截)再判断用户名密码是否正确(使用自带的用户名密码过滤器,urnamepasswordauth繁文缛节什么意思enticationfilter)配置异常处理器(handler)通过io流将异常信息写出

关于密码校验的流程:
urnamepasswordauthenticationfilter的密码校验规则是基于authenticationmanagerbuilder(认证管理器)下的 urdetailsrvice里的规则进行验证的:
其中的核心方法:

1.public urdetails *loadurbyurname(string urname)
通过请求参数的用户名去数据库查询是否存在,存在则将其封装在urdetails里面,而验证过程是通过authenticationmanagerbuilder获取到urdetail里的urname和password来校验的,
这样我们就可以通过

配置yaml文件设置账号密码通过数据库结合urdetail来设置账号密码

(urdetailsrvice中的方法,注意需要将urdetailsrvice注入authenticationmanagerbuilder中)

@overridepublic urdetails loadurbyurname(string urname) throws urnamenotfoundexception {sysur sysur = sysurrvice.getbyurname(urname);if (sysur == null) {throw new urnamenotfoundexception("用户名或密码不正确");}// 注意匹配参数,前者是明文后者是暗纹system.out.println("是否正确"+bcryptpasswordencoder.matches("111111",sysur.getpassword()));return new accountur(sysur.getid(), sysur.geturname(), sysur.getpassword(), geturauthority(sysur.getid()));}

通过了这个验证后,过滤器放行,不通过就用自定义或者默认的处理器处理

核心配置文件:

package com.markerhub.config;import com.markerhub.curity.*;import org.springframework.beans.factory.annotation.autowired;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;import org.springframework.curity.config.annotation.authentication.builders.authenticationmanagerbuilder;import org.springframework.curity.config.annotation.method.configuration.enableglobalmethodcurity;import org.springframework.curity.config.annotation.web.builders.httpcurity;import org.springframework.curity.config.annotation.web.configuration.enablewebcurity;import org.springframework.curity.config.annotation.web.configuration.webcurityconfigureradapter;import org.springframework.curity.config.http.ssioncreationpolicy;import org.springframework.curity.crypto.bcrypt.bcryptpasswordencoder;import org.springframework.curity.web.authentication.urnamepasswordauthenticationfilter;@configuration@enablewebcurity@enableglobalmethodcurity(prepostenabled = true)public class curityconfig extends webcurityconfigureradapter {@autowiredloginfailurehandler loginfailurehandler;@autowiredloginsuccesshandler loginsuccesshandler;@autowiredcaptchafilter captchafilter;@autowiredjwtauthenticationentrypoint jwtauthenticationentrypoint;@autowiredjwtaccessdeniedhandler jwtaccessdeniedhandler;@autowiredurdetailrviceimpl urdetailrvice;@autowiredjwtlogoutsuccesshandler jwtlogoutsuccesshandler;@beanjwtauthenticationfilter jwtauthenticationfilter() throws exception {jwtauthenticationfilter jwtauthenticationfilter = new jwtauthenticationfilter(authenticationmanager());return jwtauthenticationfilter;}@beanbcryptpasswordencoder bcryptpasswordencoder() {return new bcryptpasswordencoder();}private static final string[] url_whitelist = {"/login","/logout","/captcha","/favicon.ico",};protected void configure(httpcurity http) throws exception {http.cors().and().csrf().disable()// 登录配置.formlogin().successhandler(loginsuccesshandler).failurehandler(loginfailurehandler).and().logout().logoutsuccesshandler(jwtlogoutsuccesshandler)// 禁用ssion.and().ssionmanagement().ssioncreationpolicy(ssioncreationpolicy.stateless)// 配置拦截规则.and().authorizerequests().antmatchers(url_whitelist).permitall().anyrequest().authenticated()// 异常处理器.and().exceptionhandling().authenticationentrypoint(jwtauthenticationentrypoint).accessdeniedhandler(jw关于爱情伤感的诗句taccessdeniedhandler)// 配置自定义的过滤器.and().addfilter(jwtauthenticationfilter()).addfilterbefore(captchafilter, urnamepasswordauthenticationfilter.class);}@overrideprotected void configure(authenticationmanagerbuilder auth) throws exception {auth.urdetailsrvice(urdetailrvice);}}

2. 不是登录请求

通过jwtffilter来查看是否为登录状态

使用redis整合时的注意事项

本质上还是编写过滤器链:

在登录请求前添加过滤器注意验证码存储在redis的失效时间,如果超过失效时间将会被验证码拦截器拦截下来需要准备一个生成验证码的接口,存储在redis中使用完验证码需要将其删除
// 校验验证码逻辑private void validate(httprvletrequest httprvletrequest) {string code = httprvletrequest.getparameter("code");string key = httprvletrequest.getparameter("token");if (stringutils.isblank(code) || stringutils.isblank(key)) {system.out.println("验证码校验失败2");throw new captchaexception("验证码错误");}system.out.println("验证码:"+redisutil.hget(const.captcha_key, key));if (!code.equals(redisutil.hget(const.captcha_key, key))) {system.out.println("验证码校验失败3");throw new captchaexception("验证码错误");}// 一次性使用redisutil.hdel(const.captcha_key, key);}

到此这篇关于springcurity+redis认证过程总结的文章就介绍到这了,更多相关springcurity redis认证内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 10:20:00,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/b8d5abe274fd77c523cd860d4a2bd288.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:SpringSecurity+Redis认证过程小结.doc

本文 PDF 下载地址:SpringSecurity+Redis认证过程小结.pdf

标签:验证码   过滤器   密码   处理器
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图