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

[BE] 패키지 구조 변경 #828

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong;
package haengdong;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.config;
package haengdong.common.auth;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.domain;
package haengdong.common.auth;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package server.haengdong.application;
package haengdong.common.auth.application;


import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import server.haengdong.domain.TokenProvider;
import server.haengdong.domain.user.Role;
import server.haengdong.exception.AuthenticationException;
import server.haengdong.exception.HaengdongErrorCode;
import haengdong.event.application.EventService;
import haengdong.common.auth.TokenProvider;
import haengdong.user.domain.Role;
import haengdong.common.exception.AuthenticationException;
import haengdong.common.exception.HaengdongErrorCode;

@Slf4j
public class AuthService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package server.haengdong.infrastructure.auth;
package haengdong.common.auth.infrastructure;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Arrays;
import server.haengdong.exception.AuthenticationException;
import server.haengdong.exception.HaengdongErrorCode;
import haengdong.common.exception.AuthenticationException;
import haengdong.common.exception.HaengdongErrorCode;

public class AuthenticationExtractor {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package server.haengdong.infrastructure.auth;
package haengdong.common.auth.infrastructure;

import haengdong.common.properties.JwtProperties;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtException;
Expand All @@ -8,42 +9,42 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import server.haengdong.domain.TokenProvider;
import haengdong.common.auth.TokenProvider;

public class JwtTokenProvider implements TokenProvider {

private final TokenProperties tokenProperties;
private final JwtProperties jwtProperties;

public JwtTokenProvider(TokenProperties tokenProperties) {
this.tokenProperties = tokenProperties;
public JwtTokenProvider(JwtProperties jwtProperties) {
this.jwtProperties = jwtProperties;
}

@Override
public String createToken(Map<String, Object> payload) {
Claims claims = Jwts.claims(new HashMap<>(payload));
Date now = new Date();
Date validity = new Date(now.getTime() + tokenProperties.expireLength());
Date validity = new Date(now.getTime() + jwtProperties.expireLength());

return Jwts.builder()
.setClaims(claims)
.setIssuedAt(now)
.setExpiration(validity)
.signWith(SignatureAlgorithm.HS256, tokenProperties.secretKey())
.signWith(SignatureAlgorithm.HS256, jwtProperties.secretKey())
.compact();
}

@Override
public Map<String, Object> getPayload(String token) {
return Jwts.parser()
.setSigningKey(tokenProperties.secretKey())
.setSigningKey(jwtProperties.secretKey())
.parseClaimsJws(token)
.getBody();
}

@Override
public boolean validateToken(String token) {
try {
Jws<Claims> claims = Jwts.parser().setSigningKey(tokenProperties.secretKey()).parseClaimsJws(token);
Jws<Claims> claims = Jwts.parser().setSigningKey(jwtProperties.secretKey()).parseClaimsJws(token);

return !claims.getBody().getExpiration().before(new Date());
} catch (JwtException | IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.infrastructure;
package haengdong.common.config;

import com.zaxxer.hikari.HikariDataSource;
import jakarta.persistence.EntityManagerFactory;
Expand All @@ -16,6 +16,7 @@
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import haengdong.common.infrastructure.DynamicRoutingDataSource;

@Profile("prod")
@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.config;
package haengdong.common.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
package server.haengdong.config;

package haengdong.common.config;

import haengdong.common.auth.TokenProvider;
import haengdong.common.auth.application.AuthService;
import haengdong.common.auth.infrastructure.AuthenticationExtractor;
import haengdong.common.auth.infrastructure.JwtTokenProvider;
import haengdong.common.infrastructure.AdminInterceptor;
import haengdong.common.properties.CorsProperties;
import haengdong.common.properties.JwtProperties;
import haengdong.event.application.EventService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import server.haengdong.application.AuthService;
import server.haengdong.application.EventService;
import server.haengdong.domain.TokenProvider;
import server.haengdong.infrastructure.auth.AuthenticationExtractor;
import server.haengdong.infrastructure.auth.JwtTokenProvider;
import server.haengdong.infrastructure.auth.TokenProperties;

@RequiredArgsConstructor
@EnableConfigurationProperties(TokenProperties.class)
@EnableConfigurationProperties({JwtProperties.class, CorsProperties.class})
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

private final TokenProperties tokenProperties;
private final JwtProperties jwtProperties;
private final EventService eventService;

@Value("${cors.max-age}")
private Long maxAge;

@Value("${cors.allowed-origins}")
private String[] allowedOrigins;
private final CorsProperties corsProperties;

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(allowedOrigins)
.allowedOrigins(corsProperties.allowedOrigins())
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(maxAge);
.maxAge(corsProperties.maxAge());
}

@Override
Expand All @@ -57,7 +53,7 @@ public AuthService authService() {

@Bean
public TokenProvider tokenProvider() {
return new JwtTokenProvider(tokenProperties);
return new JwtTokenProvider(jwtProperties);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.domain;
package haengdong.common.domain;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.exception;
package haengdong.common.exception;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.exception;
package haengdong.common.exception;

public record ErrorResponse(
String errorCode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.exception;
package haengdong.common.exception;

import jakarta.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.exception;
package haengdong.common.exception;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.exception;
package haengdong.common.exception;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.config;
package haengdong.common.infrastructure;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -7,10 +7,10 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import server.haengdong.application.AuthService;
import server.haengdong.exception.AuthenticationException;
import server.haengdong.exception.HaengdongErrorCode;
import server.haengdong.infrastructure.auth.AuthenticationExtractor;
import haengdong.common.auth.application.AuthService;
import haengdong.common.exception.AuthenticationException;
import haengdong.common.exception.HaengdongErrorCode;
import haengdong.common.auth.infrastructure.AuthenticationExtractor;

@Slf4j
public class AdminInterceptor implements HandlerInterceptor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package server.haengdong.config;
package haengdong.common.infrastructure;

import static server.haengdong.config.AdminInterceptor.LOGIN_MEMBER_REQUEST;
import static haengdong.common.infrastructure.AdminInterceptor.LOGIN_MEMBER_REQUEST;

import haengdong.common.auth.Login;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.infrastructure;
package haengdong.common.infrastructure;

import static org.springframework.transaction.support.TransactionSynchronizationManager.isCurrentTransactionReadOnly;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.config;
package haengdong.common.infrastructure;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.haengdong.infrastructure.auth;
package haengdong.common.properties;

import java.time.Duration;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package haengdong.common.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("cors")
public record CorsProperties(
Long maxAge,
String[] allowedOrigins
) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package server.haengdong.infrastructure.auth;
package haengdong.common.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("security.jwt.token")
public record TokenProperties(String secretKey, Long expireLength) {
public record JwtProperties(String secretKey, Long expireLength) {
}
Loading
Loading