How to get a user registered through InMemoryAuthentication using Spring Security?

I have a Spring MVC web application protected by Spring Security and am in the process of writing tests. I am trying to get one of my (user) users obtained using Spring Security in my SecurityContextHolder. Once my user is "inserted" (configured on java) with:

auth.inMemoryAuthentication().getUserDetailsService().createUser(myCustomUser);

Then I can create a related token (UsernamePasswordAuthenticationToken) and ask Spring to authenticate my user with that token. The problem is that Spring does not retrieve the user instance user , but an instance of the User class. When Spring searches for such a user in the following method (from Spring InMemoryUserDetailsManager):

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDetails user = users.get(username.toLowerCase());

        if (user == null) {
            throw new UsernameNotFoundException(username);
        }

        return new User(user.getUsername(), user.getPassword(), user.isEnabled(), user.isAccountNonExpired(),
                user.isCredentialsNonExpired(), user.isAccountNonLocked(), user.getAuthorities());
    }

He creates a new user with the details provided by my configuration.

I do not see a problem with the fact that InMemoryUserDetailsManager directly returns what was sent to it via the getUserDetailsService () call. createUser, but it should be possible ... Anyway, I'm probably doing something wrong, any idea?

+4
1

, , InMemoryUserDetailsManager, Spring .
- , , .

+5

Source: https://habr.com/ru/post/1532870/


All Articles