spring sercuity学习笔记(二)(自定义逻辑控制认证原理)

本文探讨了在Spring Security中如何自定义认证逻辑。通过继承UsernamePasswordAuthenticationFilter并重写attemptAuthentication、successfulAuthentication和unsuccessfulAuthentication方法,可以实现自定义的登录验证。同时,实现UserDetailsService接口用于查询用户数据并返回UserDetails对象。PasswordEncoder用于加密密码。在认证过程中,AbstractAuthenticationProcessingFilter的doFilter方法和ProviderManager的authenticate方法协同工作,通过UserDetailsService获取用户信息,从而完成认证流程。
摘要由CSDN通过智能技术生成

UserDetailService接口:自定义逻辑控制认证

如果需自定义逻辑控制认证
创建类继承UsernamePasswordAuthenticationFilter,并重写
attemptAuthentication ,successfulAuthentication,unsuccessfulAuthentication方法
创建类实现UserDetailsService,编写查询数据过程,返回UserDetails对象,这个UserDetails对象时安全框架提供的对象

PasswordEncoder
用于返回的User对象里面密码的加密,sercuity中只认此加密方式。

!!以上是看了教学视频中大致仅仅粗略讲解的内容,但是为什么实现UserDetailService接口?为什么要继承UsernamePasswordAuthenticationFilter重写attemptAuthentication ,successfulAuthentication,unsuccessfulAuthentication方法呢?看看源码

上一篇说到在UsernamePasswordAuthenticationFilter的父类中AbstractAuthenticationProcessingFilter的dofilter方法中有这样的一段方法
在这里插入图片描述
在attemptAuthentication方法中如果抛出异常,那么会执行unsuccessfulAuthentication方法,成功则执行successfulAuthentication方法。因此需要重写attemptAuthentication去进

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值