Skip to content

Commit

Permalink
feat: 응답을 위한 TokenResponse 구현 및 기타 구현 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
choidongkuen committed Jan 4, 2024
1 parent 4e27f1a commit 9d19b7e
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 63 deletions.
2 changes: 2 additions & 0 deletions src/main/java/net/teumteum/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;


@SpringBootApplication
public class Application {

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/teumteum/core/property/JwtProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class JwtProperty {

@Getter
@Setter
static class Access{
public static class Access{
private long expiration;
private String header;

}

@Getter
@Setter
static class Refresh {
public static class Refresh {
private long expiration;
private String header;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.teumteum.core.security;

/* 소셜 OAuth 로그인 타입 */
public enum Authenticated {
카카오,네이버;
}
6 changes: 2 additions & 4 deletions src/main/java/net/teumteum/core/security/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ SecurityFilterChain securityFilterChain(HttpSecurity http, HandlerMappingIntrosp
http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(request
-> request.requestMatchers(allowedUrl).permitAll()
.requestMatchers(PathRequest.toH2Console()).permitAll()
.anyRequest().authenticated()
)
-> request.requestMatchers("/**").permitAll()
.requestMatchers(PathRequest.toH2Console()).permitAll())
.httpBasic(AbstractHttpConfigurer::disable)
.formLogin(AbstractHttpConfigurer::disable)
.sessionManagement(sessionManagement
Expand Down
25 changes: 0 additions & 25 deletions src/main/java/net/teumteum/core/security/SecurityUtil.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Getter
public class UserAuthentication extends AbstractAuthenticationToken {

private final Long id;
private Long id;
private final String oauthId;

public UserAuthentication(User user) {
Expand Down Expand Up @@ -45,4 +45,8 @@ public Object getPrincipal() {
public boolean isAuthenticated() {
return true;
}

public void setUserId(Long userId) {
id = userId;
}
}
18 changes: 18 additions & 0 deletions src/main/java/net/teumteum/core/security/dto/TokenResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.teumteum.core.security.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class TokenResponse {
private String accessToken;
private String refreshToken;

@Builder
public TokenResponse(String accessToken, String refreshToken) {
this.accessToken = accessToken;
this.refreshToken = refreshToken;
}
}
19 changes: 19 additions & 0 deletions src/main/java/net/teumteum/core/security/service/AuthService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.teumteum.core.security.service;

import lombok.RequiredArgsConstructor;
import net.teumteum.user.domain.User;
import net.teumteum.user.domain.UserConnector;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@RequiredArgsConstructor
public class AuthService {
private final JwtService jwtService;
private final UserConnector userConnector;
public Optional<User> findUserByToken(String accessToken) {
Long id = Long.parseLong(jwtService.getUserIdFromToken(accessToken));
return userConnector.findUserById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Arrays;
import lombok.RequiredArgsConstructor;
import net.teumteum.core.error.ErrorResponse;
import net.teumteum.core.security.SecurityUtil;
import net.teumteum.core.security.service.SecurityService;
import net.teumteum.user.domain.request.UserUpdateRequest;
import net.teumteum.user.domain.response.UserGetResponse;
import net.teumteum.user.domain.response.UsersGetByIdResponse;
Expand All @@ -20,15 +20,13 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.LoginContext;

@RestController
@RequiredArgsConstructor
@RequestMapping("/users")
public class UserController {

private final UserService userService;
private final SecurityUtil securityUtil;
private final SecurityService securityService;

/* userId 로 회원 조회 */
@GetMapping("/{userId}")
Expand Down Expand Up @@ -68,6 +66,6 @@ public ErrorResponse handleIllegalArgumentException(IllegalArgumentException ill
}

private Long getCurrentUserId() {
return securityUtil.getCurrentUserId();
return securityService.getCurrentUserId();
}
}
49 changes: 49 additions & 0 deletions src/test/java/net/teumteum/core/property/PropertyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.teumteum.core.property;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;

import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("property 관련 테스트의 ")
@TestPropertySource(locations = {
"classpath:application-auth.yml",
"classpath:application-redis.yml"
})
@SpringBootTest
class PropertyTest {

@Autowired
private JwtProperty jwtProperty;

@Autowired
private RedisProperty redisProperty;

@Nested
@DisplayName("jwtProperty 주입 테스트가")
class JwtPropertyTest {

@Test
@DisplayName("정상적으로 성공한다.")
void test(){
String bearer = jwtProperty.getBearer();
System.out.println(bearer);
}

}

@Nested
@DisplayName("redisProperty 주입 테스트가")
class RedisPropertyTest {
@Test
@DisplayName("정상적으로 성공한다.")
void test(){
String host = redisProperty.getHost();
System.out.println(host);
}
}

}
6 changes: 3 additions & 3 deletions src/test/java/net/teumteum/integration/IntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.teumteum.integration;

import net.teumteum.Application;
import net.teumteum.core.security.SecurityUtil;
import net.teumteum.core.security.service.SecurityService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -10,7 +10,7 @@
import org.springframework.test.context.ContextConfiguration;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = {Application.class, Api.class, Repository.class, TestLoginContext.class})
@ContextConfiguration(classes = {Application.class, Api.class, Repository.class})
abstract public class IntegrationTest {

@Autowired
Expand All @@ -20,7 +20,7 @@ abstract public class IntegrationTest {
protected Repository repository;

@Autowired
protected SecurityUtil securityUtil;
protected SecurityService securityService;

@AfterEach
@BeforeEach
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/net/teumteum/integration/SecurityUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.teumteum.integration;

import net.teumteum.core.security.UserAuthentication;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.security.auth.login.LoginContext;

@TestComponent
public class SecurityUtil {

public void clearSecurityContext() {
SecurityContextHolder.clearContext();
}

/* 해당 요청에서 로그인한 회원 id 반환 */
public Long getCurrentUserId() {
UserAuthentication userAuthentication = getUserAuthentication();
return userAuthentication.getId();
}

/* 해당 요청에서 로그인한 회원 OAuth id 반환 */
public String getCurrentUserOAuthId() {
UserAuthentication userAuthentication = getUserAuthentication();
return userAuthentication.getOauthId();
}

public void setUserId(Long userId) {
UserAuthentication userAuthentication = getUserAuthentication();
userAuthentication.setUserId(userId);
}

private static UserAuthentication getUserAuthentication() {
return (UserAuthentication) SecurityContextHolder.getContext().getAuthentication();
}
}
21 changes: 0 additions & 21 deletions src/test/java/net/teumteum/integration/TestLoginContext.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void Update_user_info() {
var existUser = repository.saveAndGetUser();
var updateUser = RequestFixture.userUpdateRequest(existUser);

loginContext.setUserId(existUser.getId());
securityService.setUserId(existUser.getId());

// when
var result = api.updateUser(VALID_TOKEN, updateUser);
Expand All @@ -135,7 +135,7 @@ void Return_200_ok_with_success_make_friends() {
var myToken = "JWT MY_TOKEN";
var friend = repository.saveAndGetUser();

loginContext.setUserId(me.getId());
securityService.setUserId(me.getId());

// when
var result = api.addFriends(myToken, friend.getId());
Expand Down

0 comments on commit 9d19b7e

Please sign in to comment.