Skip to content

Commit

Permalink
Merge pull request #55 from DKU-Dgaja/feat(#53)-Swagger-setting
Browse files Browse the repository at this point in the history
Feat(#53) swagger setting
  • Loading branch information
rndudals authored Jan 21, 2024
2 parents f622a28 + 3db9452 commit 5225b10
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
Expand All @@ -12,6 +13,9 @@
@Configuration
public class ProjectConfig implements WebMvcConfigurer {

@Value("${spring.profiles.active}")
private String activeProfile;

// 암호 인코더 정의 - bcrypt 해싱 알고리즘 사용
@Bean
public BCryptPasswordEncoder passwordEncoder() {
Expand All @@ -26,14 +30,24 @@ public ObjectMapper objectMapper() {
// 객체의 속성 이름을 snake-case로 설정
.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
}

// Cors 모두 오픈 (개발환경)
@Override
public void addCorsMappings(CorsRegistry registry) {
if (activeProfile.equals("prod")) {
prodProfileCorsMapping(registry);
} else {
devProfileCorsMapping(registry);
}
}
// Cors 모두 오픈 (개발환경)
public void devProfileCorsMapping(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST")
.allowedHeaders("*")
.allowCredentials(true);
}
// 프로덕션 환경에서는 Cors 설정을 Front 페이지와 허용할 서버만 등록
private void prodProfileCorsMapping(CorsRegistry registry) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
authorizeHttpRequest
// UnAuth Area
.requestMatchers("/auth/**").permitAll()
// Swagger 추가
.requestMatchers("/v3/api-docs/**", "/swagger-ui/**").permitAll()
// Others
.anyRequest().hasAnyAuthority("USER", "ADMIN")
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.backend.auth.config.swagger;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
public class SwaggerConfig {

@Bean
public OpenAPI openAPI(@Value("${gitudyCoreUrls}") String[] serverList) {
Info info = new Info()
.title("Gitudy Server")
.version("0.1")
.description("깃터디 서버 API 문서입니다.")
.contact(new Contact()
.name("📍 깃터디 GitHub Link")
.url("https://github.com/DKU-Dgaja/gitudy"))
.license(new License()
.name("⚖️ Apache License Version 2.0")
.url("http://www.apache.org/licenses/LICENSE-2.0"));

List<Server> servers = Arrays.stream(serverList)
.map((url) -> new Server().url(url))
.collect(Collectors.toList());

SecurityScheme securityScheme = new SecurityScheme()
.name("Bearer Authentication")
.type(SecurityScheme.Type.HTTP)
.bearerFormat("JWT")
.scheme("Bearer");
SecurityRequirement schemaRequirement = new SecurityRequirement().addList("bearerAuth");

return new OpenAPI()
.components(new Components().addSecuritySchemes("bearerAuth", securityScheme))
.security(Arrays.asList(schemaRequirement))
.info(info)
.servers(servers);
}
}

0 comments on commit 5225b10

Please sign in to comment.