-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: S3 Proxy 서버에 서브모듈을 활용한 인가 과정 추가 (#684)
* chore: s3 proxy에 config 서브모듈 추가 * feat: S3Proxy 서버에 인가 로직 추가 * refactor: S3ProxyUploader 미사용 임포트문 삭제 * fix: kafka appender 위치 변경 * chore: s3 proxy 서브모듈 최신화 * fix: AuthInterceptor 변경된 config에 맞도록 설정 변경 * refactor: Interceptor 설정될 경로 추가
- Loading branch information
1 parent
3890bc9
commit aa05210
Showing
17 changed files
with
168 additions
and
20 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 |
---|---|---|
|
@@ -10,3 +10,7 @@ | |
path = backend/src/main/resources/infra-appender | ||
url = [email protected]:zzimkkong/infra-appender.git | ||
branch = main | ||
[submodule "s3proxy/src/main/resources/config"] | ||
path = s3proxy/src/main/resources/config | ||
url = [email protected]:zzimkkong/config.git | ||
branch = main |
29 changes: 29 additions & 0 deletions
29
backend/src/main/java/com/woowacourse/zzimkkong/config/S3ProxyConfig.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,29 @@ | ||
package com.woowacourse.zzimkkong.config; | ||
|
||
import com.woowacourse.zzimkkong.infrastructure.thumbnail.S3ProxyUploader; | ||
import com.woowacourse.zzimkkong.infrastructure.thumbnail.StorageUploader; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.context.annotation.PropertySource; | ||
|
||
@Configuration | ||
@PropertySource("classpath:config/s3proxy.properties") | ||
public class S3ProxyConfig { | ||
@Bean(name = "storageUploader") | ||
@Profile("prod") | ||
public StorageUploader storageUploaderProd( | ||
@Value("${s3proxy.server-uri.prod}") final String serverUri, | ||
@Value("${s3proxy.secret-key.prod}") final String secretKey) { | ||
return new S3ProxyUploader(serverUri, secretKey); | ||
} | ||
|
||
@Bean(name = "storageUploader") | ||
@Profile({"dev", "local", "test"}) | ||
public StorageUploader storageUploaderDev( | ||
@Value("${s3proxy.server-uri.dev}") final String serverUri, | ||
@Value("${s3proxy.secret-key.dev}") final String secretKey) { | ||
return new S3ProxyUploader(serverUri, secretKey); | ||
} | ||
} |
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
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
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
Submodule config
updated
from 41eb5e to 6c04ab
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
26 changes: 26 additions & 0 deletions
26
s3proxy/src/main/java/com/woowacourse/s3proxy/config/AuthInterceptorConfig.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,26 @@ | ||
package com.woowacourse.s3proxy.config; | ||
|
||
import com.woowacourse.s3proxy.infrastructure.AuthInterceptor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.context.annotation.PropertySource; | ||
|
||
@Configuration | ||
@PropertySource("classpath:config/s3proxy.properties") | ||
public class AuthInterceptorConfig { | ||
@Bean(name = "authInterceptor") | ||
@Profile("prod") | ||
public AuthInterceptor authInterceptorProd( | ||
@Value("${s3proxy.secret-key.prod}") String secretKey) { | ||
return new AuthInterceptor(secretKey); | ||
} | ||
|
||
@Bean(name = "authInterceptor") | ||
@Profile({"dev", "local", "test"}) | ||
public AuthInterceptor authInterceptorDev( | ||
@Value("${s3proxy.secret-key.dev}") String secretKey) { | ||
return new AuthInterceptor(secretKey); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
s3proxy/src/main/java/com/woowacourse/s3proxy/config/AuthenticationPrincipalConfig.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,21 @@ | ||
package com.woowacourse.s3proxy.config; | ||
|
||
import com.woowacourse.s3proxy.infrastructure.AuthInterceptor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class AuthenticationPrincipalConfig implements WebMvcConfigurer { | ||
private final AuthInterceptor authInterceptor; | ||
|
||
public AuthenticationPrincipalConfig(AuthInterceptor authInterceptor) { | ||
this.authInterceptor = authInterceptor; | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(authInterceptor) | ||
.addPathPatterns("/api/storage/*"); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...c/main/java/com/woowacourse/s3proxy/exception/AuthorizationHeaderUninvolvedException.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.woowacourse.s3proxy.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public class AuthorizationHeaderUninvolvedException extends S3ProxyException { | ||
private static final String MESSAGE = "인가에 실패했습니다."; | ||
public AuthorizationHeaderUninvolvedException() { | ||
super(MESSAGE, HttpStatus.UNAUTHORIZED); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
s3proxy/src/main/java/com/woowacourse/s3proxy/infrastructure/AuthInterceptor.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,35 @@ | ||
package com.woowacourse.s3proxy.infrastructure; | ||
|
||
import com.woowacourse.s3proxy.exception.AuthorizationHeaderUninvolvedException; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
|
||
public class AuthInterceptor implements HandlerInterceptor { | ||
private final String secretKey; | ||
|
||
public AuthInterceptor(String secretKey) { | ||
this.secretKey = secretKey; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
if (isPreflight(request)) { | ||
return true; | ||
} | ||
|
||
String secretKey = AuthorizationExtractor.extractAccessToken(request); | ||
if (secretKey.equals(this.secretKey)) { | ||
return true; | ||
} | ||
|
||
throw new AuthorizationHeaderUninvolvedException(); | ||
} | ||
|
||
private boolean isPreflight(HttpServletRequest request) { | ||
return request.getMethod().equals(HttpMethod.OPTIONS.toString()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
s3proxy/src/main/java/com/woowacourse/s3proxy/infrastructure/AuthorizationExtractor.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,20 @@ | ||
package com.woowacourse.s3proxy.infrastructure; | ||
|
||
import com.woowacourse.s3proxy.exception.AuthorizationHeaderUninvolvedException; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Enumeration; | ||
|
||
public class AuthorizationExtractor { | ||
private static final String AUTHORIZATION_HEADER_KEY = "Authorization"; | ||
|
||
private AuthorizationExtractor() { | ||
} | ||
public static String extractAccessToken(HttpServletRequest request) { | ||
Enumeration<String> headers = request.getHeaders(AUTHORIZATION_HEADER_KEY); | ||
if (headers.hasMoreElements()) { | ||
return headers.nextElement(); | ||
} | ||
throw new AuthorizationHeaderUninvolvedException(); | ||
} | ||
} |
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