Skip to content

Commit 5225b10

Browse files
authored
Merge pull request #55 from DKU-Dgaja/feat(#53)-Swagger-setting
Feat(#53) swagger setting
2 parents f622a28 + 3db9452 commit 5225b10

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

backend/src/main/java/com/example/backend/auth/config/ProjectConfig.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
5+
import org.springframework.beans.factory.annotation.Value;
56
import org.springframework.context.annotation.Bean;
67
import org.springframework.context.annotation.Configuration;
78
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@@ -12,6 +13,9 @@
1213
@Configuration
1314
public class ProjectConfig implements WebMvcConfigurer {
1415

16+
@Value("${spring.profiles.active}")
17+
private String activeProfile;
18+
1519
// 암호 인코더 정의 - bcrypt 해싱 알고리즘 사용
1620
@Bean
1721
public BCryptPasswordEncoder passwordEncoder() {
@@ -26,14 +30,24 @@ public ObjectMapper objectMapper() {
2630
// 객체의 속성 이름을 snake-case로 설정
2731
.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
2832
}
29-
30-
// Cors 모두 오픈 (개발환경)
3133
@Override
3234
public void addCorsMappings(CorsRegistry registry) {
35+
if (activeProfile.equals("prod")) {
36+
prodProfileCorsMapping(registry);
37+
} else {
38+
devProfileCorsMapping(registry);
39+
}
40+
}
41+
// Cors 모두 오픈 (개발환경)
42+
public void devProfileCorsMapping(CorsRegistry registry) {
3343
registry.addMapping("/**")
3444
.allowedOriginPatterns("*")
3545
.allowedMethods("GET", "POST")
3646
.allowedHeaders("*")
3747
.allowCredentials(true);
3848
}
49+
// 프로덕션 환경에서는 Cors 설정을 Front 페이지와 허용할 서버만 등록
50+
private void prodProfileCorsMapping(CorsRegistry registry) {
51+
52+
}
3953
}

backend/src/main/java/com/example/backend/auth/config/security/SecurityConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
2525
authorizeHttpRequest
2626
// UnAuth Area
2727
.requestMatchers("/auth/**").permitAll()
28+
// Swagger 추가
29+
.requestMatchers("/v3/api-docs/**", "/swagger-ui/**").permitAll()
2830
// Others
2931
.anyRequest().hasAnyAuthority("USER", "ADMIN")
3032
)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.backend.auth.config.swagger;
2+
3+
import io.swagger.v3.oas.models.Components;
4+
import io.swagger.v3.oas.models.OpenAPI;
5+
import io.swagger.v3.oas.models.info.Contact;
6+
import io.swagger.v3.oas.models.info.Info;
7+
import io.swagger.v3.oas.models.info.License;
8+
import io.swagger.v3.oas.models.security.SecurityRequirement;
9+
import io.swagger.v3.oas.models.security.SecurityScheme;
10+
import io.swagger.v3.oas.models.servers.Server;
11+
import org.springframework.beans.factory.annotation.Value;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
15+
import java.util.Arrays;
16+
import java.util.List;
17+
import java.util.stream.Collectors;
18+
19+
@Configuration
20+
public class SwaggerConfig {
21+
22+
@Bean
23+
public OpenAPI openAPI(@Value("${gitudyCoreUrls}") String[] serverList) {
24+
Info info = new Info()
25+
.title("Gitudy Server")
26+
.version("0.1")
27+
.description("깃터디 서버 API 문서입니다.")
28+
.contact(new Contact()
29+
.name("📍 깃터디 GitHub Link")
30+
.url("https://github.com/DKU-Dgaja/gitudy"))
31+
.license(new License()
32+
.name("⚖️ Apache License Version 2.0")
33+
.url("http://www.apache.org/licenses/LICENSE-2.0"));
34+
35+
List<Server> servers = Arrays.stream(serverList)
36+
.map((url) -> new Server().url(url))
37+
.collect(Collectors.toList());
38+
39+
SecurityScheme securityScheme = new SecurityScheme()
40+
.name("Bearer Authentication")
41+
.type(SecurityScheme.Type.HTTP)
42+
.bearerFormat("JWT")
43+
.scheme("Bearer");
44+
SecurityRequirement schemaRequirement = new SecurityRequirement().addList("bearerAuth");
45+
46+
return new OpenAPI()
47+
.components(new Components().addSecuritySchemes("bearerAuth", securityScheme))
48+
.security(Arrays.asList(schemaRequirement))
49+
.info(info)
50+
.servers(servers);
51+
}
52+
}

0 commit comments

Comments
 (0)