Skip to content

Commit

Permalink
chore: db, swagger 설정, Dockerfile 수정
Browse files Browse the repository at this point in the history
chore: db, swagger 설정, Dockerfile 수정
  • Loading branch information
yslle authored Sep 27, 2024
2 parents b6f921d + a46ad3b commit f681b3d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FROM openjdk:11-jdk
ADD /build/libs/*.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
ARG JAR_FILE=./build/libs/*-SNAPSHOT.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT [ "java", "-jar", "/app.jar" ]
6 changes: 2 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-websocket'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
// runtimeOnly 'com.h2database:h2'
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

// Swagger
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
implementation 'io.springfox:springfox-boot-starter:3.0.0'

// Redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
import com.web.stard.global.config.jwt.JwtTokenProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
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.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@RequiredArgsConstructor
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Configuration
public class WebSecurityConfig {

private final JwtTokenProvider jwtTokenProvider;
private final RedisTemplate redisTemplate;
Expand All @@ -33,8 +35,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


/* swagger v3 */
// "/v3/api-docs/**",
// "/swagger-ui/**"
"/v3/api-docs/**",
"/swagger-ui/**",

"/user/auth/**",

Expand Down Expand Up @@ -78,8 +80,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
"/notifications/**"
};

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.cors().and()
.httpBasic().disable()
Expand All @@ -101,10 +103,11 @@ protected void configure(HttpSecurity httpSecurity) throws Exception {
// .anyRequest().authenticated() //TODO 주석 제거

.and()
// JwtAuthenticationFilter를 UsernamePasswordAuthentictaionFilter 전에 적용시킨다.
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider, redisTemplate), UsernamePasswordAuthenticationFilter.class);

// JwtAuthenticationFilter를 UsernamePasswordAuthenticationFilter 전에 적용
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider, redisTemplate), UsernamePasswordAuthenticationFilter.class);

return httpSecurity.build();
}

// 암호화에 필요한 PasswordEncoder Bean 등록
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket restAPI() {
return new Docket(DocumentationType.SWAGGER_2)
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.web.stard"))
.paths(PathSelectors.any())
.build();
.build()
.securitySchemes(List.of(apiKey()))
.securityContexts(List.of(securityContext()));
}

private ApiInfo apiInfo() {
Expand All @@ -31,4 +37,24 @@ private ApiInfo apiInfo() {
.description("Web swagger api")
.build();
}

// JWT SecurityContext 구성
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.build();
}

private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return List.of(new SecurityReference("Authorization", authorizationScopes));
}

// ApiKey 정의
private ApiKey apiKey() {
return new ApiKey("Authorization", "Authorization", "header");
}

}

0 comments on commit f681b3d

Please sign in to comment.