-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat]#7 authentication #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d88daa
feat: add /auth route
Sehi55 63aab99
feat: gradle ์์กด์ฑ ๋ฐ applcation.yml ์ค์ ์ถ๊ฐ
Sehi55 0f993a4
feat: cors ํํฐ ์ค์ ์ถ๊ฐ
Sehi55 3bbdb94
feat: security ํํฐ ์ค์ ์ถ๊ฐ
Sehi55 46d353b
feat: ์ธ์ฆ ํํฐ ์ถ๊ฐ
Sehi55 1fa9a69
fix: harden gateway auth header handling
Sehi55 0d18656
fix: add jwt.secret
Sehi55 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -35,4 +35,6 @@ out/ | |
| /.nb-gradle/ | ||
|
|
||
| ### VS Code ### | ||
| .vscode/ | ||
| .vscode/ | ||
|
|
||
| .env | ||
This file contains hidden or 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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/michelet/gateway/infrastructure/config/CorsConfig.java
This file contains hidden or 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,41 @@ | ||
| package com.michelet.gateway.infrastructure.config; | ||
|
|
||
| import java.util.List; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.web.cors.CorsConfiguration; | ||
| import org.springframework.web.cors.reactive.CorsWebFilter; | ||
| import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; | ||
|
|
||
| @Configuration | ||
| @EnableConfigurationProperties(CorsProperties.class) | ||
| public class CorsConfig { | ||
| private final CorsProperties corsProperties; | ||
|
|
||
| public CorsConfig(CorsProperties corsProperties) { | ||
| this.corsProperties = corsProperties; | ||
| } | ||
|
|
||
| @Bean | ||
| public CorsWebFilter corsWebFilter(){ | ||
| CorsConfiguration config = new CorsConfiguration(); | ||
| config.setAllowedOrigins(corsProperties.allowedOrigins()); | ||
| config.setAllowedMethods(List.of( | ||
| HttpMethod.GET.name(), | ||
| HttpMethod.DELETE.name(), | ||
| HttpMethod.POST.name(), | ||
| HttpMethod.PUT.name(), | ||
| HttpMethod.PATCH.name(), | ||
| HttpMethod.OPTIONS.name() | ||
| )); | ||
| config.setAllowedHeaders(List.of("*")); | ||
| config.setAllowCredentials(true); //์ฟ ํค์ refreshToken | ||
|
|
||
| UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
| source.registerCorsConfiguration("/**", config); | ||
|
|
||
| return new CorsWebFilter(source); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/michelet/gateway/infrastructure/config/CorsProperties.java
This file contains hidden or 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.michelet.gateway.infrastructure.config; | ||
|
|
||
| import java.util.List; | ||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| @ConfigurationProperties(prefix = "cors") | ||
| public record CorsProperties( | ||
| List<String> allowedOrigins | ||
| ) { | ||
| } |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/michelet/gateway/infrastructure/config/SecurityConfig.java
This file contains hidden or 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,51 @@ | ||
| package com.michelet.gateway.infrastructure.config; | ||
|
|
||
| import com.michelet.gateway.infrastructure.security.GatewayRoleConverter; | ||
| import java.nio.charset.StandardCharsets; | ||
| import javax.crypto.SecretKey; | ||
| import javax.crypto.spec.SecretKeySpec; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; | ||
| import org.springframework.security.config.web.server.ServerHttpSecurity; | ||
| import org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder; | ||
| import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder; | ||
| import org.springframework.security.oauth2.server.resource.authentication.ReactiveJwtAuthenticationConverter; | ||
| import org.springframework.security.web.server.SecurityWebFilterChain; | ||
|
|
||
| @Configuration | ||
| @EnableWebFluxSecurity | ||
| public class SecurityConfig { | ||
| @Bean | ||
| public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http, GatewayRoleConverter roleConverter){ | ||
| ReactiveJwtAuthenticationConverter jwtAuthenticationConverter = new ReactiveJwtAuthenticationConverter(); | ||
| jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(roleConverter); | ||
| return http | ||
| .csrf(ServerHttpSecurity.CsrfSpec::disable) | ||
| .authorizeExchange( | ||
| exchanges-> exchanges.pathMatchers( | ||
| "/api/*/auth/login", | ||
| "/api/*/auth/reissue", | ||
| "/api/*/users/signup" | ||
| ).permitAll() | ||
| .pathMatchers("/api/*/admin/**").hasRole("MASTER") | ||
| .anyExchange().authenticated() | ||
|
|
||
| ) | ||
| .oauth2ResourceServer(oauth2-> oauth2.jwt( | ||
| jwtSpec -> jwtSpec.jwtAuthenticationConverter(jwtAuthenticationConverter) | ||
| )).build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public ReactiveJwtDecoder reactiveJwtDecoder(@Value("${jwt.secret}") String secret){ | ||
| SecretKey secretKey = new SecretKeySpec( | ||
| secret.getBytes(StandardCharsets.UTF_8), | ||
| "HmacSHA256" | ||
| ); | ||
| return NimbusReactiveJwtDecoder.withSecretKey(secretKey).build(); | ||
| } | ||
|
|
||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/michelet/gateway/infrastructure/security/GatewayRoleConverter.java
This file contains hidden or 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,22 @@ | ||
| package com.michelet.gateway.infrastructure.security; | ||
|
|
||
| import org.springframework.core.convert.converter.Converter; | ||
| import org.springframework.security.core.GrantedAuthority; | ||
| import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
| import org.springframework.security.oauth2.jwt.Jwt; | ||
| import org.springframework.stereotype.Component; | ||
| import reactor.core.publisher.Flux; | ||
|
|
||
| @Component | ||
| public class GatewayRoleConverter implements Converter<Jwt, Flux<GrantedAuthority>> { | ||
|
|
||
| @Override | ||
| public Flux<GrantedAuthority> convert(Jwt jwt) { | ||
| String role = jwt.getClaimAsString("role"); | ||
|
|
||
| if(role == null || role.isBlank()) | ||
| return Flux.empty(); | ||
|
|
||
| return Flux.just(new SimpleGrantedAuthority("ROLE_" +role)); | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/michelet/gateway/infrastructure/security/JwtAuthenticationFilter.java
This file contains hidden or 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,71 @@ | ||
| package com.michelet.gateway.infrastructure.security; | ||
|
|
||
| import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
| import org.springframework.cloud.gateway.filter.GlobalFilter; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.http.server.reactive.ServerHttpRequest; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.context.ReactiveSecurityContextHolder; | ||
| import org.springframework.security.oauth2.jwt.Jwt; | ||
| import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.server.ServerWebExchange; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| @Component | ||
| public class JwtAuthenticationFilter implements GlobalFilter, Ordered { | ||
| private static final String USER_ID_HEADER = "X-User-Id"; | ||
| private static final String USER_ROLE_HEADER = "X-User-Role"; | ||
|
|
||
|
|
||
| @Override | ||
| public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | ||
| return ReactiveSecurityContextHolder.getContext() | ||
| .flatMap(securityContext -> { | ||
| Authentication authentication = securityContext.getAuthentication(); | ||
| if(authentication == null){ | ||
| return chain.filter(exchange); | ||
| } | ||
| return addHeaders(authentication,exchange,chain); | ||
| }) | ||
| .switchIfEmpty(chain.filter(exchange)); | ||
| } | ||
|
|
||
| public Mono<Void> addHeaders( | ||
| Authentication authentication, | ||
| ServerWebExchange exchange, | ||
| GatewayFilterChain chain | ||
| ){ | ||
| if(!(authentication instanceof JwtAuthenticationToken)) | ||
| return chain.filter(exchange); | ||
| Jwt jwt = ((JwtAuthenticationToken) authentication).getToken(); | ||
|
|
||
| ServerHttpRequest mutatedRequest = exchange.getRequest() | ||
| .mutate() | ||
| .headers( | ||
| httpHeaders -> { | ||
| httpHeaders.remove(USER_ID_HEADER); | ||
| httpHeaders.remove(USER_ROLE_HEADER); | ||
|
|
||
| String userId = jwt.getSubject(); | ||
| if (userId != null && !userId.isBlank()) { | ||
| httpHeaders.set(USER_ID_HEADER, userId); | ||
| } | ||
|
|
||
| String role = jwt.getClaimAsString("role"); | ||
| if (role != null && !role.isBlank()) { | ||
| httpHeaders.set(USER_ROLE_HEADER, role); | ||
| } | ||
| } | ||
| ) | ||
| .build(); | ||
|
|
||
| return chain.filter(exchange.mutate().request(mutatedRequest).build()); | ||
| } | ||
|
|
||
| @Override | ||
| public int getOrder() { | ||
| return Ordered.LOWEST_PRECEDENCE - 5; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| eureka: | ||
| client: | ||
| service-url: | ||
| defaultZone: http://eureka-server:8761/eureka/ | ||
| defaultZone: http://${EUREKA_CONTAINER_NAME}:${EUREKA_PORT}/eureka/ | ||
|
Sehi55 marked this conversation as resolved.
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| eureka: | ||
| client: | ||
| service-url: | ||
| defaultZone: http://localhost:8761/eureka/ | ||
| defaultZone: http://localhost:${EUREKA_PORT:8761}/eureka/ | ||
|
|
||
| cors: | ||
| allowed-origins: | ||
| - http://localhost:3000 |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.