I want to generate JWT if the username password provided are accurate. I have a userdetailservice and I am using AbstractUserDetailsAuthenticationProvider. Here is my code:
public class JwtAuthenticationTokenFilter extends AbstractAuthenticationProcessingFilter { public JwtAuthenticationTokenFilter() { super("/rest/**"); } @Override public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException { String header = httpServletRequest.getHeader("Authorisation"); if (header == null || !header.startsWith("Token")) { throw new RuntimeException("JWT Token is missing"); } String authenticationToken = header.substring(6); JwtAuthenticationToken token = new JwtAuthenticationToken(authenticationToken); return getAuthenticationManager().authenticate(token); } When I try to login it returns a JWT even if the password is wrong.
@RestController @RequestMapping("/token") public class TokenController { private JwtGenerator jwtGenerator; public TokenController(JwtGenerator jwtGenerator) { this.jwtGenerator = jwtGenerator; } @RequestMapping(value = "/login", method = RequestMethod.POST) public String createAuthenticationToken( @RequestBody User user, HttpServletResponse response) throws AuthenticationException, IOException { return jwtGenerator.generate(user); } My understanding is when a user tries to login, validation of the user's identity is made by the user detail service which is loaded by username, so I dont know why its not working.
This is part of my security config:
@Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests().antMatchers("**/rest/**").authenticated() .and() .exceptionHandling().authenticationEntryPoint(entryPoint) .and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); http.addFilterBefore(authenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class); http.headers().cacheControl(); }