Skip to content

Commit 03cdfd2

Browse files
authored
feat: added jwt config common files (#5)
2 parents 07de096 + 80b085c commit 03cdfd2

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@
6969
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
7070
<version>2.1.0</version>
7171
</dependency>
72+
<dependency>
73+
<groupId>io.jsonwebtoken</groupId>
74+
<artifactId>jjwt-api</artifactId>
75+
<version>0.12.3</version>
76+
</dependency>
77+
<dependency>
78+
<groupId>io.jsonwebtoken</groupId>
79+
<artifactId>jjwt-impl</artifactId>
80+
<version>0.12.3</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>io.jsonwebtoken</groupId>
84+
<artifactId>jjwt-jackson</artifactId>
85+
<version>0.12.3</version>
86+
</dependency>
7287
</dependencies>
7388
<profiles>
7489
<profile>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package br.com.grupo63.techchallenge.common.config;
2+
3+
import io.jsonwebtoken.Claims;
4+
import jakarta.servlet.*;
5+
import jakarta.servlet.http.HttpServletRequest;
6+
import jakarta.servlet.http.HttpServletResponse;
7+
import lombok.RequiredArgsConstructor;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.springframework.util.StringUtils;
11+
12+
import java.io.IOException;
13+
import java.security.GeneralSecurityException;
14+
15+
@RequiredArgsConstructor
16+
public class JwtFilter implements Filter {
17+
18+
private final JwtService jwtService;
19+
20+
@Override
21+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
22+
try {
23+
String authHeader = ((HttpServletRequest) request).getHeader("Authorization");
24+
if (!StringUtils.hasLength(authHeader) || !StringUtils.startsWithIgnoreCase(authHeader, "Bearer ")) {
25+
throw new GeneralSecurityException("Missing or invalid authorization header");
26+
}
27+
String jwt = authHeader.substring(7);
28+
Claims claims = jwtService.getClaims(jwt);
29+
request.setAttribute("clientId", claims.get("sub"));
30+
filterChain.doFilter(request, response);
31+
} catch (Exception e) {
32+
((HttpServletResponse) response).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
33+
response.getWriter().write("Unauthorized: Missing or incorrect JWT Token.");
34+
}
35+
}
36+
}
37+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package br.com.grupo63.techchallenge.common.config;
2+
3+
import io.jsonwebtoken.Claims;
4+
import io.jsonwebtoken.Jwts;
5+
import io.jsonwebtoken.io.Decoders;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.security.Key;
10+
import java.security.KeyFactory;
11+
import java.security.NoSuchAlgorithmException;
12+
import java.security.spec.InvalidKeySpecException;
13+
import java.security.spec.X509EncodedKeySpec;
14+
15+
@Service
16+
public class JwtService {
17+
18+
@Value("${jwt.token.key.public}")
19+
private String jwtSigningKey;
20+
21+
public Claims getClaims(String token) throws NoSuchAlgorithmException, InvalidKeySpecException {
22+
return Jwts.parser().setSigningKey(getSigningKey()).build().parseClaimsJws(token)
23+
.getBody();
24+
}
25+
26+
private Key getSigningKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
27+
byte[] keyBytes = Decoders.BASE64.decode(jwtSigningKey);
28+
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
29+
KeyFactory kf = KeyFactory.getInstance("RSA");
30+
return kf.generatePublic(spec);
31+
}
32+
}

src/main/resources/application.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
jwt:
2+
token:
3+
key:
4+
public: "${JWT_PUBLIC_KEY}"

0 commit comments

Comments
 (0)