-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create authorization annotation
- Loading branch information
Showing
7 changed files
with
216 additions
and
60 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
src/main/java/com/kumofactory/cloud/global/middleware/AuthorizationFromToken.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,4 @@ | ||
package com.kumofactory.cloud.global.middleware; | ||
|
||
public class AuthorizationFromToken { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromToken.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,11 @@ | ||
package com.kumofactory.cloud.global.middleware.auth; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD}) | ||
public @interface AuthorizationFromToken { | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.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,63 @@ | ||
package com.kumofactory.cloud.global.middleware.auth; | ||
|
||
import com.kumofactory.cloud.jwt.provider.JwtTokenProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.AfterReturning; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.http.Cookie; | ||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Arrays; | ||
|
||
@Aspect | ||
@Component | ||
@RequiredArgsConstructor | ||
public class AuthorizationFromTokenAspect { | ||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
@Around(value = "@annotation(AuthorizationFromToken)") | ||
public Object applyAuthorizationFromToken(ProceedingJoinPoint joinPoint, AuthorizationFromToken AuthorizationFromToken) throws Throwable { | ||
HttpServletRequest request = findHttpServletRequest(joinPoint.getArgs()); | ||
HttpServletRequest header = findHttpServletRequest(joinPoint.getArgs()); | ||
String accessToken = extractAccessTokenFromCookies(request.getCookies()); | ||
String userId = getUserFromAccessToken(accessToken); | ||
|
||
// 추출된 사용자 정보를 매개변수로 전달 | ||
Object[] args = joinPoint.getArgs(); | ||
args[0] = userId; | ||
|
||
return joinPoint.proceed(args); | ||
} | ||
|
||
private HttpServletRequest findHttpServletRequest(Object[] args) { | ||
for (Object arg : args) { | ||
if (arg instanceof HttpServletRequest) { | ||
return (HttpServletRequest) arg; | ||
} | ||
} | ||
throw new IllegalArgumentException("HttpServletRequest not found in method parameters"); | ||
} | ||
|
||
private String extractAccessTokenFromCookies(Cookie[] cookies) { | ||
if (cookies != null) { | ||
return Arrays.stream(cookies) | ||
.filter(cookie -> "access_token".equals(cookie.getName())) | ||
.map(Cookie::getValue) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
return null; | ||
} | ||
|
||
private String getUserFromAccessToken(String accessToken) { | ||
boolean isValidate = jwtTokenProvider.validateAccessToken(accessToken); | ||
if (isValidate) { | ||
return jwtTokenProvider.getClaimsFormToken(accessToken).getSubject(); | ||
} | ||
return null; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2Service.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,8 @@ | ||
package com.kumofactory.cloud.infra.service.aws_cdk; | ||
|
||
public interface Ec2Service { | ||
|
||
void createBastionHostLinux(); | ||
|
||
void createEc2Instance(); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/Ec2ServiceImpl.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,43 @@ | ||
package com.kumofactory.cloud.infra.service.aws_cdk; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
import software.amazon.awscdk.services.ec2.BastionHostLinux; | ||
import software.amazon.awscdk.services.ec2.BastionHostLinuxProps; | ||
import software.amazon.awscdk.services.ec2.SubnetFilter; | ||
import software.amazon.awscdk.services.ec2.SubnetSelection; | ||
import software.amazon.awscdk.services.ec2.SubnetSelection.Builder; | ||
import software.amazon.awscdk.services.ec2.Instance; | ||
|
||
/** | ||
* AWS CDK를 이용한 EC2 서비스 | ||
*/ | ||
@Service | ||
@Slf4j | ||
public class Ec2ServiceImpl implements Ec2Service { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(Ec2ServiceImpl.class); | ||
|
||
@Override | ||
public void createBastionHostLinux() { | ||
logger.info("================ Create BastionHostLinux Instance ================"); | ||
List<String> subnetsId = new ArrayList<>(); | ||
List<SubnetFilter> subnetFilters = new ArrayList<>(); | ||
subnetsId.add("subnet-043aec505f84e38ac"); | ||
SubnetFilter subnetFilter = SubnetFilter.byIds(subnetsId); | ||
subnetFilters.add(subnetFilter); | ||
BastionHostLinuxProps.builder().subnetSelection(SubnetSelection.builder().subnetFilters(subnetFilters).build()); | ||
} | ||
|
||
@Override | ||
public void createEc2Instance() { | ||
logger.info("================ Create EC2 Instance ================"); | ||
|
||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/kumofactory/cloud/infra/service/aws_cdk/stack/Ec2Stack.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,16 @@ | ||
package com.kumofactory.cloud.infra.service.aws_cdk.stack; | ||
|
||
import software.amazon.awscdk.Stack; | ||
import software.amazon.awscdk.services.ec2.ISubnet; | ||
import software.amazon.awscdk.services.ec2.Subnet; | ||
import software.amazon.awscdk.services.ec2.Vpc; | ||
import software.amazon.awscdk.services.ec2.VpcLookupOptions; | ||
import software.constructs.Construct; | ||
|
||
public class Ec2Stack extends Stack { | ||
public Ec2Stack(final Construct scope, String id) { | ||
super(scope, id); | ||
Vpc vpc = (Vpc) Vpc.fromLookup(this, "KumofactoryVPC", VpcLookupOptions.builder().vpcId("vpc-0719a1184fa5ccdcd").build()); | ||
ISubnet kumofactorySubnet = Subnet.fromSubnetId(this, "KumofactorySubnet", "subnet-043aec505f84e38ac"); | ||
} | ||
} |
131 changes: 71 additions & 60 deletions
131
src/main/java/com/kumofactory/cloud/jwt/provider/JwtTokenProvider.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 |
---|---|---|
@@ -1,76 +1,87 @@ | ||
package com.kumofactory.cloud.jwt.provider; | ||
|
||
import com.kumofactory.cloud.jwt.dto.TokenDto; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jws; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.*; | ||
import io.jsonwebtoken.security.Keys; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Date; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class JwtTokenProvider { | ||
|
||
@Value("${jwt.secret}") | ||
private String secret; | ||
@Value("${jwt.token-validity-in-milliseconds}") | ||
private long VALIDITY; | ||
|
||
public TokenDto create(String id) { | ||
|
||
Date now = new Date(); | ||
|
||
String accessToken = Jwts.builder() | ||
.setSubject(id) | ||
.setIssuedAt(now) | ||
.setExpiration(new Date(now.getTime() + VALIDITY)) | ||
.signWith(Keys.hmacShaKeyFor(secret.getBytes())) | ||
.compact(); | ||
|
||
String refreshToken = Jwts.builder() | ||
.setSubject(id) | ||
.setIssuedAt(now) | ||
.setExpiration(new Date(now.getTime() + (VALIDITY * 365))) | ||
.signWith(Keys.hmacShaKeyFor(secret.getBytes())) | ||
.compact(); | ||
|
||
return TokenDto.builder() | ||
.userEmail(id) | ||
.accessToken(accessToken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
} | ||
|
||
public String getUserId(String token) { | ||
return Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token).getBody().getSubject(); | ||
} | ||
|
||
public boolean validateAccessToken(String token) { | ||
try { | ||
Jws<Claims> claims = Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token); | ||
return !claims.getBody().getExpiration().before(new Date()); | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean validateRefreshToken(String token) { | ||
try { | ||
Jws<Claims> claims = Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token); | ||
return !claims.getBody().getExpiration().before(new Date()); | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
|
||
public String refreshAccessToken() { | ||
|
||
return null; | ||
} | ||
private final Logger logger = LoggerFactory.getLogger(JwtTokenProvider.class); | ||
|
||
@Value("${jwt.secret}") | ||
private String secret; | ||
@Value("${jwt.token-validity-in-milliseconds}") | ||
private long VALIDITY; | ||
|
||
public TokenDto create(String id) { | ||
|
||
Date now = new Date(); | ||
|
||
String accessToken = Jwts.builder() | ||
.setSubject(id) | ||
.setIssuedAt(now) | ||
.setExpiration(new Date(now.getTime() + VALIDITY)) | ||
.signWith(Keys.hmacShaKeyFor(secret.getBytes())) | ||
.compact(); | ||
|
||
String refreshToken = Jwts.builder() | ||
.setSubject(id) | ||
.setIssuedAt(now) | ||
.setExpiration(new Date(now.getTime() + (VALIDITY * 365))) | ||
.signWith(Keys.hmacShaKeyFor(secret.getBytes())) | ||
.compact(); | ||
|
||
return TokenDto.builder() | ||
.userEmail(id) | ||
.accessToken(accessToken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
} | ||
|
||
public boolean validateAccessToken(String token) { | ||
try { | ||
Claims claims = getClaimsFormToken(token); | ||
return true; | ||
} catch (ExpiredJwtException exception) { | ||
logger.error("Token Expired"); | ||
throw new ExpiredJwtException(exception.getHeader(), exception.getClaims(), exception.getMessage()); | ||
} catch (JwtException exception) { | ||
logger.error("Token Tampered"); | ||
return new JwtException(exception.getMessage()).getMessage().equals(exception.getMessage()); | ||
} catch (NullPointerException exception) { | ||
logger.error("Token is null"); | ||
return new NullPointerException().getMessage().equals(exception.getMessage()); | ||
} | ||
} | ||
|
||
public boolean validateRefreshToken(String token) { | ||
try { | ||
Jws<Claims> claims = Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token); | ||
return !claims.getBody().getExpiration().before(new Date()); | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
|
||
public String refreshAccessToken(Claims claims) { | ||
String id = claims.getSubject(); | ||
TokenDto tokenDto = create(id); | ||
return null; | ||
} | ||
|
||
public Claims getClaimsFormToken(String token) { | ||
return Jwts.parserBuilder().setSigningKey(Keys.hmacShaKeyFor(secret.getBytes())).build().parseClaimsJws(token).getBody(); | ||
} | ||
|
||
|
||
} |