-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feat(#10): RestTemplate 이용한 카카오 계정 정보 받아오기 로직 작성 * Feat(#10): 카카오 계정 이메일 추출 * Feat(#10): 네이버 카카오 인가코드 이용해 사용자 정보 추출 * Feat(#10): jwt 인증 구현 * Feat(#10): Redis 이용한 리프레쉬 토큰 구현 * Chore(#10): docker-compose.yml에 redis 추가 * Feat(#10): swagger 내용 작성 * Feat(#10): 토큰 갱신 api 구현 * Refactor(#10): 코드 리팩토링 * Fix(#10): inner class protected 삭제 * Fix(#10): gitignore 오타 수정
- Loading branch information
1 parent
eac59ee
commit 1b3af09
Showing
28 changed files
with
678 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,5 @@ application.properties | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
**/src/main/generated/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/numberone/backend/LoginTestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.numberone.backend; | ||
|
||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class LoginTestController { | ||
@RequestMapping("/logintest") | ||
public String test(Authentication authentication){ | ||
return "Hello "+authentication.getName(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/numberone/backend/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.numberone.backend.config; | ||
|
||
import com.numberone.backend.properties.RedisProperties; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class RedisConfig { | ||
private final RedisProperties redisProperties; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort()); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate() { | ||
RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
return redisTemplate; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/numberone/backend/config/RestTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.numberone.backend.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class RestTemplateConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/numberone/backend/config/auth/JwtFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.numberone.backend.config.auth; | ||
|
||
import com.numberone.backend.domain.member.entity.Member; | ||
import com.numberone.backend.domain.member.service.MemberService; | ||
import com.numberone.backend.domain.token.util.JwtUtil; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class JwtFilter extends OncePerRequestFilter { | ||
private final JwtUtil jwtUtil; | ||
private final MemberService memberService; | ||
|
||
//토큰이 유효하지 않다면 setAuthentication이 진행되지 않아 UsernamePasswordAuthenticationFilter에서 인증이 되지 않음 | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) { | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
String token = authorizationHeader.split(" ")[1]; | ||
if (jwtUtil.isExpired(token)) { | ||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
response.getWriter().write("Token has expired"); | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
String email = jwtUtil.getEmail(token); | ||
Member member = memberService.findByEmail(email); | ||
|
||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken( | ||
member.getEmail(), null, Collections.emptyList()); | ||
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authenticationToken); | ||
filterChain.doFilter(request, response); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/numberone/backend/domain/member/entity/Member.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.numberone.backend.domain.member.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
public class Member { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String email; | ||
|
||
@Builder | ||
public Member(String email) { | ||
this.email = email; | ||
} | ||
|
||
public static Member of(String email){ | ||
return Member.builder() | ||
.email(email) | ||
.build(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/numberone/backend/domain/member/repository/MemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.numberone.backend.domain.member.repository; | ||
|
||
import com.numberone.backend.domain.member.entity.Member; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface MemberRepository extends JpaRepository<Member, Long> { | ||
Optional<Member> findByEmail(String email); | ||
boolean existsByEmail(String email); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/numberone/backend/domain/member/service/MemberService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.numberone.backend.domain.member.service; | ||
|
||
import com.numberone.backend.domain.member.entity.Member; | ||
import com.numberone.backend.domain.member.repository.MemberRepository; | ||
import com.numberone.backend.exception.notfound.NotFoundMemberException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Transactional(readOnly = true) | ||
public class MemberService { | ||
private final MemberRepository memberRepository; | ||
|
||
public Member findByEmail(String email) { | ||
return memberRepository.findByEmail(email) | ||
.orElseThrow(NotFoundMemberException::new); | ||
} | ||
|
||
public void create(String email) { | ||
memberRepository.save(Member.of(email)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/numberone/backend/domain/token/controller/TokenController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.numberone.backend.domain.token.controller; | ||
|
||
import com.numberone.backend.domain.token.dto.request.TokenRequest; | ||
import com.numberone.backend.domain.token.dto.response.TokenResponse; | ||
import com.numberone.backend.domain.token.service.TokenService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
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; | ||
|
||
@Tag(name = "token", description = "토큰 관련 API") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/token") | ||
public class TokenController { | ||
private final TokenService tokenService; | ||
|
||
@Operation(summary = "카카오 토큰을 이용하여 서버 JWT 토큰 발급받기", description = | ||
""" | ||
카카오 토큰을 body 에 담아서 post 요청 해주세요. | ||
앞으로 서버 요청 시에 사용할 수 있는 JWT 토큰이 발급됩니다. | ||
이후 서버에 API 요청시 이 JWT 토큰을 같이 담아서 요청해야 정상적으로 API가 호출 됩니다. | ||
""") | ||
@PostMapping("/kakao") | ||
public TokenResponse loginKakao(@RequestBody TokenRequest tokenRequest) { | ||
return tokenService.loginKakao(tokenRequest); | ||
} | ||
|
||
@Operation(summary = "네이버 토큰을 이용하여 서버 JWT 토큰 발급받기", description = | ||
""" | ||
네이버 토큰을 body 에 담아서 post 요청 해주세요. | ||
앞으로 서버 요청 시에 사용할 수 있는 JWT 토큰이 발급됩니다. | ||
이후 서버에 API 요청시 이 JWT 토큰을 같이 담아서 요청해야 정상적으로 API가 호출 됩니다. | ||
""") | ||
@PostMapping("/naver") | ||
public TokenResponse loginNaver(@RequestBody TokenRequest tokenRequest) { | ||
return tokenService.loginNaver(tokenRequest); | ||
} | ||
|
||
@Operation(summary = "만료된 JWT 토큰 갱신하기", description = | ||
""" | ||
만료된 JWT 토큰을 body 에 담아서 post 요청 해주세요. | ||
새로 사용할 수 있는 JWT 토큰이 발급됩니다. | ||
""") | ||
@PostMapping("/refresh") | ||
public TokenResponse refresh(@RequestBody TokenRequest tokenRequest) { | ||
return tokenService.refresh(tokenRequest); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/numberone/backend/domain/token/dto/request/TokenRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.numberone.backend.domain.token.dto.request; | ||
|
||
import lombok.*; | ||
|
||
@ToString | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class TokenRequest { | ||
private String token; | ||
} |
Oops, something went wrong.