title: Any example of jwt authenticating with mysql in spring boot?
body: <p>I am new to using jwt authentication with spring for rest apis.
  I made an adaptation of an authentication example but I am not at all sure that the way I implement it is correct.</p>
  <pre><code>@RestController
  </code></pre>
  <p>@CrossOrigin
  public class JwtAuthenticationController {</p>
  <pre><code>@Autowired
  private AuthenticationManager authenticationManager;
  @Autowired
  private JwtTokenUtil jwtTokenUtil;
  @Autowired
  private JwtUserDetailsService userDetailsService;
  @Autowired
  private JwtRequestRepository jwtRequestRepository;
  
  @RequestMapping(value = &quot;/authenticate&quot;, method = RequestMethod.POST)
  public ResponseEntity&lt;?&gt; createAuthenticationToken(@RequestBody JwtRequest authenticationRequest) throws Exception 
  {
      String CREDENTIALS = &quot;INVALID_CREDENTIALS&quot;;
      
      if(jwtRequestRepository.findByUsernameAndPassword(authenticationRequest.getUsername(), authenticationRequest.getPassword())!=null) {
          CREDENTIALS = authenticationRequest.getUsername();
      }
      //authenticate(authenticationRequest.getUsername(),authenticationRequest.getPassword());
      final UserDetails userDetails = 
       userDetailsService.loadUserByUsername(CREDENTIALS);
      //JwtUserDetails userDetails = new JwtUserDetails();
      //userDetails.setUsername(authenticationRequest.getUsername());
          
          
      final String token = jwtTokenUtil.generateToken(userDetails);
      return ResponseEntity.ok(new JwtResponse(token));
  }
  
  private void authenticate(String username, String password) throws Exception {
      try {
          authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
      } catch (DisabledException e) {
          throw new Exception(&quot;USER_DISABLED&quot;, e);
      } catch (BadCredentialsException e) {
          throw new Exception(&quot;INVALID_CREDENTIALS&quot;, e);
      }
  }
  </code></pre>
  <p>}</p>
  <p>so I ask them if they know of any way to be able to authenticate the user by accessing the database and verify that the password and the user exist and then generate the token.</p>