Skip to content

Commit

Permalink
Merge pull request #57 from Mojacknong/feature_56/스웨거
Browse files Browse the repository at this point in the history
Feature 56/스웨거
  • Loading branch information
MinchoGreenT authored Aug 1, 2024
2 parents 2419930 + d4d7645 commit 78fa546
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 70 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'

// 스웨거
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
// implementation 'io.springfox:springfox-boot-starter:3.0.0'
// implementation 'io.springfox:springfox-swagger-ui:3.0.0'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'

// S3
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Diary;
import com.modernfarmer.farmusspring.domain.myveggiegarden.entity.Routine;
import io.swagger.models.auth.In;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
import java.util.Arrays;
import java.util.List;

import static org.springframework.security.config.Customizer.withDefaults;


Expand All @@ -27,60 +29,64 @@ public class SecurityConfig {
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("https://locahost:8080"));
configuration.setAllowedOrigins(List.of("http://locahost:8080"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector);
http
.formLogin(formLogin -> formLogin.disable())
.httpBasic(httpBasic -> httpBasic.disable())
.csrf(csrf -> csrf.disable())
.cors(withDefaults())
.headers(headers -> headers.frameOptions(frameOptions -> frameOptions.disable()))
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(request -> request
.requestMatchers(
"/api/auth/logout",
"/api/auth/reissue-token",
"/api/user/on-boarding/motivation",
"/api/user/on-boarding/level",
"/api/user/on-boarding/complete",
"/api/user",
"/api/user/profile-image",
"/api/user/profile",
"/api/veggie-info",
"/api/veggie-info/**",
"api/my-veggie",
"api/my-veggie/diary",
"/api/my-veggie/simple-list",
"/api/my-veggie/list"
).permitAll()
public WebSecurityCustomizer configure() {
return (web -> web.ignoring()
// http://localhost:8080/swagger-ui/index.html
.requestMatchers(
"/swagger-ui/**",
"/v3/api-docs/**",
"/swagger-resources/**",
"/webjars/**"
)
.authorizeHttpRequests(request -> request.anyRequest().authenticated());

return http.build();


}
@Bean
public WebSecurityCustomizer configure() {
return (web -> web.ignoring()

.requestMatchers("/api/auth/kakao-login")
.requestMatchers("/api/auth/kakao-login")
.requestMatchers("/api/auth/google-login")
.requestMatchers("/api/my-veggie/routine")
.requestMatchers("/api/my-veggie/diary/{myVeggieId}/one")
.requestMatchers("/api/my-veggie/diary/check")
.requestMatchers("/api/my-veggie/{myVeggieId}/profile")
);
}
);
}


@Bean
public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
http
.addFilterAfter(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class)
.authorizeHttpRequests(request -> request
.requestMatchers(
"/api/auth/logout",
"/api/auth/reissue-token",
"/api/user/on-boarding/motivation",
"/api/user/on-boarding/level",
"/api/user/on-boarding/complete",
"/api/user",
"/api/user/profile-image",
"/api/user/profile",
"/api/veggie-info",
"/api/veggie-info/**",
"api/my-veggie",
"api/my-veggie/diary",
"/api/my-veggie/simple-list",
"/api/my-veggie/list",
"/api/my-veggie/diary/{myVeggieId}/all",
"/api/my-veggie/diary/{myVeggieId}/one"
)
// .authenticated()
// .anyRequest().denyAll()
.permitAll()
)
.authorizeHttpRequests(request -> request.anyRequest().authenticated());
return http.build();
}




}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,58 @@
package com.modernfarmer.farmusspring.global.config;

import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//import springfox.documentation.builders.ApiInfoBuilder;
//import springfox.documentation.builders.PathSelectors;
//import springfox.documentation.builders.RequestHandlerSelectors;
//import springfox.documentation.service.ApiInfo;
//import springfox.documentation.spi.DocumentationType;
//import springfox.documentation.spring.web.plugins.Docket;


import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;


@Configuration
public class SwaggerConfig {
private static final String API_NAME = "Farmus Farm Service API";
private static final String API_VERSION = "0.0.2";
private static final String API_DESCRIPTION = "팜어스 팜 서비스 api 명세서입니다.";
public class SwaggerConfig implements WebMvcConfigurer {

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false) //기본 응답코드 표시
.apiInfo(apiInfo()) //Api 정보
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.farmusfarm.domain")) //적용할 패키지명
.paths(PathSelectors.any()) //패키지 하위에서 적용할 url path 지정
.build();
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(new Info().title("My API").version("1.0"))
.addSecurityItem(new SecurityRequirement().addList("bearerAuth"))
.components(new io.swagger.v3.oas.models.Components()
.addSecuritySchemes("bearerAuth", new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")));
}

public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(API_NAME)
.version(API_VERSION)
.description(API_DESCRIPTION)
.build();
}
// private static final String API_NAME = "Farmus Farm Service API";
// private static final String API_VERSION = "0.0.2";
// private static final String API_DESCRIPTION = "팜어스 팜 서비스 api 명세서입니다.";
//
// @Bean
// public Docket api() {
// return new Docket(DocumentationType.SWAGGER_2)
// .useDefaultResponseMessages(false) //기본 응답코드 표시
// .apiInfo(apiInfo()) //Api 정보
// .select()
// .apis(RequestHandlerSelectors.basePackage("com.modernfarmer.farmusspring")) //적용할 패키지명
// .paths(PathSelectors.any()) //패키지 하위에서 적용할 url path 지정
// .build();
// }
//
// public ApiInfo apiInfo() {
// return new ApiInfoBuilder()
// .title(API_NAME)
// .version(API_VERSION)
// .description(API_DESCRIPTION)
// .build();
// }
}

0 comments on commit 78fa546

Please sign in to comment.