How to achieve both in-memory and database user authentication within project reactor with springboot3.2.2 (webflux)

14 Views Asked by At

I have my custom user entity that maps to a MySQL table. Of course it implements UserDetails.

@Table("users")
@JsonIgnoreProperties(value = {"password"})
@Builder
public class UserEntity extends BaseEntity implements UserDetails {
    public static enum ROLES {
        ROLE_ADMIN, ROLE_USER
    }
    private String username;
    private String firstname;
    private String lastname;
    private String email;
    private String password;
    private ROLES role;
    private Boolean enabled;
    .....
    }

I also have a customer ReactiveUserDetailsService

@Service
@Primary
@RequiredArgsConstructor
@Slf4j
public class CustomUserDetailsService implements ReactiveUserDetailsService {

    final UserRepository userRepository;
    @Override
    public Mono<UserDetails> findByUsername(String username) {
        return userRepository.getUserByUsername(username)
                .mapNotNull(r-> {
                    log.info(String.valueOf(r));
                    return (UserDetails) r;
                })
                .switchIfEmpty(Mono.error(new UsernameNotFoundException("User not found")));
    }
}

My scenario is, I need to have multiple SecurityWebFilterChain according to the two pathMatchers I have /api and /web

Where by the /api uses an inmemory user to authenticate whereas the /web uses database users to authenticate.

I am quite new to reactive and kindly require your assistance in achieving this.

My usecase strictly requires me to use both inmemory and database (dao).

Thank you in advance.

0

There are 0 best solutions below