Authenticate Endpoint Part 2

In our previous lesson, we set up the authentication controller and service interface for our blog platform.

Now we'll implement the authentication service to handle user verification and JWT token generation.

This implementation will enable secure user authentication and stateless session management across our application.

Implementing the Authentication Service

The AuthenticationServiceImpl class forms the core of our authentication logic, integrating with Spring Security's authentication manager:

package com.devtiro.blog.services.impl; import com.devtiro.blog.services.AuthenticationService; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.security.Keys; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.stereotype.Service; import java.security.Key; import java.util.Date; import java.util.HashMap; import java.util.Map; @Service @RequiredArgsConstructor public class AuthenticationServiceImpl implements AuthenticationService { private final AuthenticationManager authenticationManager; private final UserDetailsService userDetailsService; @Value("${jwt.secret}") private String secretKey; private final Long jwtExpiryMs = 86400000L; @Override public UserDetails authenticate(String email, String password) { authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(email, password) ); return userDetailsService.loadUserByUsername(email); } @Override public String generateToken(UserDetails userDetails) { Map<String, Object> claims = new HashMap<>(); return Jwts.builder() .setClaims(claims) .setSubject(userDetails.getUsername()) .setIssuedAt(new Date(System.currentTimeMillis())) .setExpiration(new Date(System.currentTimeMillis() + jwtExpiryMs)) .signWith(getSigningKey(), SignatureAlgorithm.HS256) .compact(); } @Override public UserDetails validateToken(String token) { String username = extractUsername(token); return userDetailsService.loadUserByUsername(username); } private String extractUsername(String token) { Claims claims = Jwts.parserBuilder() .setSigningKey(getSigningKey()) .build() .parseClaimsJws(token) .getBody(); return claims.getSubject(); } private Key getSigningKey() { byte[] keyBytes = secretKey.getBytes(); return Keys.hmacShaKeyFor(keyBytes); } }

Update Properties

Let's not forget to add a secret to our properties file. It needs to be at least 32 bytes long!

jwt.secret=your-256-bit-secret-key-here-make-it-at-least-32-bytes-long

Summary

  • Implemented AuthenticationServiceImpl with dependency injection for required services
  • Created authentication method using Spring Security's AuthenticationManager
  • Added token generation logic using JJWT library
  • Configured JWT expiration and secret key through properties
© 2026 Devtiro Ltd. All rights reserved