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 | |
---|
1 | package com.example.rezevirajmasa.demo.service.impl;
|
---|
2 |
|
---|
3 | import com.example.rezevirajmasa.demo.config.security.SecurityConstants;
|
---|
4 | import io.jsonwebtoken.Claims;
|
---|
5 | import io.jsonwebtoken.ExpiredJwtException;
|
---|
6 | import io.jsonwebtoken.Jwts;
|
---|
7 | import io.jsonwebtoken.SignatureException;
|
---|
8 | import org.springframework.beans.factory.annotation.Value;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | @Service
|
---|
12 | public 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.