I'm developing a RESTful web service and had login working. I wanted to add security and access tokens so I added a UserDetailsService
as seen below:
@Component
public class CustomLoginAuthenticationProvider implements UserDetailsService {
@Autowired
private BusinessUserService businessUserService;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
if(email == null || email.isEmpty() || !email.contains("@")) {
System.out.println("ERROR - THIS IS THE USERNAME:" + email);
throw new UsernameNotFoundException(email);
}
//... More below, but it doesn't matter. Exception thrown every time
}
However, the email string is empty. I can't understand why because I'm having a hard time understanding exactly when this method is called and what value is sent as the parameter for this method as this is a REST back end being sent JSON. Here's my WebSecurityConfigurerAdapter
setup:
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Bean
public CustomLoginAuthenticationProvider customLoginAuthenticationProvider() {
return new CustomLoginAuthenticationProvider();
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/images/**", "/fonts/**",
"/videos/**", "/", "/register", "/login", "/about",
"/contact", "/test")
.permitAll()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.usernameParameter("email")
.passwordParameter("password")
.and()
.logout()
.logoutSuccessUrl("/")
.permitAll()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.addFilterAfter(new CsrfTokenFilter(), CsrfFilter.class);
}
}
I specify that an email should be sent to the method when I use .usernameParameter("email")
so I'm not really sure why it's not populating the email parameter. I'm using AngularJS in the front end and sending the credentials to the back end using JSON.