@Bean public UserDetailsService userDetailsService() { return new InMemoryUserDetailsManager(PasswordEncodedUser.user()); } }
public static User.UserBuilder withUsername(String username) { return builder().username(username); }
public static User.UserBuilder withUserDetails(UserDetails userDetails) { return User.withUserDetails(userDetails) .passwordEncoder(passwordEncoder()); }
@Test public void authenticateWhenSuccessThenSuccess() { UserDetails user = PasswordEncodedUser.withUsername(this.username) .password(this.password) .roles("USER") .build(); when(repository.findByUsername(user.getUsername())).thenReturn(Mono.just(user)); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); Authentication authentication = manager.authenticate(token).block(); assertThat(authentication).isEqualTo(authentication); }
public static User.UserBuilder builder() { return User.builder().passwordEncoder(passwordEncoder()); }
@Test public void authenticateWhenPasswordNotEqualThenBadCredentials() { UserDetails user = PasswordEncodedUser.withUsername(this.username) .password(this.password) .roles("USER") .build(); when(repository.findByUsername(user.getUsername())).thenReturn(Mono.just(user)); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, this.password + "INVALID"); Mono<Authentication> authentication = manager.authenticate(token); StepVerifier .create(authentication) .expectError(BadCredentialsException.class) .verify(); }
@Test public void delegateUsesExisitingAuthentication() { String username = "user"; String password = "password"; when(this.uds.loadUserByUsername(username)).thenReturn(PasswordEncodedUser.user()); AuthenticationManager authenticationManager = this.adapter.authenticationManager; assertThat(authenticationManager).isNotNull(); Authentication auth = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(username, password)); verify(this.uds).loadUserByUsername(username); assertThat(auth.getPrincipal()).isEqualTo(PasswordEncodedUser.user()); }
@Test public void getAuthenticationWhenUserDetailsServiceBeanThenAuthenticationManagerUsesUserDetailsServiceBean() throws Exception { this.spring.register(UserDetailsServiceBeanConfig.class).autowire(); UserDetailsService uds = this.spring.getContext().getBean(UserDetailsService.class); AuthenticationManager am = this.spring.getContext().getBean(AuthenticationConfiguration.class).getAuthenticationManager(); when(uds.loadUserByUsername("user")).thenReturn(PasswordEncodedUser.user(), PasswordEncodedUser.user()); am.authenticate(new UsernamePasswordAuthenticationToken("user", "password")); assertThatThrownBy(() -> am.authenticate(new UsernamePasswordAuthenticationToken("user", "invalid"))) .isInstanceOf(AuthenticationException.class); }
@Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser(PasswordEncodedUser.user()); } }
@Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser(PasswordEncodedUser.user()); }
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser(PasswordEncodedUser.user()); }
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser(PasswordEncodedUser.user()); } // @formatter:on
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser(PasswordEncodedUser.user()); } }
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser(PasswordEncodedUser.user()); } // @formatter:on