Skip to content

Commit

Permalink
Merge pull request #42 from Orange-Co/main
Browse files Browse the repository at this point in the history
deploy
  • Loading branch information
Kang1221 authored Aug 10, 2024
2 parents 4bdfa03 + 986e8cc commit cdeb7c1
Show file tree
Hide file tree
Showing 34 changed files with 495 additions and 53 deletions.
2 changes: 1 addition & 1 deletion DDANZI_Server_yml
14 changes: 9 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
//lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
//jpa
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// mysql
implementation 'mysql:mysql-connector-java:8.0.32'
//redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

//lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

// security
implementation 'org.springframework.boot:spring-boot-starter-security'
//jwt
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/co/orange/ddanzi/controller/AuthController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package co.orange.ddanzi.controller;

import co.orange.ddanzi.dto.auth.LoginDto;
import co.orange.ddanzi.global.common.response.ApiResponse;
import co.orange.ddanzi.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/auth")
public class AuthController {
private final AuthService authService;

@PostMapping("/signin/test")
ApiResponse<?> signin(@RequestBody LoginDto requestDto){
return authService.testSignin(requestDto.getIdToken());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import co.orange.ddanzi.domain.user.User;
import co.orange.ddanzi.dto.item.ConfirmProductRequestDto;
import co.orange.ddanzi.dto.item.SaveItemRequestDto;
import co.orange.ddanzi.global.common.exception.Error;
import co.orange.ddanzi.global.common.error.Error;
import co.orange.ddanzi.global.common.response.ApiResponse;
import co.orange.ddanzi.repository.AddressRepository;
import co.orange.ddanzi.repository.UserRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ ApiResponse<?> getSetting(){
return settingService.getSetting();
}

@GetMapping("/address/enter")
ApiResponse<?> enterAddress(){
return settingService.enterAddress();
}

@GetMapping("/address")
ApiResponse<?> getAddress(){
return settingService.getAddress();
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/co/orange/ddanzi/domain/user/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public class Address {
@Column(name = "address_id")
private Long id; //주소 고유 ID

@Column(name = "recipient")
private String recipient;

@Column(name = "recipient_phone")
private String recipientPhone;

@Column(name = "zip_code", nullable = false, length = 5)
private String zipCode; //우편번호

Expand All @@ -36,8 +42,10 @@ public class Address {
private User user; //회원:주소=1:N

@Builder
public Address(String zipCode, AddressType type, String address, String detailAddress, User user) {
public Address(String zipCode, String recipient, String recipientPhone, AddressType type, String address, String detailAddress, User user) {
this.zipCode = zipCode;
this.recipient = recipient;
this.recipientPhone = recipientPhone;
this.type = type;
this.address = address;
this.detailAddress = detailAddress;
Expand All @@ -46,6 +54,8 @@ public Address(String zipCode, AddressType type, String address, String detailAd

public void update(AddressRequestDto requestDto){
this.zipCode = requestDto.getZipCode();
this.recipient = requestDto.getRecipient();
this.recipientPhone = requestDto.getRecipientPhone();
this.type = requestDto.getType();
this.address = requestDto.getAddress();
this.detailAddress = requestDto.getDetailAddress();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/co/orange/ddanzi/dto/ProductInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public class ProductInfo {
private Integer originPrice;
private Integer salePrice;
private Integer interestCount;
private Boolean isInterested;
}
11 changes: 11 additions & 0 deletions src/main/java/co/orange/ddanzi/dto/auth/AuthResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.orange.ddanzi.dto.auth;

import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class AuthResponseDto {
private String accesstoken;
private String nickname;
}
11 changes: 11 additions & 0 deletions src/main/java/co/orange/ddanzi/dto/auth/LoginDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.orange.ddanzi.dto.auth;

import co.orange.ddanzi.domain.user.enums.LoginType;
import lombok.Getter;

@Getter
public class LoginDto {
private String idToken;
private LoginType type;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

@Getter
public class AddressRequestDto {
private String recipient;
private String recipientPhone;
private String zipCode;
private AddressType type;
private String address;
Expand All @@ -15,6 +17,8 @@ public class AddressRequestDto {
public Address toEntity(User user){
return Address.builder()
.user(user)
.recipient(recipient)
.recipientPhone(recipientPhone)
.zipCode(zipCode)
.type(type)
.address(address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
@Builder
public class AddressResponseDto {
private Long addressId;
private String name;
private String recipient;
private String zipCode;
private AddressType type;
private String address;
private String detailAddress;
private String phone;
private String recipientPhone;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.orange.ddanzi.dto.setting;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class EnterAddressResponseDto {
private String name;
private String phone;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package co.orange.ddanzi.global.common.exception;
package co.orange.ddanzi.global.common.error;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -16,13 +16,17 @@ public enum Error {
DUE_DATE_IS_INCORRECT(HttpStatus.BAD_REQUEST, "The due date is incorrect."),

// 401 UNAUTHORIZED

INVALID_JWT_EXCEPTION(HttpStatus.UNAUTHORIZED, "Invalid JWT"),
LOG_OUT_JWT_TOKEN(HttpStatus.UNAUTHORIZED,"Logged out user"),
JWT_EXPIRED(HttpStatus.UNAUTHORIZED,"JWT expired"),
JWT_TOKEN_NOT_EXISTS(HttpStatus.UNAUTHORIZED,"JWT value does not exist in header"),

// 403 Forbidden


// 404 NOT FOUND
AUTHENTICATION_INFO_NOT_FOUND(HttpStatus.NOT_FOUND, "The Authentication of user does not exist."),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "User does not exist."),
PRODUCT_NOT_FOUND(HttpStatus.NOT_FOUND, "The product does not exist."),
CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND, "The category of the product does not exist."),
DISCOUNT_INFO_NOT_FOUND(HttpStatus.NOT_FOUND, "The discount info of the category does not exist."),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package co.orange.ddanzi.global.common.exception;
package co.orange.ddanzi.global.common.error;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.servlet.http.HttpServletRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package co.orange.ddanzi.global.common.exception;

import co.orange.ddanzi.global.common.error.Error;
import lombok.Getter;

@Getter
public class ApiException extends RuntimeException {
private final Error error;

public ApiException(Error error){
super(error.getMessage());
this.error = error;
}

public int getHttpStatus(){
return error.getHttpStatusCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package co.orange.ddanzi.global.common.exception;

import co.orange.ddanzi.global.common.error.Error;

public class UnauthorizedException extends ApiException{
public UnauthorizedException(Error error) {
super(error);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package co.orange.ddanzi.global.common.exception;

import co.orange.ddanzi.global.common.error.Error;

public class UserNotFoundException extends ApiException{
public UserNotFoundException() {
super(Error.USER_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.orange.ddanzi.global.common.response;

import co.orange.ddanzi.global.common.exception.Error;
import co.orange.ddanzi.global.common.error.Error;
import co.orange.ddanzi.global.config.handler.GlobalControllerHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package co.orange.ddanzi.global.config.handler;

import co.orange.ddanzi.global.common.exception.Error;
import co.orange.ddanzi.global.common.exception.ErrorResponse;
import co.orange.ddanzi.global.common.error.Error;
import co.orange.ddanzi.global.common.error.ErrorResponse;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/co/orange/ddanzi/global/config/jwt/AuthUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package co.orange.ddanzi.global.config.jwt;

import co.orange.ddanzi.domain.user.User;
import co.orange.ddanzi.global.common.exception.UserNotFoundException;
import co.orange.ddanzi.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;


@Slf4j
@RequiredArgsConstructor
@Component
public class AuthUtils {
private final UserRepository userRepository;

public User getUser() {
String currentUserNickname = getCurrentUserNickname();
if (currentUserNickname == null) {
return null;
}
return userRepository.findByLoginId(currentUserNickname)
.orElseThrow(() -> new UserNotFoundException());

}

public Authentication getAuthentication() {
// SecurityContext에서 인증 정보 가져오기
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication;
}

public Object getPrincipal() {
// 현재 사용자의 principal 가져오기
return getAuthentication().getPrincipal();

}

public String getCurrentUserNickname() {
Object principalObject = getPrincipal();

if (principalObject instanceof UserDetails) {
UserDetails userDetails = (UserDetails) principalObject;
log.info("id token -> {}", userDetails.getUsername());
return userDetails.getUsername();
}
return null;
}
}
46 changes: 46 additions & 0 deletions src/main/java/co/orange/ddanzi/global/config/jwt/JwtFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package co.orange.ddanzi.global.config.jwt;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

@Slf4j
@RequiredArgsConstructor
@Component
public class JwtFilter extends OncePerRequestFilter {
private final JwtUtils jwtUtils;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

String token = jwtUtils.resolveJWT(request);
log.info("Request to {}: token={}", request.getRequestURI(), token);

if (token != null && jwtUtils.validateToken(token)) {
Authentication authentication = jwtUtils.getAuthentication(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
else {
log.info("No valid token found, proceeding without authentication");
}

filterChain.doFilter(request, response);
}

@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
String path = request.getRequestURI();
return path.startsWith("/api/v1/auth")
|| path.equals("/api/v1/search")
;
}
}
Loading

0 comments on commit cdeb7c1

Please sign in to comment.