source: src/main/java/com/example/rezevirajmasa/demo/service/impl/TokenService.java

main
Last change on this file was 8ca35dc, checked in by Aleksandar Panovski <apano77@…>, 4 months ago

Done with stupid timeslots

  • Property mode set to 100644
File size: 1.2 KB
Line 
1package com.example.rezevirajmasa.demo.service.impl;
2
3import com.example.rezevirajmasa.demo.config.security.SecurityConstants;
4import io.jsonwebtoken.Claims;
5import io.jsonwebtoken.ExpiredJwtException;
6import io.jsonwebtoken.Jwts;
7import io.jsonwebtoken.SignatureException;
8import org.springframework.beans.factory.annotation.Value;
9import org.springframework.stereotype.Service;
10
11@Service
12public class TokenService {
13
14 public String getUserIdFromToken(String token) {
15 try {
16 String jwtToken = token.substring(7);
17
18 Claims claims = Jwts.parser()
19 .setSigningKey(SecurityConstants.SECRET)
20 .parseClaimsJws(jwtToken)
21 .getBody();
22
23 return claims.getSubject(); // Assumes 'sub' claim contains user ID
24 } catch (ExpiredJwtException e) {
25 throw new RuntimeException("Token has expired", e);
26 } catch (SignatureException e) {
27 throw new RuntimeException("Invalid token signature", e);
28 } catch (IllegalArgumentException e) {
29 throw new RuntimeException("Invalid token format", e);
30 } catch (Exception e) {
31 throw new RuntimeException("Could not extract user ID from token", e);
32 }
33 }
34}
Note: See TracBrowser for help on using the repository browser.