Skip to content

Commit

Permalink
gitignore 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
youKeon committed Mar 2, 2023
1 parent fe99df0 commit edfa7b0
Show file tree
Hide file tree
Showing 26 changed files with 174 additions and 43 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ prometheus/data

.env

src/main/resources/application-dbconfig.properties
src/main/resources/application-dbconfig.properties
src/main/resources/application-jwt.yml
src/main/resources/application-oauth.yml
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ dependencies {
implementation 'org.apache.httpcomponents:httpclient:4.5.12'
implementation 'org.apache.httpcomponents:httpmime:4.3.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:2.3.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.2'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.tecst.tecst.domain.answer.entity;

import com.tecst.tecst.domain.Type;
import com.tecst.tecst.domain.common_question.entity.CommonQuestion;
import com.tecst.tecst.domain.user.entity.User;
import lombok.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.tecst.tecst.domain.common_question.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tecst.tecst.domain.Type;
import com.tecst.tecst.domain.answer.entity.Answer;
import com.tecst.tecst.domain.bookmark.entity.Bookmark;
import com.tecst.tecst.domain.user.entity.User;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.client;

import com.tecst.tecst.domain.oauth.dto.KakaoUserResponse;
import com.tecst.tecst.domain.oauth.exception.TokenValidFailedException;
import com.tecst.tecst.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.client;

import com.tecst.tecst.domain.user.entity.User;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package com.tecst.tecst.global.security;
package com.tecst.tecst.domain.oauth.config;

import com.tecst.tecst.domain.oauth.AuthTokenProvider;
import com.tecst.tecst.domain.oauth.JwtAuthenticationFilter;
import com.tecst.tecst.domain.oauth.jwt.AuthTokenProvider;
import com.tecst.tecst.domain.oauth.jwt.JwtAuthenticationFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.web.cors.CorsConfiguration;

@RequiredArgsConstructor
@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
package com.tecst.tecst.domain.oauth.controller;public class OauthController {
package com.tecst.tecst.domain.oauth.controller;

import com.tecst.tecst.domain.oauth.dto.ApiResponse;
import com.tecst.tecst.domain.oauth.dto.AuthRequest;
import com.tecst.tecst.domain.oauth.dto.AuthResponse;
import com.tecst.tecst.domain.oauth.jwt.AuthTokenProvider;
import com.tecst.tecst.domain.oauth.service.KakaoAuthService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class OauthController {
private final KakaoAuthService kakaoAuthService;
private final AuthTokenProvider authTokenProvider;

@ApiOperation(value = "카카오 로그인", notes = "카카오 엑세스 토큰을 이용하여 사용자 정보 받아 저장하고 앱의 토큰 신환")
@PostMapping(value = "/kakao")
public ResponseEntity<AuthResponse> kakaoAuthRequest(@RequestBody AuthRequest authRequest) {
return ApiResponse.success(kakaoAuthService.login(authRequest));
}
}
27 changes: 25 additions & 2 deletions src/main/java/com/tecst/tecst/domain/oauth/dto/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
package com.tecst.tecst.domain.oauth.dto;public class ApiResponse {
}
package com.tecst.tecst.domain.oauth.dto;

import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@Getter
public class ApiResponse<T> {

public static <T> ResponseEntity<T> success(T body) {
return ResponseEntity.status(HttpStatus.OK).body(body);
}

public static <T> ResponseEntity<T> created(T body) {
return ResponseEntity.status(HttpStatus.CREATED).body(body);
}

public static <T> ResponseEntity<T> fail(T body) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(body);
}

public static <T> ResponseEntity<T> forbidden(T body) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(body);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.enumerate;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.exception;

public class TokenValidFailedException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.jwt;

import com.tecst.tecst.domain.oauth.enumerate.RoleType;
import io.jsonwebtoken.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.jwt;

import com.tecst.tecst.domain.oauth.exception.TokenValidFailedException;
import com.tecst.tecst.domain.oauth.enumerate.RoleType;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.security.Keys;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.jwt;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tecst.tecst.domain.oauth;
package com.tecst.tecst.domain.oauth.jwt;

import javax.servlet.http.HttpServletRequest;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
package com.tecst.tecst.domain.oauth.service;public class KakaoAuthService {
}
package com.tecst.tecst.domain.oauth.service;

import com.tecst.tecst.domain.oauth.client.ClientKakao;
import com.tecst.tecst.domain.oauth.dto.AuthRequest;
import com.tecst.tecst.domain.oauth.dto.AuthResponse;
import com.tecst.tecst.domain.oauth.jwt.AuthToken;
import com.tecst.tecst.domain.oauth.jwt.AuthTokenProvider;
import com.tecst.tecst.domain.user.entity.User;
import com.tecst.tecst.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class KakaoAuthService {

private final ClientKakao clientKakao;
private final AuthTokenProvider authTokenProvider;
private final UserRepository userRepository;

@Transactional
public AuthResponse login(AuthRequest authRequest) {
User kakaoMember = clientKakao.getUserData(authRequest.getAccessToken());
String socialId = kakaoMember.getEmail();
User member = userRepository.findByEmail(socialId);

AuthToken appToken = authTokenProvider.createUserAppToken(socialId);

if (member == null) {
userRepository.save(kakaoMember);
return AuthResponse.builder()
.appToken(appToken.getToken())
.isNewUser(Boolean.TRUE)
.build();
}

return AuthResponse.builder()
.appToken(appToken.getToken())
.isNewUser(Boolean.FALSE)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,40 @@
package com.tecst.tecst.domain.oauth.service;public class OauthService {
package com.tecst.tecst.domain.oauth.service;

import com.tecst.tecst.domain.oauth.client.ClientKakao;
import com.tecst.tecst.domain.oauth.dto.AuthRequest;
import com.tecst.tecst.domain.oauth.dto.AuthResponse;
import com.tecst.tecst.domain.oauth.jwt.AuthToken;
import com.tecst.tecst.domain.oauth.jwt.AuthTokenProvider;
import com.tecst.tecst.domain.user.entity.User;
import com.tecst.tecst.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@RequiredArgsConstructor
public class OauthService {

private final UserRepository userRepository;
private final ClientKakao clientKakao;
private final AuthTokenProvider authTokenProvider;

@Transactional
public AuthResponse login(AuthRequest authRequest) {
User kakaoMember = clientKakao.getUserData(authRequest.getAccessToken());
String email = kakaoMember.getEmail();
User user = userRepository.findByEmail(email);

AuthToken appToken = authTokenProvider.createUserAppToken(email);

if (user == null) {
userRepository.save(kakaoMember);
}

return AuthResponse.builder()
.appToken(appToken.getToken())
.build();
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/tecst/tecst/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ public class User {
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Answer> answers = new ArrayList<Answer>();
@Builder
private User(String email, String password) {
public User(String email, String password) {
this.email = email;
this.password = password;
}

public void updateRefreshToken(String reIssuedRefreshToken) {

}

// @OneToMany(mappedBy = "User")
// private List<Personal_Question> personal_questions = new ArrayList<Personal_Question>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import com.tecst.tecst.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
import java.util.UUID;

public interface UserRepository extends JpaRepository<User, UUID> {
User findByUserId(UUID id);

User findByEmail(String email);

}
3 changes: 3 additions & 0 deletions src/main/java/com/tecst/tecst/global/result/ResultCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public enum ResultCode {
BOOKMARK_REGISTRATION_SUCCESS("B001", "북마크 등록 성공"),
BOOKMARK_DELETE_SUCCESS("B002", "북마크 삭제 성공"),

// Oauth
TOKEN_GET_SUCESS("T001", "토근 수령 성공"),

// answers(사용자가 입력한 정답)
REGISTER_ANSWER_SUCCESS("A001", "응답 등록 성공"),
COMMENT_FIND_SUCCESS("A002", "응답 찾기 성공"),
Expand Down
Empty file.
10 changes: 0 additions & 10 deletions src/main/resources/application-oauth.yml

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

0 comments on commit edfa7b0

Please sign in to comment.