-
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.
- Loading branch information
Showing
5 changed files
with
146 additions
and
7 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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/nadoyagsa/pillaroid/configuration/SecurityConfiguration.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,26 @@ | ||
package com.nadoyagsa.pillaroid.configuration; | ||
|
||
import com.nadoyagsa.pillaroid.jwt.AuthInterceptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class SecurityConfiguration implements WebMvcConfigurer { | ||
private final AuthInterceptor authInterceptor; | ||
|
||
@Autowired | ||
public SecurityConfiguration(AuthInterceptor authInterceptor) { | ||
this.authInterceptor = authInterceptor; | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
//토큰 검사 안하는 경로 설정함(/login/**, 알약 검색 등) | ||
//TODO: 알약 검색 시 patterns 사용해서 경로 추가해야 함 | ||
registry.addInterceptor(authInterceptor) | ||
.addPathPatterns("/**") | ||
.excludePathPatterns(new String[]{"/login/**"}); | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/nadoyagsa/pillaroid/jwt/AuthInterceptor.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,30 @@ | ||
package com.nadoyagsa.pillaroid.jwt; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@Component | ||
public class AuthInterceptor implements HandlerInterceptor { | ||
private final AuthTokenProvider authTokenProvider; | ||
|
||
@Autowired | ||
public AuthInterceptor(AuthTokenProvider authTokenProvider) { | ||
this.authTokenProvider = authTokenProvider; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
String token = request.getHeader("authorization"); | ||
if (token != null && authTokenProvider.validateToken(token)) { | ||
return true; | ||
} else { | ||
response.setStatus(HttpStatus.UNAUTHORIZED.value()); | ||
return false; | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/com/nadoyagsa/pillaroid/jwt/AuthTokenProvider.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,70 @@ | ||
package com.nadoyagsa.pillaroid.jwt; | ||
|
||
import io.jsonwebtoken.*; | ||
import io.jsonwebtoken.io.Decoders; | ||
import io.jsonwebtoken.security.Keys; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.security.Key; | ||
import java.util.Date; | ||
|
||
@Component | ||
public class AuthTokenProvider implements InitializingBean { | ||
private final Logger logger = LoggerFactory.getLogger(AuthTokenProvider.class); | ||
|
||
private final String secretKey; | ||
private Key key; | ||
|
||
public AuthTokenProvider(@Value("${jwt.secret-key}") String secretKey) { | ||
this.secretKey = secretKey; | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
byte[] keyBytes = Decoders.BASE64.decode(secretKey); | ||
this.key = Keys.hmacShaKeyFor(keyBytes); | ||
} | ||
|
||
// 토큰의 payload에 카카오 회원번호를 삽입 | ||
public String createAuthToken(Long kakaoAccountId) { | ||
return Jwts.builder() | ||
.setHeaderParam(Header.TYPE, Header.JWT_TYPE) | ||
.setIssuer("pillaroid") | ||
.setIssuedAt(new Date()) | ||
.claim("accountId", kakaoAccountId) | ||
.signWith(key, SignatureAlgorithm.HS256) | ||
.compact(); | ||
} | ||
|
||
// 토큰으로부터 payload를 추출하는 메서드 | ||
public Claims getClaims(String token) { | ||
return Jwts.parserBuilder() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
} | ||
|
||
public boolean validateToken(String token) { | ||
try { | ||
Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token); | ||
|
||
return true; | ||
} catch (io.jsonwebtoken.security.SecurityException | MalformedJwtException e) { | ||
logger.info("잘못된 JWT 서명입니다."); | ||
} catch (ExpiredJwtException e) { | ||
logger.info("만료된 JWT 토큰입니다."); | ||
} catch (UnsupportedJwtException e) { | ||
logger.info("지원되지 않는 JWT 토큰입니다."); | ||
} catch (IllegalArgumentException e) { | ||
logger.info("JWT 토큰이 잘못되었습니다."); | ||
} catch (Exception e) { | ||
logger.info("서비스에 접근할 수 없는 토큰입니다."); | ||
} | ||
return false; | ||
} | ||
} |