Spring Boot AuthenticationManager won't authenticate my user

Viewed 391

I'm trying to create a Spring Boot app which uses JDBC to get user details from MySQL database and then use JWT to validate requests. I understand that first user needs to login to get their request authenticated and then server will return JWT.

The below code snippet is I'm using now:

try {
    System.out.println("Before auth " + username + password);
    authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (DisabledException e) {
    System.out.println("USER_DISABLED");
    throw new Exception("USER_DISABLED", e);
} catch (BadCredentialsException e) {
    System.out.println("INVALID_CREDENTIALS");
    throw new Exception("INVALID_CREDENTIALS", e);
}
final UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUserName());
final String token = jwtTokenUtil.generateToken(userDetails);
return ResponseEntity.ok(new JWTToken(token));

After I run this and give the user details, only "Before auth " with the username and password will be logged. And there is nothing after that. No exception or anything else so I'm not sure what's stopped there.

Could anyone please guide me what should I do? Attached the webSecurityConfig class also:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.csrf().disable()
            // dont authenticate this particular request
            .authorizeRequests().antMatchers("/security/authenticate", "/user/register").permitAll().
            // all other requests need to be authenticated
                    anyRequest().authenticated().and().
            // make sure we use stateless session; session won't be used to
            // store user's state.
                    exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    // Add a filter to validate the tokens with every request
    httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
0 Answers
Related