当前位置:网站首页>Chapter 17 oauth2loginauthenticationwebfilter source code analysis
Chapter 17 oauth2loginauthenticationwebfilter source code analysis
2022-07-18 11:18:00 【buffeer】
SpringBoot OAuth2 Client The authorization core of is implemented by two filters :
OAuth2AuthorizationRequestRedirectWebFilterOAuth2LoginAuthenticationWebFilter
OAuth2LoginAuthenticationWebFilter The filter is the protagonist of today's main analysis .
overview
OAuth2LoginAuthenticationWebFilter Filters depend on many classes :
- ReactiveAuthticationManager: obtain Access Token、 Get user information
- ServerOAuth2AuthorizedClientRepository
- ServerWebExchangeMatcher: Used to match the request path and intercept processing
- ServerAuthenticationSuccessHandler: Callback processing after successful authorization
- ServerAuthenticationFailureHandler: Callback processing after authorization failure
The following functions are realized :
- Authorization code exchange AccessToken
- AccessToken In exchange for user information ( nickname 、 Mailbox and so on )
The following mind map mainly describes OAuth2LoginAuthenticatioinWebFilter Dependent classes , It is convenient for us to have a clear and intuitive understanding of it .
Core source analysis
OAuth2LoginAuthticationWebFilter The filter inherits AuthenticationWebFilter . So its main function is in the parent class AuthenticationWebFilter Finish in .AuthenticationWebFilter It is mainly responsible for matching the request path and intercepting processing 、 Start certification 、 Callback after successful authentication 、 Callback after failure . The source code is shown below .
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
// from ServerWebExchangeMatcher Match the request path to be intercepted
return this.requiresAuthenticationMatcher.matches(exchange).filter((matchResult) -> {
return matchResult.isMatch();
}).flatMap((matchResult) -> {
return this.authenticationConverter.convert(exchange);
}).switchIfEmpty(chain.filter(exchange).then(Mono.empty())).flatMap((token) -> {
return this.authenticate(exchange, chain, token);
}).onErrorResume(AuthenticationException.class, (ex) -> {
// Throw out AuthenticationException abnormal Callback processing after authentication failure
return this.authenticationFailureHandler.onAuthenticationFailure(new WebFilterExchange(exchange, chain), ex);
});
}
private Mono<Void> authenticate(ServerWebExchange exchange, WebFilterChain chain, Authentication token) {
return this.authenticationManagerResolver.resolve(exchange).flatMap((authenticationManager) -> {
// from ReactiveAuthticationManager To be responsible for certification
return authenticationManager.authenticate(token);
}).switchIfEmpty(Mono.defer(() -> {
return Mono.error(new IllegalStateException("No provider found for " + token.getClass()));
})).flatMap((authentication) -> {
// Callback processing after successful authentication
return this.onAuthenticationSuccess(authentication, new WebFilterExchange(exchange, chain));
}).doOnError(AuthenticationException.class, (ex) -> {
logger.debug(LogMessage.format("Authentication failed: %s", ex.getMessage()));
});
}
If you summarize the above functions in one sentence : Match the request path to be intercepted , And start certification , Authentication successful call successful callback ; Authentication failure call failure callback .
Maybe you'll be curious : How to match request paths 、 What path should be blocked 、 What is the specific work of certification 、 How to deal with the success or failure of authentication . Next, let's analyze one by one .
边栏推荐
- How does wechat applet realize pull-down refresh?
- Solution of removing elements with double pointers in C language foundation
- Reading a data driven graph generic model for temporary interaction networks
- Filter
- 【开发教程6】疯壳·开源蓝牙智能健康手表-触摸
- socket
- Using off heap memory
- Experiment 5 image segmentation and description
- 我以为的软件项目管理
- Algorithm In Interview
猜你喜欢

曲伟海:坚持选择不放弃 是实现初心的法宝

Experiment 2 image enhancement

The Institute of standardization issued the requirements for cloud migration related capabilities, and Wanbo Intelligent Cloud participated in the requirements and white paper preparation

SQLyog无操作一段时间后重新操作会卡死问题(解决办法)

TCmalloc学习

openpcdet之pointpillar代码阅读——第二篇:网络结构

静态库和动态库

The digital tube displays numbers circularly

【JVM】垃圾收集器

Design of pattern water lamp based on 51 single chip microcomputer
随机推荐
How to display the prompt box when the wechat applet is loading?
IP static routing comprehensive experiment
How to install SQLite in Ubuntu without sudo permission
微信小程序如何实现下拉刷新?
Written at the end of the recruitment of Huazhong University of science and technology
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
Electric quantity indicating circuit based on ADC0832
Two bytes, carried out by the interviewer and shared with everyone
Filter
JS array object in goods_ With the same ID and an object, put goods_ size_ Add ID values together
openpcdet之pointpillar代码阅读——第二篇:网络结构
Double thread guessing numbers
Flink(三)处理函数
Sword finger offer 68 - I. nearest common ancestor of binary search tree
Offre de doigts 64. Trouver 1 + 2 +... + n
Ping message and ICMP message header
The relationship between loss function and maximum likelihood estimation | understanding of cross entropy
Design of pattern water lamp based on 51 single chip microcomputer
OLED circularly displays picture text
笔记