-
Notifications
You must be signed in to change notification settings - Fork 0
feat: swagger 추가 #12
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: swagger 추가 #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package run.backend.global.config; | ||
|
|
||
| import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
| import io.swagger.v3.oas.annotations.info.Info; | ||
| import io.swagger.v3.oas.models.security.SecurityRequirement; | ||
| import io.swagger.v3.oas.models.servers.Server; | ||
| import io.swagger.v3.oas.models.Components; | ||
| import io.swagger.v3.oas.models.OpenAPI; | ||
| import io.swagger.v3.oas.models.security.SecurityScheme; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import java.util.List; | ||
|
|
||
|
|
||
| @Configuration | ||
| @OpenAPIDefinition( | ||
| info = @Info( | ||
| title = "Runners-Fight API", | ||
| description = "Runners-Fight 서비스의 API 명세서입니다.", | ||
| version = "v1" | ||
| ) | ||
| ) | ||
| public class SwaggerConfig { | ||
|
|
||
| @Value("${swagger.server.url.prod}") | ||
| private String prodUrl; | ||
|
|
||
| @Bean | ||
| public OpenAPI openAPI() { | ||
| String jwt = "JWT"; | ||
| SecurityRequirement securityRequirement = new SecurityRequirement().addList(jwt); | ||
| Components components = new Components().addSecuritySchemes(jwt, new SecurityScheme() | ||
| .name(jwt) | ||
| .type(SecurityScheme.Type.HTTP) | ||
| .scheme("bearer") | ||
| .bearerFormat("jwt") | ||
| ); | ||
|
|
||
| return new OpenAPI() | ||
| .addSecurityItem(securityRequirement) | ||
| .components(components) | ||
| .servers(List.of( | ||
| new Server().url("http://localhost:8080").description("local server"), | ||
| new Server().url(prodUrl).description("production server") | ||
| )); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,22 @@ | |
|
|
||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.security.config.Customizer; | ||
| import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
| import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
| import org.springframework.security.config.http.SessionCreationPolicy; | ||
| import org.springframework.security.core.userdetails.User; | ||
| import org.springframework.security.core.userdetails.UserDetails; | ||
| import org.springframework.security.core.userdetails.UserDetailsService; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
| import org.springframework.security.provisioning.InMemoryUserDetailsManager; | ||
| import org.springframework.security.web.SecurityFilterChain; | ||
| import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
| import org.springframework.web.cors.CorsConfiguration; | ||
|
|
@@ -22,24 +31,74 @@ | |
| @RequiredArgsConstructor | ||
| public class SecurityConfig { | ||
|
|
||
| @Value("${swagger.id}") | ||
| private String swaggerUser; | ||
|
|
||
| @Value("${swagger.pwd}") | ||
| private String swaggerPassword; | ||
|
|
||
| private final JwtTokenProvider jwtTokenProvider; | ||
|
|
||
| private final String[] SwaggerPatterns = { | ||
| "/v3/api-docs/**", | ||
| "/swagger-ui/**", | ||
| "/swagger-ui.html", | ||
| "/swagger" | ||
| }; | ||
|
|
||
| private final String[] PermitAllPatterns = { | ||
| "/api/v1/auth/**" | ||
| }; | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
| http.csrf(AbstractHttpConfigurer::disable).httpBasic(AbstractHttpConfigurer::disable) | ||
| .formLogin(AbstractHttpConfigurer::disable); | ||
| public UserDetailsService userDetailsService() { | ||
| UserDetails user = | ||
| User.withUsername(swaggerUser) | ||
| .password(passwordEncoder().encode(swaggerPassword)) | ||
| .roles("SWAGGER") | ||
| .build(); | ||
| return new InMemoryUserDetailsManager(user); | ||
| } | ||
|
|
||
| @Bean | ||
| public PasswordEncoder passwordEncoder() { | ||
| return new BCryptPasswordEncoder(8); | ||
| } | ||
|
|
||
| @Bean | ||
| @Order(1) | ||
| public SecurityFilterChain swaggerSecurityFilterChain(HttpSecurity http) throws Exception { | ||
| http | ||
| .securityMatcher(SwaggerPatterns) | ||
| .authorizeHttpRequests(authorize -> authorize | ||
| .anyRequest().hasRole("SWAGGER") | ||
| ) | ||
| .httpBasic(Customizer.withDefaults()) | ||
| .csrf(AbstractHttpConfigurer::disable); | ||
|
|
||
| return http.build(); | ||
| } | ||
|
|
||
| http.sessionManagement( | ||
| session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)); | ||
| @Bean | ||
| @Order(2) | ||
| public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
| http | ||
| .securityMatcher(PermitAllPatterns) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기에 permitAllPatterns가 들어가면 저기에만 시큐리티 적용 아닌가요??
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. securityMatcher에 있는 uri에 대해서 해당 필터 체인을 적용하겠다는 의미로 적었습니다!!! |
||
| .csrf(AbstractHttpConfigurer::disable) | ||
| .httpBasic(AbstractHttpConfigurer::disable) | ||
| .formLogin(AbstractHttpConfigurer::disable); | ||
|
|
||
| http.authorizeHttpRequests( | ||
| authorize -> authorize.requestMatchers("/api/v1/auth/**").permitAll().anyRequest() | ||
| .authenticated()); | ||
| http | ||
| .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
| .cors(cors -> cors.configurationSource(corsConfigurationSource())); | ||
|
|
||
| http.cors(cors -> cors.configurationSource(corsConfigurationSource())); | ||
| http | ||
| .authorizeHttpRequests(authorize -> authorize | ||
| .requestMatchers(PermitAllPatterns).permitAll() | ||
| .anyRequest().authenticated()); | ||
|
|
||
| http.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), | ||
| UsernamePasswordAuthenticationFilter.class); | ||
| http | ||
| .addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class); | ||
|
|
||
| return http.build(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이런건 어떻게 아셨어요..? 시큐리티 고수,,,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ㅋㅋㅋ api 외부로 노출되면 안 된다고 해서..