|
| 1 | +package com.techtorque.admin_service.config; |
| 2 | + |
| 3 | +import io.jsonwebtoken.Claims; |
| 4 | +import io.jsonwebtoken.Jwts; |
| 5 | +import io.jsonwebtoken.security.Keys; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 8 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 9 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 10 | +import org.springframework.stereotype.Component; |
| 11 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 12 | + |
| 13 | +import jakarta.servlet.FilterChain; |
| 14 | +import jakarta.servlet.ServletException; |
| 15 | +import jakarta.servlet.http.HttpServletRequest; |
| 16 | +import jakarta.servlet.http.HttpServletResponse; |
| 17 | +import javax.crypto.SecretKey; |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +@Component |
| 26 | +public class JwtAuthenticationFilter extends OncePerRequestFilter { |
| 27 | + |
| 28 | + @Value("${jwt.secret:mysecretkey}") |
| 29 | + private String jwtSecret; |
| 30 | + |
| 31 | + @Override |
| 32 | + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) |
| 33 | + throws ServletException, IOException { |
| 34 | + |
| 35 | + String authHeader = request.getHeader("Authorization"); |
| 36 | + |
| 37 | + if (authHeader != null && authHeader.startsWith("Bearer ")) { |
| 38 | + String token = authHeader.substring(7); |
| 39 | + |
| 40 | + try { |
| 41 | + SecretKey key = Keys.hmacShaKeyFor(jwtSecret.getBytes(StandardCharsets.UTF_8)); |
| 42 | + |
| 43 | + Claims claims = Jwts.parser() |
| 44 | + .verifyWith(key) |
| 45 | + .build() |
| 46 | + .parseSignedClaims(token) |
| 47 | + .getPayload(); |
| 48 | + |
| 49 | + String username = claims.getSubject(); |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + List<String> roles = (List<String>) claims.get("roles"); |
| 52 | + |
| 53 | + if (username != null && roles != null) { |
| 54 | + List<SimpleGrantedAuthority> authorities = roles.stream() |
| 55 | + .map(role -> { |
| 56 | + String roleUpper = role.trim().toUpperCase(); |
| 57 | + // Treat SUPER_ADMIN as ADMIN for authorization purposes |
| 58 | + if ("SUPER_ADMIN".equals(roleUpper)) { |
| 59 | + // Add both SUPER_ADMIN and ADMIN roles |
| 60 | + return Arrays.asList( |
| 61 | + new SimpleGrantedAuthority("ROLE_SUPER_ADMIN"), |
| 62 | + new SimpleGrantedAuthority("ROLE_ADMIN") |
| 63 | + ); |
| 64 | + } |
| 65 | + return Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + roleUpper)); |
| 66 | + }) |
| 67 | + .flatMap(List::stream) |
| 68 | + .collect(Collectors.toList()); |
| 69 | + |
| 70 | + UsernamePasswordAuthenticationToken authentication = |
| 71 | + new UsernamePasswordAuthenticationToken(username, null, authorities); |
| 72 | + |
| 73 | + SecurityContextHolder.getContext().setAuthentication(authentication); |
| 74 | + } |
| 75 | + } catch (Exception e) { |
| 76 | + logger.warn("JWT token validation failed: " + e.getMessage()); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + filterChain.doFilter(request, response); |
| 81 | + } |
| 82 | +} |
0 commit comments