认证源码分析与自定义后端认证逻辑

简介:

1.认证流程分析
UsernamePasswordAuthenticationFilter
先看主要负责认证的过滤器UsernamePasswordAuthenticationFilter,有删减,注意注释。
AuthenticationManager
由上面源码得知,真正认证操作在AuthenticationManager里面!
AbstractUserDetailsAuthenticationProvider
咱们继续再找到AuthenticationProvider的实现类AbstractUserDetailsAuthenticationProvider:
AbstractUserDetailsAuthenticationProvider
按理说到此已经知道自定义认证方法的怎么写了,但咱们把返回的流程也大概走一遍,上面不是说到返回了一个 UserDetails对象对象吗?
跟着它就又回到AbstractUserDetailsAuthenticationProvider对象中authenticate方法最后一行了。
UsernamePasswordAuthenticationToken
来到UsernamePasswordAuthenticationToken对象发现里面有两个构造方法
AbstractAuthenticationToken
再点进去super(authorities)看看:
由此,咱们需要牢记自定义认证业务逻辑返回的UserDetails对象中一定要放置权限信息! 现在可以结束源码分析了?先不要着急! 咱们回到最初的地方UsernamePasswordAuthenticationFilter,你看好看了,这可是个过滤器,咱们分析这么 久,都没提到doFilter方法,你不觉得心里不踏实?可是这里面也没有doFilter呀?那就从父类找!
AbstractAuthenticationProcessingFilter
点开AbstractAuthenticationProcessingFilter,删掉不必要的代码!
可见AbstractAuthenticationProcessingFilter这个过滤器对于认证成功与否,做了两个分支,成功执行 successfulAuthentication,失败执行unsuccessfulAuthentication。
在successfulAuthentication内部,将认证信息存储到了SecurityContext中。并调用了loginSuccess方法,这就是常见的“记住我”功能!此功能具体应用,咱们后续再研究!
2.自定义认证实现流程
1.实现UserDetailService与自定义逻辑
自定义用户类需要实现UserDetails接口,并实现接口的方法,所以我们编写下述代码。
2.注册自定义实现类
Java
运行代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.yzxb.SpringSecurity.config;

import com.yzxb.SpringSecurity.service.MyUserService;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import javax.annotation.Resource;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Resource
private MyUserService myUserService;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .anyRequest().authenticated()
            .and().formLogin().loginPage("/login.html")
            .loginProcessingUrl("/doLogin")
            .defaultSuccessUrl("/demo/index ")
            .failureUrl("/login.html")
            .usernameParameter("uname")
            .passwordParameter("passwd")
            .permitAll()
            .and()
            .csrf()
            .disable();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(myUserService);
}

}
改完的效果如下图
然后重启项目,就可以实现自定义的数据库认证逻辑。
3.完整代码获取
git仓库地址:https://github.com/Herbbbb/SpringSecurity.git
分支名称:Day02-用户自定义认证

相关文章
|
1月前
|
数据采集 Java Go
爬虫项目该选 Python 还是 Golang?看这篇就够了
本文对比Python与Golang在爬虫开发中的七大维度:语法简洁性、第三方库丰富度(如Scrapy vs Colly)、并发性能(Goroutine vs GIL限制)、内存占用、代码可读性、数据处理能力(Pandas等优势)及部署便捷性(Go可直接编译为跨平台二进制),助你按需选型。
194 10
|
3月前
|
安全 Java 应用服务中间件
4.认识SpringSecurity
Spring Security 是 Spring 生态中的安全框架,提供认证、授权及安全防护功能。支持多种认证方式(如表单、OAuth2、JWT等),基于过滤器链实现请求控制,可防御 CSRF 等攻击,保障 Web 应用安全。
|
安全 API Android开发
Android 中的FragmentManager
8月更文挑战第9天
465 1
|
Python
Python中类创建和实例化过程
Python中类创建和实例化过程
419 1
|
XML 数据格式 Python
【Python】已解决:FileNotFoundError: [Errno 2] No such file or directory: ‘./1.xml’
【Python】已解决:FileNotFoundError: [Errno 2] No such file or directory: ‘./1.xml’
941 0
|
安全 网络安全 数据安全/隐私保护
深入解析HTTPS:安全机制全方位剖析
深入解析HTTPS:安全机制全方位剖析
|
JSON 数据格式 Python
flask 接收get请求, 以及返回 json格式
flask 接收get请求, 以及返回 json格式
302 0
|
关系型数据库 PostgreSQL
Postgres psql: 致命错误: 角色 “postgres“ 不存在
Postgres psql: 致命错误: 角色 “postgres“ 不存在
1060 0
|
存储 SQL 算法
【案例实战】分布式应用下登录检验解决方案(JWT)
【案例实战】分布式应用下登录检验解决方案(JWT)
【案例实战】分布式应用下登录检验解决方案(JWT)
|
存储 安全 Java
SpringSecurity 核心过滤器——SecurityContextPersistenceFilter
SecurityContextHolder,这个是一个非常基础的对象,存储了当前应用的上下文SecurityContext,而在SecurityContext可以获取Authentication对象。也就是当前认证的相关信息会存储在Authentication对象中。
501 0

热门文章

最新文章