File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
backend/src/main/java/com/backend/domain/user/util Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 66import io .jsonwebtoken .SignatureAlgorithm ;
77import io .jsonwebtoken .security .Keys ;
88import jakarta .annotation .PostConstruct ;
9+ import jakarta .servlet .http .Cookie ;
10+ import jakarta .servlet .http .HttpServletRequest ;
911import org .springframework .beans .factory .annotation .Value ;
1012import org .springframework .stereotype .Component ;
1113
@@ -87,4 +89,37 @@ public long getExpiration(String token) {
8789 }
8890 return remain ;
8991 }
92+
93+ //JWT에서 email추출
94+ //HttpServletRequest가 매개변수로 들어갑니다.
95+ public String getEmail (HttpServletRequest request ){
96+ String jwt = getJwtToken (request );
97+ if (jwt != null ){
98+ Claims claims = getClaimsWithoutVerification (jwt );
99+ return claims .getSubject ();
100+ }
101+ return null ;
102+
103+ }
104+
105+ public String getName (HttpServletRequest request ){
106+ String jwt = getJwtToken (request );
107+ if (jwt != null ){
108+ Claims claims = getClaimsWithoutVerification (jwt );
109+ return claims .get ("name" , String .class );
110+ }
111+ return null ;
112+ }
113+
114+ public String getJwtToken (HttpServletRequest request ){
115+ Cookie [] cookies = request .getCookies (); //
116+ if (cookies != null ) {
117+ for (Cookie c : cookies ) {
118+ if (c .getName ().equals ("token" )) {
119+ return c .getValue ();
120+ }
121+ }
122+ }
123+ return null ;
124+ }
90125}
You can’t perform that action at this time.
0 commit comments