Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use distributed object SharedObject

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use distributed object SharedObject". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's idea to study and learn "how to use distributed object SharedObject".

1. Preface

In the previous article, we analyzed the details of the initialization of AuthenticationManager, and there was a piece of code that attracted the attention of many students:

ApplicationContext context = http.getSharedObject (ApplicationContext.class); CaptchaAuthenticationProvider captchaAuthenticationProvider = context.getBean ("captchaAuthenticationProvider", CaptchaAuthenticationProvider.class)

The above gets Spring's application context object ApplicationContext directly from the HttpSecurity object. How does it do that? what is the concept of SharedObject? Let's find out this problem today.

2. SharedObject

In Spring Security, SharedObject is neither an object nor an interface, but a general term for a certain class of "shareable" objects.

As the name implies, SharedObject means an object that can be shared. What it does is to turn some objects into SharedObject if you want to share them in different scope configurations, which feels a bit like distributed objects. To make it easier for you to understand, here is the relevant architecture:

Organizational structure of the configuration class

Only the implementation class of AbstractConfiguredSecurityBuilder or HttpSecurityBuilder has the ability to manipulate SharedObject. One is to register SharedObject, and the other is to get SharedObject.

Registration of SharedObject

SharedObject stores to a HashMap with its Class type as Key and the instance as Value

Inject AuthenticationManagerBuilder

The AuthenticationManagerBuilder as we know it is shared here.

Another part is registered when all HttpSecurityBuilder objects are initialized. It is initialized and configured by SecurityConfigurer:

Public interface SecurityConfigurer {void init (B builder) throws Exception; void configure (B builder) throws Exception;}

The above two methods are used to initialize and configure HttpSecurityBuilder, respectively. For example, the well-known WebSecurityConfigurerAdapter is used to configure HttpSecurity, and the relevant code can be found in its init method:

Private Map, Object > sharedObjects = new HashMap (); sharedObjects.putAll (localConfigureAuthenticationBldr.getSharedObjects ()); sharedObjects.put (UserDetailsService.class, userDetailsService ()); sharedObjects.put (ApplicationContext.class, context); sharedObjects.put (ContentNegotiationStrategy.class, contentNegotiationStrategy); sharedObjects.put (AuthenticationTrustResolver.class, trustResolver); return sharedObjects;}

This is the fundamental reason why I can get ApplicationContext at the beginning of the article.

Acquisition and use of SharedObject

What can we get marked as SharedObject classes? SecurityConfigurer has many implementations that are used to configure specific functions related to authentication and authorization. For example, OAuth3ClientConfigurer is used to configure the OAuth3 client. It sets some commonly used objects to SharedObject:

Public OAuth3ClientConfigurer clientRegistrationRepository (ClientRegistrationRepository clientRegistrationRepository) {Assert.notNull (clientRegistrationRepository, "clientRegistrationRepository cannot be null"); this.getBuilder () .setSharedObject (ClientRegistrationRepository.class, clientRegistrationRepository); return this;}

When you need to use ClientRegistrationRepository elsewhere in your HttpSecurity configuration, you can get it directly through getSharedObject, just like at the beginning of the article, without having to write some acquisition methods.

Thank you for reading, the above is the content of "how to use distributed object SharedObject", after the study of this article, I believe you have a deeper understanding of how to use distributed object SharedObject, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report