Springboot + spring security, configure multiple data sources: verify different user tables

1. Introduction

As mentioned in the previous article, after configuring the source of jdbc and mybatis and logging in, the following error occurred!

The latter solution is: block one of the sources, log in successfully, and analyze the reason.

But, what if you need to configure multiple data sources? Because this kind of demand still exists in practice, for example, ordinary members and management system users should have different tables, then two sets of data sources are needed, so this problem must still be solved!

How to deal with it

First look at the source code, the screenshot is as follows:

Because in the source code, judged that if there are two UserDetailServices, the AuthenticationProvider will not be set for the global AuthenticationManager, because it is a provider corresponding to a UserDetailService. Now if you want two groups, you need to define the AuthenticationManager yourself.

2. Create a project

How to create a Spring Security project has been explained in the previous article, so I won’t repeat it here.

3. Code implementation

3.1 Define two groups of UserDetailService

Define two UserDetailsService, the code of the UserDetailService class is as follows:

@Bean
    public UserDetailsService userDetailsService1(){
        UserDetails userDetails = User.withUsername("memory1").password("{noop}memory1").roles("memory1").build();
        return new InMemoryUserDetailsManager(userDetails);
    }
    @Bean
    public UserDetailsService userDetailsService2(){
        UserDetails userDetails = User.withUsername("memory2").password("{noop}memory2").roles("memory2").build();
        return new InMemoryUserDetailsManager(userDetails);
    }

3.2 Define AuthenticationManager

Define AuthenticationManager, the code of AuthenticationManager class is as follows:

@Bean
    public AuthenticationManager authenticationManager(){
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setUserDetailsService(userDetailsService1());
        DaoAuthenticationProvider provider2 = new DaoAuthenticationProvider();
        provider2.setUserDetailsService(userDetailsService2());
        return new ProviderManager(provider2, provider);
    }

3.3 Authentication login

memory1, the screenshot is as follows:
memory2, the screenshot is as follows:

So far, configuring multiple data sources is handled.

“The Shawshank Redemption”

Life boils down to a simple choice: get busy living, or get busy dying.

Cowardice imprisons the soul, hoping to feel free. The strong save themselves, the saint saves others.

Hope is a beautiful thing, perhaps the best of things. Good things never die.

Every man is his own god. If you give up on yourself, who else will save you?

“furnace”

We fight all the way, not to change the world, but to prevent the world from changing us.

Reality is like a jellyfish, its seemingly benign and harmless reality is always deadly.

When we come to the world, we all travel alone. Even if there are people around us, we will eventually go our separate ways!

The most beautiful and precious things in the world are inaudible and invisible, and can only be felt with the heart.

“godfather”

People can make mistakes all the time, but they can never make fatal mistakes.

Don’t hate your enemies, it will cloud your judgment.

People are not born great, but the more they live, the greater they become.

“Alive”

People live to live themselves, not to live for anything other than life.

To cry with laughter, to live with death.

There is nothing more telling than time, because time can change everything without telling us.

Your life is given by your parents, if you want to die, you have to ask them first.

“I’m Not the God of Medicine”

There is only one disease in the world, the disease of poverty, and you have no cure for this disease, nor can you cure it.

The most noble thing in the world is kindness, which is a tribute to life.

The Lord of the Rings

Hold your hand tightly, and there is nothing inside; let go of your hand, and you get everything.

I would rather spend the short life of a mortal with you than see the vicissitudes of the world alone.

20. All happy families are alike, but every unhappy family is unhappy in its own way.

There may be a day when human beings become shrunken and cowardly, abandoning friends and severing friendships, but not today.
“Eating Men and Women”

22. Life can’t be like cooking, you have to prepare all the ingredients before cooking.

What is “pity”? Only when you have the word “pity” in your heart can you know pity.

In fact, a family, living under the same roof, can still live their own lives, but the scruples that arise from the heart are what makes a home a home.

“Let the bullets fly”

There is no road in the world, but there is a road when there are legs.

If you live, you will die sooner or later; if you die, you will live forever.

Make money, don’t be shabby

“The Life of the Disliked Pine Nut”

When you were young, everyone felt that their future was shining brightly, didn’t they? But once you grow up, nothing will go your way.

Either sighed sadly, or indulged in sadness, or ended his life hastily, or laughed and prevaricated, and made mistakes.

The human heart is fragile, and it is difficult to pray for the person you hate. Forgiving the unforgivable and praying for it is the love of God.

I want to leave evidence of my life in this world, because I was born human after all.

“Once Upon a Time in America”

No matter when and where, it’s never too late to do what you want to do.

Loneliness is not terrible, what is terrible is the fear of loneliness.

You have to keep moving forward, otherwise you won’t know what else life will give you.

“The Fantasy Drifting of Boy Pi”

Here we must speak of fear, which is life’s only real opponent, because only fear can defeat life.

Life is constantly letting go, but the most regrettable thing is that we have no time to say goodbye!

If every transition we experience in life takes us further in life, then we truly experience what life wants us to experience.

“Rashomon”

A self-satisfied person only cares about himself, but making irresponsible remarks is meddling.

We come to a compromise with what “can” get rather than what we want.

Lying is human nature, and most of the time we can’t even be honest with ourselves. That’s because people are too vulnerable to lie, even to themselves.

“Forrest Gump”

Life is like a box of chocolates, you never know what you’re going to get.

“What kind of person do you want to be in the future?”
“What do you mean, can’t I be myself in the future?”

I don’t think people’s mental maturity is more and more tolerant and encompassing, and everything can be accepted. On the contrary, I think it should be a process of gradually eliminating, knowing what is most important to oneself, and knowing what is not important. Then, be a simple person.

A little wealth is all a man really needs, and the rest is for showing off.