-
Notifications
You must be signed in to change notification settings - Fork 0
fix(#7): DI 적용하는 피드백 반영 #10
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
12 commits
Select commit
Hold shift + click to select a range
ea8da0c
feat/#5-Swagger 설정 추가
ParkJiYeoung8297 c24e513
fix: swagger와 spring 버전 호환 문제로 swagger 버전 수정
ParkJiYeoung8297 a0b987f
feat: SpringSecurity 설정 추가
ParkJiYeoung8297 0ca21c8
chore: 코드 체크스타일 맞게 수정
ParkJiYeoung8297 f4c8bbc
fix(#7): jwt 시크릿값 오류로 인해 CI workflow 수정
ParkJiYeoung8297 e029f78
feat(#7): CD 워크플로우 추가
ParkJiYeoung8297 5358d19
chore: 사용하지 않는 import 제거
ParkJiYeoung8297 bbe1e4e
fix: application-test.yml 추가 및 DB 설정을 postgres로 변경
ParkJiYeoung8297 d4c1826
fix: jwt 시크릿 키 길이 오류 수정
ParkJiYeoung8297 c943224
fix: test에서 cors 변수 빠진 것 추가
ParkJiYeoung8297 c898929
fix: CI 워크플로우에서 어플리케이션 이름 오류 수정
ParkJiYeoung8297 b285988
fix(#7): DI 적용 피드백 반영
ParkJiYeoung8297 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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| name: CD - PROD | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ main ] | ||
|
|
||
| jobs: | ||
| deploy: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: actions/setup-java@v4 | ||
| with: | ||
| java-version: "21" | ||
| distribution: "temurin" | ||
|
|
||
| - uses: gradle/actions/setup-gradle@v3 | ||
|
|
||
| - name: Grant execute permission | ||
| run: chmod +x gradlew | ||
|
|
||
| - name: Build JAR | ||
| run: ./gradlew build -x test | ||
|
|
||
| - name: Docker Login | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
| password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
|
|
||
| - name: Build & Push | ||
| uses: docker/build-push-action@v5 | ||
| with: | ||
| context: . | ||
| push: true | ||
| tags: | | ||
| ${{ secrets.DOCKERHUB_USERNAME }}/spot-backend:prod | ||
| ${{ secrets.DOCKERHUB_USERNAME }}/spot-backend:${{ github.sha }} | ||
|
|
||
| - name: Deploy | ||
| uses: appleboy/ssh-action@v1.0.3 | ||
| with: | ||
| host: ${{ secrets.PROD_SERVER_IP }} | ||
| username: ubuntu | ||
| key: ${{ secrets.PROD_SERVER_SSH_KEY }} | ||
| script: | | ||
| cd /home/ubuntu/spot | ||
| git pull origin main | ||
| docker compose pull | ||
| docker compose up -d |
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
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
76 changes: 76 additions & 0 deletions
76
src/main/java/com/example/RealMatch/global/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,76 @@ | ||
| package com.example.RealMatch.global.config; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| 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.builders.HttpSecurity; | ||
| import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
| import org.springframework.security.web.SecurityFilterChain; | ||
| import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
| import org.springframework.web.cors.CorsConfiguration; | ||
| import org.springframework.web.cors.CorsConfigurationSource; | ||
| import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
|
|
||
| import com.example.RealMatch.global.config.jwt.JwtAuthenticationFilter; | ||
| import com.example.RealMatch.global.presentation.advice.CustomAccessDeniedHandler; | ||
| import com.example.RealMatch.global.presentation.advice.CustomAuthEntryPoint; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Configuration | ||
| @EnableWebSecurity | ||
| @RequiredArgsConstructor | ||
| public class SecurityConfig { | ||
| private final JwtAuthenticationFilter jwtAuthenticationFilter; | ||
|
|
||
| private static final String[] PERMIT_ALL_URL_ARRAY = { | ||
| "/api/v1/test", | ||
| "/v3/api-docs/**", "/swagger-ui/**", "/swagger-resources/**", "/swagger-ui.html" | ||
| }; | ||
|
|
||
| private static final String[] REQUEST_AUTHENTICATED_ARRAY = { | ||
| "/api/v1/test-auth" | ||
| }; | ||
|
|
||
| @Value("${cors.allowed-origin}") | ||
| private String allowedOrigin; | ||
| @Value("${swagger.server-url}") | ||
| String swaggerUrl; | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain securityFilterChain(HttpSecurity http, CustomAuthEntryPoint customAuthEntryPoint, CustomAccessDeniedHandler customAccessDeniedHandler) throws Exception { | ||
| http | ||
| .cors(cors -> cors.configurationSource(corsConfigurationSource())) | ||
| .csrf(csrf -> csrf.disable()) | ||
|
|
||
| .exceptionHandling(exception -> exception | ||
| .authenticationEntryPoint(customAuthEntryPoint) // 401 | ||
| .accessDeniedHandler(customAccessDeniedHandler) // 403 | ||
| ) | ||
|
|
||
| .authorizeHttpRequests(auth -> auth | ||
| .requestMatchers(REQUEST_AUTHENTICATED_ARRAY).authenticated() | ||
| .requestMatchers(PERMIT_ALL_URL_ARRAY).permitAll() | ||
| .anyRequest().denyAll() | ||
| ) | ||
| .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); | ||
| return http.build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public CorsConfigurationSource corsConfigurationSource() { | ||
|
|
||
| CorsConfiguration configuration = new CorsConfiguration(); | ||
| configuration.setAllowedOrigins(List.of(allowedOrigin, "http://localhost:8080", swaggerUrl)); | ||
| configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")); | ||
| configuration.setAllowedHeaders(List.of("*")); | ||
| configuration.setAllowCredentials(true); // 쿠키/인증정보 포함 요청 | ||
|
|
||
| UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
| source.registerCorsConfiguration("/**", configuration); | ||
| return source; | ||
| } | ||
| } | ||
|
|
||
52 changes: 52 additions & 0 deletions
52
src/main/java/com/example/RealMatch/global/config/SwaggerConfig.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,52 @@ | ||
| package com.example.RealMatch.global.config; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import io.swagger.v3.oas.models.Components; | ||
| import io.swagger.v3.oas.models.OpenAPI; | ||
| import io.swagger.v3.oas.models.info.Info; | ||
| import io.swagger.v3.oas.models.security.SecurityRequirement; | ||
| import io.swagger.v3.oas.models.security.SecurityScheme; | ||
| import io.swagger.v3.oas.models.servers.Server; | ||
|
|
||
| @Configuration | ||
| public class SwaggerConfig { | ||
|
|
||
| @Value("${swagger.server-url}") | ||
| private String swaggerUrl; | ||
|
|
||
| @Bean | ||
| public OpenAPI localOpenAPI() { | ||
| Info info = new Info() | ||
| .title("🔗 RealMatch API") | ||
| .version("1.0.0") | ||
| .description("RealMatch API 명세서입니다."); | ||
|
|
||
| String jwtSchemeName = "JWT Authentication"; | ||
|
|
||
| io.swagger.v3.oas.models.security.SecurityScheme securityScheme = new io.swagger.v3.oas.models.security.SecurityScheme() | ||
| .name("Authorization") | ||
| .type(SecurityScheme.Type.HTTP) | ||
| .scheme("bearer") | ||
| .bearerFormat("JWT"); | ||
|
|
||
| SecurityRequirement securityRequirement = new SecurityRequirement().addList(jwtSchemeName); | ||
|
|
||
| Components components = new Components() | ||
| .addSecuritySchemes(jwtSchemeName, securityScheme); | ||
|
|
||
| return new OpenAPI() | ||
| .info(info) | ||
| .addSecurityItem(securityRequirement) | ||
| .components(components) | ||
| .servers(List.of( | ||
| new Server() | ||
| .url(swaggerUrl) | ||
| )); | ||
| } | ||
|
|
||
| } |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/example/RealMatch/global/config/jwt/CustomUserDetails.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,58 @@ | ||
| package com.example.RealMatch.global.config.jwt; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.List; | ||
|
|
||
| import org.springframework.security.core.GrantedAuthority; | ||
| import org.springframework.security.core.userdetails.UserDetails; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class CustomUserDetails implements UserDetails { | ||
|
|
||
| private final Long userId; // DB PK | ||
| private final String providerId; // 소셜 고유 ID | ||
| private final String role; // USER / ADMIN | ||
|
|
||
| public CustomUserDetails(Long userId, String providerId, String role) { | ||
| this.userId = userId; | ||
| this.providerId = providerId; | ||
| this.role = role; | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<? extends GrantedAuthority> getAuthorities() { | ||
| return List.of(() -> "ROLE_" + role); | ||
| } | ||
|
|
||
| @Override | ||
| public String getPassword() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public String getUsername() { | ||
| return providerId; | ||
| } // 소셜 UUID 기준 | ||
|
|
||
| @Override | ||
| public boolean isAccountNonExpired() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isAccountNonLocked() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isCredentialsNonExpired() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled() { | ||
| return true; | ||
| } | ||
| } |
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.