Skip to content

Commit a354e5b

Browse files
authored
Feat: 자체엑세스토큰을 통해 로그인 (#124)
* chore: 디렉토리 구조 수정 * feat: login By accessToken * remove: todo 주석 삭제
1 parent ec8a76c commit a354e5b

16 files changed

+90
-104
lines changed

src/main/java/com/flint/flint/config/SecurityConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.flint.flint.config;
22

3-
import com.flint.flint.security.auth.jwt.JwtAuthenticationEntryPoint;
4-
import com.flint.flint.security.auth.jwt.JwtAuthenticationFilter;
5-
import com.flint.flint.security.auth.jwt.JwtService;
3+
import com.flint.flint.security.jwt.JwtAuthenticationEntryPoint;
4+
import com.flint.flint.security.jwt.JwtAuthenticationFilter;
5+
import com.flint.flint.security.jwt.JwtService;
66
import lombok.RequiredArgsConstructor;
77
import org.springframework.context.annotation.Bean;
88
import org.springframework.context.annotation.Configuration;

src/main/java/com/flint/flint/mail/controller/MailController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.flint.flint.mail.dto.request.SuccessUniversityAuthRequest;
66
import com.flint.flint.mail.dto.response.EmailAuthNumberRespose;
77
import com.flint.flint.mail.service.AuthEmailService;
8-
import com.flint.flint.security.auth.dto.AuthenticationResponse;
8+
import com.flint.flint.security.auth.dto.response.AuthenticationResponse;
99
import com.flint.flint.security.auth.dto.AuthorityMemberDTO;
1010
import jakarta.validation.Valid;
1111
import lombok.RequiredArgsConstructor;

src/main/java/com/flint/flint/mail/service/AuthEmailService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.flint.flint.member.spec.Authority;
1111
import com.flint.flint.redis.RedisUtil;
1212
import com.flint.flint.security.auth.AuthenticationService;
13-
import com.flint.flint.security.auth.dto.AuthenticationResponse;
13+
import com.flint.flint.security.auth.dto.response.AuthenticationResponse;
1414
import jakarta.transaction.Transactional;
1515
import lombok.RequiredArgsConstructor;
1616
import org.springframework.http.HttpStatus;

src/main/java/com/flint/flint/member/domain/main/Policy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.flint.flint.common.BaseTimeEntity;
44
import com.flint.flint.member.spec.Agree;
5-
import com.flint.flint.security.auth.dto.RegisterRequest;
5+
import com.flint.flint.security.auth.dto.request.RegisterRequest;
66
import jakarta.persistence.*;
77
import jakarta.validation.constraints.NotNull;
88
import lombok.Builder;
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.flint.flint.security.auth;
22

33
import com.flint.flint.common.ResponseForm;
4-
import com.flint.flint.security.auth.dto.AuthenticationResponse;
5-
import com.flint.flint.security.auth.dto.RegisterRequest;
6-
import com.flint.flint.security.oauth.dto.AuthorizionRequestHeader;
4+
import com.flint.flint.security.auth.dto.response.AuthenticationResponse;
5+
import com.flint.flint.security.auth.dto.request.RegisterRequest;
6+
import jakarta.servlet.http.HttpServletRequest;
77
import jakarta.validation.Valid;
88
import lombok.RequiredArgsConstructor;
99
import org.springframework.http.HttpHeaders;
@@ -24,29 +24,28 @@ public class AuthenticationController {
2424
//테스트용
2525

2626
@PostMapping("/register")
27-
public ResponseForm<AuthenticationResponse> register(@Valid @RequestBody RegisterRequest registerRequest, @RequestHeader(HttpHeaders.AUTHORIZATION) AuthorizionRequestHeader authorizionRequestHeader) {
28-
AuthenticationResponse authenticationResponse = authenticationService.register(registerRequest, authorizionRequestHeader);
27+
public ResponseForm<AuthenticationResponse> register(@Valid @RequestBody RegisterRequest registerRequest, HttpServletRequest oauth2TokenWithBearer) {
28+
AuthenticationResponse authenticationResponse = authenticationService.register(registerRequest, oauth2TokenWithBearer);
2929
return new ResponseForm<>(authenticationResponse);
3030

3131
}
3232

3333
@PostMapping("/login/{providerName}")
34-
public ResponseForm<AuthenticationResponse> login(@PathVariable String providerName, @RequestHeader(HttpHeaders.AUTHORIZATION) AuthorizionRequestHeader authorizionRequestHeader) {
35-
AuthenticationResponse authenticationResponse = authenticationService.login(providerName, authorizionRequestHeader);
34+
public ResponseForm<AuthenticationResponse> loginByOauth2Provider(@PathVariable String providerName, HttpServletRequest oauth2TokenWithBearer) {
35+
AuthenticationResponse authenticationResponse = authenticationService.loginByOauth2Provider(providerName, oauth2TokenWithBearer);
3636
return new ResponseForm<>(authenticationResponse);
3737
}
3838

39-
//TODO
40-
// @PostMapping("/withdraw")
41-
// public void removeMember(@PathVariable String id) {
42-
// Optional<Member> member = memberRepository.findById(id);
43-
// memberRepository.deleteById(id);
44-
// authenticationService.deleteRedisToken(email);
45-
// }
39+
@PostMapping("/login/accesstoken")
40+
public ResponseForm loginByAccessToken(HttpServletRequest accessTokenWithBearer) {
41+
authenticationService.loginByAccessToken(accessTokenWithBearer);
42+
return new ResponseForm<>();
43+
}
44+
4645

4746
@PostMapping("/renew")
48-
public ResponseForm<AuthenticationResponse> newTokenByRefreshToken(@RequestHeader(HttpHeaders.AUTHORIZATION) String refreshToken) {
49-
AuthenticationResponse authenticationResponse = authenticationService.newTokenByRefreshToken(refreshToken);
47+
public ResponseForm<AuthenticationResponse> newTokenByRefreshToken(HttpServletRequest refreshTokenWithBearer) {
48+
AuthenticationResponse authenticationResponse = authenticationService.newTokenByRefreshToken(refreshTokenWithBearer);
5049
return new ResponseForm<>(authenticationResponse);
5150
}
52-
}
51+
}

src/main/java/com/flint/flint/security/auth/AuthenticationService.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import com.flint.flint.member.repository.MemberRepository;
99
import com.flint.flint.member.repository.PolicyRepository;
1010
import com.flint.flint.redis.RedisUtil;
11-
import com.flint.flint.security.auth.dto.AuthenticationResponse;
11+
import com.flint.flint.security.auth.dto.response.AuthenticationResponse;
1212
import com.flint.flint.security.auth.dto.ClaimsDTO;
13-
import com.flint.flint.security.auth.dto.RegisterRequest;
14-
import com.flint.flint.security.auth.jwt.JwtService;
15-
import com.flint.flint.security.oauth.dto.AuthorizionRequestHeader;
16-
import com.flint.flint.security.oauth.dto.OAuth2UserAttribute;
17-
import com.flint.flint.security.oauth.dto.OAuth2UserAttributeFactory;
13+
import com.flint.flint.security.auth.dto.request.RegisterRequest;
14+
import com.flint.flint.security.jwt.JwtService;
15+
import com.flint.flint.security.oauth.OAuth2UserAttribute;
16+
import com.flint.flint.security.oauth.OAuth2UserAttributeFactory;
17+
import jakarta.servlet.http.HttpServletRequest;
1818
import jakarta.transaction.Transactional;
1919
import lombok.RequiredArgsConstructor;
2020
import org.springframework.beans.factory.annotation.Value;
@@ -43,38 +43,48 @@ public class AuthenticationService {
4343
* member 저장, 수신동의 저장, 엑세스,리프레쉬토큰 생성, redis에 리프레쉬 토큰 저장
4444
*/
4545
@Transactional
46-
public AuthenticationResponse register(RegisterRequest registerRequest, AuthorizionRequestHeader authorizionRequestHeader) {
46+
public AuthenticationResponse register(RegisterRequest registerRequest, HttpServletRequest oauth2TokenWithBearer) {
4747
//카카오인지 네이버인지 선택
4848
OAuth2UserAttribute oAuth2UserAttribute = OAuth2UserAttributeFactory.of(registerRequest.getProviderName());
49-
String oauth2AccessToekn = authorizionRequestHeader.getAccessToken().replace("Bearer ", "");
49+
String oauth2AccessToken = jwtService.parseTokenFrom(oauth2TokenWithBearer);
5050
//정보 추출
51-
oAuth2UserAttribute.setUserAttributesByOauthToken(oauth2AccessToekn);
51+
oAuth2UserAttribute.setUserAttributesByOauthToken(oauth2AccessToken);
5252
checkRegistration(oAuth2UserAttribute.getProviderId());
5353
Member member = saveInformation(registerRequest, oAuth2UserAttribute);
5454
return generateToken(member);
5555
}
5656

5757
/**
58-
* 유저 리프레쉬 토큰의 만료기간까지 다 지났을 때 로그인
59-
* (리프레쉬 토큰이 살아있을 때 로그인은 newTokenByRefreshToken()를 호출한다)
60-
* 엑세스,리프레쉬토큰 생성, redis에 리프레쉬 토큰 저장
58+
* Oauth2Provider 토큰으로 로그인
59+
* 첫 로그인 혹은 엑세스,리프레쉬 토큰 없을 때
6160
*/
6261
@Transactional
63-
public AuthenticationResponse login(String providerName, AuthorizionRequestHeader authorizionRequestHeader) {
62+
public AuthenticationResponse loginByOauth2Provider(String providerName, HttpServletRequest oauth2TokenWithBearer) {
6463
OAuth2UserAttribute oAuth2UserAttribute = OAuth2UserAttributeFactory.of(providerName);
65-
String oauth2AccessToekn = authorizionRequestHeader.getAccessToken().replace("Bearer ", "");
64+
String oauth2AccessToken = jwtService.parseTokenFrom(oauth2TokenWithBearer);
6665
//정보 추출
67-
oAuth2UserAttribute.setUserAttributesByOauthToken(oauth2AccessToekn);
66+
oAuth2UserAttribute.setUserAttributesByOauthToken(oauth2AccessToken);
6867
String providerId = oAuth2UserAttribute.getProviderId();
6968
Member member = memberRepository.findByProviderId(providerId).orElseThrow(() -> new FlintCustomException(HttpStatus.NOT_FOUND, ResultCode.USER_NOT_JOINED));
7069
return generateToken(member);
7170
}
7271

7372
/**
74-
* 리프레쉬 토큰 재발급
73+
* 엑세스 토큰으로 로그인
7574
*/
7675
@Transactional
77-
public AuthenticationResponse newTokenByRefreshToken(String refreshToken) {
76+
public void loginByAccessToken(HttpServletRequest accessTokenWithBearer) {
77+
String accessToken = jwtService.parseTokenFrom(accessTokenWithBearer);
78+
jwtService.isTokenValid(accessToken);
79+
}
80+
81+
82+
/**
83+
* 리프레쉬 토큰으로 로그인
84+
*/
85+
@Transactional
86+
public AuthenticationResponse newTokenByRefreshToken(HttpServletRequest refreshTokenWithBearer) {
87+
String refreshToken = jwtService.parseTokenFrom(refreshTokenWithBearer);
7888
String providerId = jwtService.parseProviderId(refreshToken);
7989
Member member = memberRepository.findByProviderId(providerId).orElseThrow(() -> new FlintCustomException(HttpStatus.NOT_FOUND, ResultCode.USER_NOT_FOUND));
8090
jwtService.isTokenValid(refreshToken);
@@ -84,6 +94,7 @@ public AuthenticationResponse newTokenByRefreshToken(String refreshToken) {
8494
throw new FlintCustomException(HttpStatus.BAD_REQUEST, ResultCode.REFRESHTOKEN_OUTDATED);
8595
}
8696

97+
8798
/**
8899
* 엑세스 토큰 리프레쉬 토큰 생성, 레디쉬에 리프레쉬 토큰 저장
89100
*/
@@ -118,4 +129,4 @@ private Member saveInformation(RegisterRequest registerRequest, OAuth2UserAttrib
118129
policyRepository.save(policy);
119130
return member;
120131
}
121-
}
132+
}

src/main/java/com/flint/flint/security/auth/dto/RegisterRequest.java renamed to src/main/java/com/flint/flint/security/auth/dto/request/RegisterRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.flint.flint.security.auth.dto;
1+
package com.flint.flint.security.auth.dto.request;
22

33
import jakarta.validation.constraints.NotBlank;
44
import lombok.*;

src/main/java/com/flint/flint/security/auth/dto/AuthenticationResponse.java renamed to src/main/java/com/flint/flint/security/auth/dto/response/AuthenticationResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.flint.flint.security.auth.dto;
1+
package com.flint.flint.security.auth.dto.response;
22

33
import lombok.AllArgsConstructor;
44
import lombok.Builder;

src/main/java/com/flint/flint/security/auth/jwt/JwtAuthenticationEntryPoint.java renamed to src/main/java/com/flint/flint/security/jwt/JwtAuthenticationEntryPoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.flint.flint.security.auth.jwt;
1+
package com.flint.flint.security.jwt;
22

33
import jakarta.servlet.http.HttpServletRequest;
44
import jakarta.servlet.http.HttpServletResponse;

src/main/java/com/flint/flint/security/auth/jwt/JwtAuthenticationFilter.java renamed to src/main/java/com/flint/flint/security/jwt/JwtAuthenticationFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.flint.flint.security.auth.jwt;
1+
package com.flint.flint.security.jwt;
22

33
import jakarta.servlet.FilterChain;
44
import jakarta.servlet.ServletException;

0 commit comments

Comments
 (0)