title: JAVA Springboot + spring security + JWT
body: <p>I have been trying to create a basic spring-security + JWT application which asks for registration ('register') and then uses the same data from saved via register to sign-in '/sigin'. I created the following classes to register the user and then to test sign in. Unfortunately I am not able to register the user.</p>
  <p>Any kind of guidance to nudge me towards the correct implementation would help me out. I am stuck at this currently</p>
  <pre><code>Entity Class
  
  
  @Entity
  public class ApplicationUser {
  
     public String user_name;
          @Id
       public String user_email;
       public String password;
       public String user_mobile;
       public String location;
   public String getUser_name() {
          return user_name;
      }
      public void setUser_name(String user_name) {
          this.user_name = user_name;
      }
      public String getUser_email() {
          return user_email;
      }
      public void setUser_email(String user_email) {
          this.user_email = user_email;
      }
      public String getPassword() {
          return password;
      }
      public void setPassword(String password) {
          this.password = password;
      }
      public String getUser_mobile() {
          return user_mobile;
      }
      public void setUser_mobile(String user_mobile) {
          this.user_mobile = user_mobile;
      }
      public String getLocation() {
          return location;
      }
      public void setLocation(String location) {
          this.location = location;
      }
      public ApplicationUser(String user_name, String user_email, String password, String user_mobile, String location) {
          super();
          this.user_name = user_name;
          this.user_email = user_email;
          this.password = password;
          this.user_mobile = user_mobile;
          this.location = location;
      }
      public ApplicationUser() {
          super();
      }
      public ApplicationUser(String user_name, String password) {
          super();
          this.user_name = user_name;
          this.password = password;
      }
      
      //    public Date user_dob;
      
  }
  ---------------------------------------------------------------
  
  Repsoitory 
  
  public interface ApplicationUserRepository  extends JpaRepository&lt;ApplicationUser, String&gt;{
   
  
  }
  --------------------------------------------------------------
  
  Service Class
  
  @Service
  public class ApplicationUserService {
  
    
    @Autowired
    private ApplicationUserRepository repository;
  
    public void registerUser(ApplicationUser user){
               
         repository.save(user);
    }
      
  }
  
  
  @Service
  public class UserAuthService implements UserDetailsService  {
  
    @Autowired
    private ApplicationUserRepository repo; 
  
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
      ApplicationUser appuser =  repo.findById(username).get();
  
          return new org.springframework.security.core.userdetails.User(appuser.getUser_email(), appuser.getPassword(), new ArrayList&lt;&gt;());
      }
  
       
  
  }
  -------------------------------------------------------------
  
  JWTUTIL 
  
  Component
  public class JwtUtil {
  
     private String secret = &quot;SecretKey&quot;;
  
      public String extractUsername(String token) {
          return extractClaim(token, Claims::getSubject);
      }
  
      public Date extractExpiration(String token) {
          return extractClaim(token, Claims::getExpiration);
      }
  
      public &lt;T&gt; T extractClaim(String token, Function&lt;Claims, T&gt; claimsResolver) {
          final Claims claims = extractAllClaims(token);
          return claimsResolver.apply(claims);
      }
      private Claims extractAllClaims(String token) {
          return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
      }
  
      private Boolean isTokenExpired(String token) {
          return extractExpiration(token).before(new Date());
      }
  
      public String generateToken(String username) {
          Map&lt;String, Object&gt; claims = new HashMap&lt;&gt;();
          return createToken(claims, username);
      }
  
      private String createToken(Map&lt;String, Object&gt; claims, String subject) {
  
          return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
                  .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10))
                  .signWith(SignatureAlgorithm.HS256, secret).compact();
      }
  
      public Boolean validateToken(String token, UserDetails userDetails) {
          final String username = extractUsername(token);
          return (username.equals(userDetails.getUsername()) &amp;&amp; !isTokenExpired(token));
      }
      
  }
  -------------------------------------------------
  
  Configure
  
  @Configuration
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class ApiSecurityConfig extends WebSecurityConfigurerAdapter {
  
  
  
    @Autowired
    private JwtAuthenticationFilter jwtFilter;
  
    @Autowired  
    private UserAuthService userDetailsService;
  
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        
         auth.userDetailsService(userDetailsService);
    }
  
    @Bean
    public PasswordEncoder PasswordEncoder(){
  
      return NoOpPasswordEncoder.getInstance();
  
      }
  
      @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.csrf().disable().authorizeRequests().antMatchers(&quot;/signin&quot;, &quot;/register&quot;)
                  .permitAll().anyRequest().authenticated();
           
      }
  
  
  
  }
  ----------------------------------------------------------------------------------------------------
  
  Controller
  
  
  @RestController
  public class ApplicationUserController {
  
    @Autowired
    private ApplicationUserService userService;
  
      @Autowired
      private JwtUtil jwtUtil;
      
      @Autowired
      private AuthenticationManager authenticationManager;
  
    @RequestMapping(value = &quot;/register&quot;, method = RequestMethod.POST)
     
      public String applicationUser(@RequestBody ApplicationUser user){
                  
               userService.registerUser(user);
               System.out.println(&quot;&quot;+user);
  
         return &quot;Registraion successful&quot;;      
      }
   
      
  
    @PostMapping(&quot;/signin&quot;)
    public String generateToken(@RequestBody ApplicationUser authrequest) throws Exception{
  
      try{
            authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(authrequest.getUser_email(), authrequest.getPassword())
            );
      }catch (Exception ex) {
              throw new Exception(&quot;inavalid username/password&quot;);
      }
          String userEmail = authrequest.getUser_email();
  
          return jwtUtil.generateToken(userEmail);
  
    }
  
      
  }
  </code></pre>