Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/member #1

Merged
merged 10 commits into from
Aug 26, 2024
53 changes: 40 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,52 @@ plugins {
id 'io.spring.dependency-management' version '1.1.6'
}

group = 'co.kr.ticketing'
version = '0.0.1-SNAPSHOT'
allprojects {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
group = 'co.kr.ticketing'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = '17'

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
}

repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

repositories {
mavenCentral()
}
}

tasks.named('test') {
useJUnitPlatform()
}


jar {
enabled = false
}

bootJar {
enabled = false
}

build {
enabled = false
}
28 changes: 28 additions & 0 deletions member/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
dependencies {
// database
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.mysql:mysql-connector-j'

//encryption
implementation 'org.springframework.security:spring-security-crypto:5.5.0'

// validator
implementation 'org.springframework.boot:spring-boot-starter-validation'

//jwt
implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'com.sun.xml.bind:jaxb-impl:4.0.1'
implementation 'com.sun.xml.bind:jaxb-core:4.0.1'
implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
}

bootJar {
archivesBaseName = 'Admin'
archiveFileName = 'admin.jar'
destinationDirectory = file(project.rootProject.projectDir)
enabled = true
}

jar {
enabled = false
}
15 changes: 15 additions & 0 deletions member/src/main/java/co/kr/ticketing/member/MemberApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.kr.ticketing.member;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@SpringBootApplication
public class MemberApplication {

public static void main(String[] args) {
SpringApplication.run(MemberApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package co.kr.ticketing.member.auth.aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import co.kr.ticketing.member.auth.config.AuthConfig;
import co.kr.ticketing.member.auth.util.TokenUtil;
import co.kr.ticketing.member.exception.UnAuthorizedException;
import jakarta.servlet.http.Cookie;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class AuthAspect {
TokenUtil tokenUtil;

@Before("@annotation(LoginCheck)")
public void loginCheck(JoinPoint jp) {
String token = getToken();
if (!tokenUtil.isValidToken(token)) {
throw new UnAuthorizedException();
}
}

private String getToken() {
Cookie[] cookies = ((ServletRequestAttributes)(RequestContextHolder.currentRequestAttributes())).getRequest()
.getCookies();

return cookies == null ? "" : Arrays.stream(cookies)
.filter(cookie -> cookie.getName().equals(AuthConfig.LOGIN_COOKIE_NAME))
.findAny()
.orElseThrow(UnAuthorizedException::new)
.getValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.kr.ticketing.member.auth.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginCheck {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.kr.ticketing.member.auth.config;

import lombok.experimental.UtilityClass;

@UtilityClass
public class AuthConfig {
public static String SECRET_KEY = "test_secret_key";
public static String PHONE_NUMBER = "phoneNumber";
public static int TOKEN_VALID_TIME = 86400;
public static String LOGIN_COOKIE_NAME = "token";
}
76 changes: 76 additions & 0 deletions member/src/main/java/co/kr/ticketing/member/auth/util/JwtUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package co.kr.ticketing.member.auth.util;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import co.kr.ticketing.member.auth.config.AuthConfig;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;

@Component
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class JwtUtil {

public String generateJwt(Map<String, Object> valueMap, int expireSeconds) {
return generateToken(valueMap, expireSeconds, AuthConfig.SECRET_KEY);
}

public boolean isValidJwt(String jwt) {
if (jwt.isEmpty()) {
return false;
}

try {
Jwts.parser()
.setSigningKey(AuthConfig.SECRET_KEY.getBytes())
.parseClaimsJws(jwt)
.getBody();
} catch (JwtException e) {
return false;
}

return true;
}

private String generateToken(Map<String, Object> valueMap, int expireSeconds, String secretKey) {
Map<String, Object> headers = new HashMap<>();
headers.put("typ", "JWT");
headers.put("alg", "HS256");

Map<String, Object> payload = new HashMap<>();
payload.putAll(valueMap);

return Jwts.builder()
.setHeader(headers)
.setClaims(payload)
.setIssuedAt(
Date.from(
ZonedDateTime.now().toInstant()
))
.setExpiration(
getDate(expireSeconds)
)
.signWith(SignatureAlgorithm.HS256, secretKey.getBytes()).compact();
}

private Date getDate(int seconds) {
LocalDateTime localDateTime = LocalDateTime.now().plusSeconds(seconds);
return Date.from(
localDateTime.atZone(
ZoneId.systemDefault()
)
.toInstant()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package co.kr.ticketing.member.auth.util;

import java.util.Map;

import org.springframework.stereotype.Component;

import co.kr.ticketing.member.auth.config.AuthConfig;
import co.kr.ticketing.member.domain.model.Member;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;

@Component
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class TokenUtil {
JwtUtil jwtUtil;

public String generateToken(Member member) {
Map<String, Object> payload = Map.of(
AuthConfig.PHONE_NUMBER, member.getPhoneNumber()
);

return jwtUtil.generateJwt(payload, AuthConfig.TOKEN_VALID_TIME);
}

public boolean isValidToken(String token) {
return jwtUtil.isValidJwt(token);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package co.kr.ticketing.member.common;

import lombok.Getter;

@Getter
public class ResponseDto<T> {
private final String code;
private final T body;

public ResponseDto(String code) {
this.code = code;
this.body = null;
}

public ResponseDto(String code, T body) {
this.code = code;
this.body = body;
}
}
Loading
Loading