Skip to content
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

Feat(#53) swagger setting #55

Merged
merged 2 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading