UserDetailService接口:自定义逻辑控制认证
如果需自定义逻辑控制认证
创建类继承UsernamePasswordAuthenticationFilter,并重写
attemptAuthentication ,successfulAuthentication,unsuccessfulAuthentication方法
创建类实现UserDetailsService,编写查询数据过程,返回UserDetails对象,这个UserDetails对象时安全框架提供的对象
PasswordEncoder
用于返回的User对象里面密码的加密,sercuity中只认此加密方式。
!!以上是看了教学视频中大致仅仅粗略讲解的内容,但是为什么实现UserDetailService接口?为什么要继承UsernamePasswordAuthenticationFilter重写attemptAuthentication ,successfulAuthentication,unsuccessfulAuthentication方法呢?看看源码
上一篇说到在UsernamePasswordAuthenticationFilter的父类中AbstractAuthenticationProcessingFilter的dofilter方法中有这样的一段方法
在attemptAuthentication方法中如果抛出异常,那么会执行unsuccessfulAuthentication方法,成功则执行successfulAuthentication方法。因此需要重写attemptAuthentication去进