Skip to content

Commit

Permalink
Merge branch 'dev' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
Jyuung committed Aug 22, 2024
2 parents d71d5e8 + f622316 commit 13bb4a0
Show file tree
Hide file tree
Showing 83 changed files with 1,852 additions and 181 deletions.
2 changes: 1 addition & 1 deletion delivery/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

//implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-security'

implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package delivery.common.config.entrypoint;

import com.fasterxml.jackson.databind.ObjectMapper;
import global.api.Api;
import global.errorcode.ErrorCode;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {

Api<Object> body = Api.ERROR(ErrorCode.MISSING_REQUIRED_HEADER);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
OutputStream responseStream = response.getOutputStream();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(responseStream, body);
responseStream.flush();

}
}
12 changes: 12 additions & 0 deletions delivery/src/main/java/delivery/common/config/jpa/JpaConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package delivery.common.config.jpa;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@EntityScan(basePackages = {"db"})
@EnableJpaRepositories(basePackages = {"db"})
public class JpaConfig {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package delivery.common.exception.jwt;

import global.errorcode.ErrorCodeIfs;

public class TokenException extends RuntimeException{

private final ErrorCodeIfs errorCodeIfs;
private final String description;

public TokenException(ErrorCodeIfs errorCodeIfs) {
super(errorCodeIfs.getDescription());
this.errorCodeIfs = errorCodeIfs;
this.description = errorCodeIfs.getDescription();
}

public TokenException(ErrorCodeIfs errorCodeIfs, String errorDescription) {
this.errorCodeIfs = errorCodeIfs;
this.description = errorDescription;
}

public TokenException(ErrorCodeIfs errorCodeIfs, Throwable throwable) {
super(throwable);
this.errorCodeIfs = errorCodeIfs;
this.description = errorCodeIfs.getDescription();
}

public TokenException(ErrorCodeIfs errorCodeIfs, Throwable throwable,
String errorDescription) {
super(throwable);
this.errorCodeIfs = errorCodeIfs;
this.description = errorDescription;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package delivery.common.exception.jwt;

import global.errorcode.ErrorCodeIfs;

public class TokenExpiredException extends RuntimeException{

private final ErrorCodeIfs errorCodeIfs;
private final String description;

public TokenExpiredException(ErrorCodeIfs errorCodeIfs) {
super(errorCodeIfs.getDescription());
this.errorCodeIfs = errorCodeIfs;
this.description = errorCodeIfs.getDescription();
}

public TokenExpiredException(ErrorCodeIfs errorCodeIfs, String errorDescription) {
this.errorCodeIfs = errorCodeIfs;
this.description = errorDescription;
}

public TokenExpiredException(ErrorCodeIfs errorCodeIfs, Throwable throwable) {
super(throwable);
this.errorCodeIfs = errorCodeIfs;
this.description = errorCodeIfs.getDescription();
}

public TokenExpiredException(ErrorCodeIfs errorCodeIfs, Throwable throwable,
String errorDescription) {
super(throwable);
this.errorCodeIfs = errorCodeIfs;
this.description = errorDescription;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package delivery.common.exception.jwt;

import global.errorcode.ErrorCodeIfs;

public class TokenSignatureException extends RuntimeException{

private final ErrorCodeIfs errorCodeIfs;
private final String description;

public TokenSignatureException(ErrorCodeIfs errorCodeIfs) {
super(errorCodeIfs.getDescription());
this.errorCodeIfs = errorCodeIfs;
this.description = errorCodeIfs.getDescription();
}

public TokenSignatureException(ErrorCodeIfs errorCodeIfs, String errorDescription) {
this.errorCodeIfs = errorCodeIfs;
this.description = errorDescription;
}

public TokenSignatureException(ErrorCodeIfs errorCodeIfs, Throwable throwable) {
super(throwable);
this.errorCodeIfs = errorCodeIfs;
this.description = errorCodeIfs.getDescription();
}

public TokenSignatureException(ErrorCodeIfs errorCodeIfs, Throwable throwable,
String errorDescription) {
super(throwable);
this.errorCodeIfs = errorCodeIfs;
this.description = errorDescription;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package delivery.common.exception.user;

import global.errorcode.ErrorCodeIfs;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class UserNameNotFoundException extends UsernameNotFoundException {

private final ErrorCodeIfs errorCodeIfs;
private final String description;

public UserNameNotFoundException(ErrorCodeIfs errorCodeIfs) {
super(errorCodeIfs.getDescription());
this.errorCodeIfs = errorCodeIfs;
this.description = errorCodeIfs.getDescription();
}

public UserNameNotFoundException(ErrorCodeIfs errorCodeIfs, String errorDescription) {
super(errorCodeIfs.getDescription());
this.errorCodeIfs = errorCodeIfs;
this.description = errorDescription;
}

public UserNameNotFoundException(ErrorCodeIfs errorCodeIfs, Throwable throwable) {
super(throwable.toString());
this.errorCodeIfs = errorCodeIfs;
this.description = errorCodeIfs.getDescription();
}

public UserNameNotFoundException(ErrorCodeIfs errorCodeIfs, Throwable throwable,
String errorDescription) {
super(throwable.toString());
this.errorCodeIfs = errorCodeIfs;
this.description = errorDescription;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package delivery.common.security;

import delivery.domain.users.security.jwt.filter.JwtAuthFilter;
import delivery.domain.users.security.jwt.service.TokenService;
import delivery.domain.users.security.service.AuthorizationService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


@Configuration
@EnableWebSecurity // security 활성화
@EnableGlobalAuthentication
@RequiredArgsConstructor
public class SecurityConfig {

private final AuthenticationEntryPoint authEntryPoint;

private final AuthorizationService authorizationService;
private final TokenService tokenService;

private final List<String> WHITE_LIST = List.of("/swagger-ui.html", "/swagger-ui/**",
"/v3/api-docs/**", "/open-api/**");

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {

httpSecurity.cors(cors -> cors.disable())
.addFilterBefore(new JwtAuthFilter(authorizationService, tokenService),
UsernamePasswordAuthenticationFilter.class)
.csrf((csrfConfig) -> csrfConfig.disable()) // 1번
.sessionManagement(sessionManagement -> sessionManagement.sessionCreationPolicy(
SessionCreationPolicy.STATELESS)).authorizeHttpRequests(it -> {
it.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.requestMatchers(WHITE_LIST.toArray(new String[0])).permitAll().anyRequest()
.authenticated()
;
}).formLogin(AbstractHttpConfigurer::disable).httpBasic(AbstractHttpConfigurer::disable)
.httpBasic(basic -> basic.authenticationEntryPoint(authEntryPoint))
.exceptionHandling(Customizer.withDefaults())
;

return httpSecurity.build();
}

@Bean
public BCryptPasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().requestMatchers(WHITE_LIST.toArray(new String[0]));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import db.domain.users.UserEntity;
import delivery.common.error.GoodsErrorCode;
import delivery.common.error.ReceivingErrorCode;
import delivery.common.error.ShippingErrorCode;
import delivery.common.exception.goods.GoodsNotInReceivingException;
import delivery.common.exception.goods.GoodsNotInShippingIngException;
import delivery.common.exception.receiving.ReceivingNotInConfirmationException;
import delivery.common.utils.datetime.DateTimeUtils;
import delivery.common.utils.datetime.DateTimeUtils.RequestDateTime;
Expand All @@ -20,7 +18,7 @@
import delivery.domain.receiving.converter.ReceivingConverter;
import delivery.domain.receiving.service.ReceivingService;
import delivery.domain.users.converter.UserConverter;
import delivery.domain.users.service.UserService;
import delivery.domain.users.security.jwt.service.UsersService;
import global.annotation.Business;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -34,7 +32,7 @@ public class ReceivingBusiness {

private final ReceivingService receivingService;
private final ReceivingConverter receivingConverter;
private final UserService userService;
private final UsersService userService;
private final UserConverter userConverter;
private final GoodsService goodsService;
private final GoodsConverter goodsConverter;
Expand Down Expand Up @@ -74,7 +72,7 @@ private ReceivingResponse setGoodsIdAndUserNameReceivingResponse(
}).toList();

ReceivingResponse response = receivingConverter.toResponse(receivingEntity);
UserEntity userEntity = userService.getUserBy(receivingEntity.getUserId());
UserEntity userEntity = userService.getUserWithThrow(receivingEntity.getUserId());
response.setGoodsIdList(goodsIdList);
response.setUserName(userEntity.getName());
return response;
Expand Down Expand Up @@ -106,7 +104,7 @@ private ReceivingResponseList getResponseList(List<ReceivingEntity> receivingEnt
responseList.getReservationResponseList().forEach(reservationResponse -> {
reservationResponse.setGoodsIdList(goodsIdList);
reservationResponse.setUserName(
userService.getUserBy(receivingEntity.getUserId()).getName());
userService.getUserWithThrow(receivingEntity.getUserId()).getName());
});

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,25 @@

import db.domain.goods.GoodsEntity;
import db.domain.goods.enums.GoodsStatus;
import db.domain.receiving.ReceivingEntity;
import db.domain.receiving.enums.ReceivingStatus;
import db.domain.shipping.ShippingEntity;
import db.domain.shipping.enums.ShippingStatus;
import db.domain.users.UserEntity;
import delivery.common.error.GoodsErrorCode;
import delivery.common.error.ReceivingErrorCode;
import delivery.common.error.ShippingErrorCode;
import delivery.common.exception.goods.GoodsNotFoundException;
import delivery.common.exception.goods.GoodsNotInShippingIngException;
import delivery.common.exception.goods.GoodsNotInStorageException;
import delivery.common.exception.receiving.ReceivingNotInConfirmationException;
import delivery.common.exception.shipping.ShippingNotInRegisteredException;
import delivery.common.utils.datetime.DateTimeUtils;
import delivery.common.utils.datetime.DateTimeUtils.RequestDateTime;
import delivery.domain.goods.converter.GoodsConverter;
import delivery.domain.goods.service.GoodsService;
import delivery.domain.receiving.controller.model.ReceivingResponse;
import delivery.domain.receiving.controller.model.ReceivingResponseList;
import delivery.domain.shipping.controller.model.ShippingResponse;
import delivery.domain.shipping.controller.model.ShippingResponseList;
import delivery.domain.shipping.converter.ShippingConverter;
import delivery.domain.shipping.service.ShippingService;
import delivery.domain.users.converter.UserConverter;
import delivery.domain.users.service.UserService;
import delivery.domain.users.security.jwt.service.UsersService;
import global.annotation.Business;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -40,7 +31,7 @@ public class ShippingBusiness {

private final ShippingService shippingService;
private final ShippingConverter shippingConverter;
private final UserService userService;
private final UsersService userService;
private final UserConverter userConverter;
private final GoodsService goodsService;
private final GoodsConverter goodsConverter;
Expand Down Expand Up @@ -77,7 +68,7 @@ private ShippingResponse getShippingResponse(ShippingEntity shippingEntity) {
}
).toList();

UserEntity userEntity = userService.getUserBy(shippingEntity.getUserId());
UserEntity userEntity = userService.getUserWithThrow(shippingEntity.getUserId());

response.setUserName(userEntity.getName());
response.setGoodsIdList(goodsIdList);
Expand Down Expand Up @@ -109,7 +100,7 @@ private ShippingResponseList getResponseList(List<ShippingEntity> shippingEntity

responseList.getReservationResponseList().forEach(reservationResponse -> {
reservationResponse.setGoodsIdList(goodsIdList);
reservationResponse.setUserName(userService.getUserBy(shippingEntity.getUserId()).getName());
reservationResponse.setUserName(userService.getUserWithThrow(shippingEntity.getUserId()).getName());
});

});
Expand Down
Loading

0 comments on commit 13bb4a0

Please sign in to comment.