-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: signup,login modify user controller, service * feat: signup, login security config * H2 database (#25) * Test: Change database * feat: login, JWT config --------- Co-authored-by: JiSeong Lee <cocoquiet@knu.ac.kr> * Update ci.yml .env 파일 추가하도록 설정 --------- Co-authored-by: JiSeong Lee <cocoquiet@knu.ac.kr> Co-authored-by: Yeonwoo Sea <62321953+Village-GG-Water@users.noreply.github.com>
- Loading branch information
1 parent
73db79a
commit 9ee620d
Showing
14 changed files
with
356 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
53 changes: 53 additions & 0 deletions
53
src/main/java/com/kert/config/JwtAuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.kert.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
private final SecurityUserService securityUserService; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
String token = getJWTFromRequest(request); | ||
|
||
if (token != null && jwtTokenProvider.validateToken(token)) { | ||
Long userId = jwtTokenProvider.getUserIdFromJWT(token); | ||
|
||
try { | ||
SecurityUser userDetails = securityUserService.loadUserById(userId); | ||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); | ||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} catch (UsernameNotFoundException e) { | ||
throw new RuntimeException("User not found"); | ||
} | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private String getJWTFromRequest(HttpServletRequest request) { | ||
String bearerToken = request.getHeader("Authorization"); | ||
if (bearerToken != null && bearerToken.startsWith("Bearer ")) { | ||
return bearerToken.substring(7); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.kert.config; | ||
|
||
import io.github.cdimascio.dotenv.Dotenv; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.security.Keys; | ||
import io.jsonwebtoken.io.Decoders; | ||
import io.jsonwebtoken.security.SignatureException; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.crypto.SecretKey; | ||
import java.util.Date; | ||
|
||
@Configuration | ||
public class JwtTokenProvider { | ||
|
||
private final Dotenv dotenv = Dotenv.load(); | ||
|
||
private final String SECRET_KEY = dotenv.get("JWT_SECRET"); | ||
private final long EXPIRATION_TIME = 86400000; | ||
private final SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(SECRET_KEY)); | ||
|
||
// JWT 토큰 생성 | ||
public String generateToken(Long studentId) { | ||
Date now = new Date(); | ||
Date expiryDate = new Date(now.getTime() + EXPIRATION_TIME); | ||
|
||
return Jwts.builder() | ||
.setSubject(Long.toString(studentId)) | ||
.setIssuedAt(now) | ||
.setExpiration(expiryDate) | ||
.signWith(key, SignatureAlgorithm.HS512) | ||
.compact(); | ||
} | ||
|
||
// JWT로부터 사용자 ID 추출 | ||
public Long getUserIdFromJWT(String token) { | ||
Claims claims = Jwts.parserBuilder() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
|
||
return Long.parseLong(claims.getSubject()); | ||
} | ||
|
||
// JWT 토큰의 유효성 검증 | ||
public boolean validateToken(String token) { | ||
try { | ||
Jwts.parserBuilder() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(token); | ||
return true; | ||
} catch (SignatureException e) { | ||
return false; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.