Skip to content

Commit

Permalink
[feat] swagger 비밀번호 설정 #78
Browse files Browse the repository at this point in the history
  • Loading branch information
wjdtkdgns committed Aug 2, 2023
1 parent 20796a1 commit 0642d8e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,43 @@

import static allchive.server.core.consts.AllchiveConst.SwaggerPatterns;

import allchive.server.core.helper.SpringEnvironmentHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
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.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.access.expression.DefaultWebSecurityExpressionHandler;

@RequiredArgsConstructor
@EnableWebSecurity()
public class SecurityConfig {
private final FilterConfig filterConfig;
private final SpringEnvironmentHelper springEnvironmentHelper;

@Value("${swagger.user}")
private String swaggerUser;

@Value("${swagger.password}")
private String swaggerPassword;

@Bean
public InMemoryUserDetailsManager userDetailsService() {
UserDetails user =
User.withUsername(swaggerUser)
.password(passwordEncoder().encode(swaggerPassword))
.roles("SWAGGER")
.build();
return new InMemoryUserDetailsManager(user);
}

@Bean
public PasswordEncoder passwordEncoder() {
Expand All @@ -30,6 +52,10 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests().expressionHandler(expressionHandler());

if (springEnvironmentHelper.isProdAndDevProfile()) {
http.authorizeRequests().mvcMatchers(SwaggerPatterns).authenticated().and().httpBasic();
}

http.authorizeRequests()
.antMatchers(SwaggerPatterns)
.permitAll()
Expand Down
4 changes: 4 additions & 0 deletions Api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ springdoc:
swagger-ui:
tags-sorter: alpha

swagger:
user: ${SWAGGER_USER:user}
password: ${SWAGGER_PASSWORD:password}

---
spring:
config:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ public class SpringEnvironmentHelper {
private final Environment environment;

public Boolean isProdProfile() {
String[] activeProfiles = environment.getActiveProfiles();
List<String> currentProfile = Arrays.stream(activeProfiles).toList();
List<String> currentProfile = getCurrentProfile();
return currentProfile.contains(PROD);
}

public Boolean isDevProfile() {
String[] activeProfiles = environment.getActiveProfiles();
List<String> currentProfile = Arrays.stream(activeProfiles).toList();
List<String> currentProfile = getCurrentProfile();
return currentProfile.contains(DEV);
}

public Boolean isProdAndDevProfile() {
List<String> currentProfile = getCurrentProfile();
return currentProfile.contains(PROD) || currentProfile.contains(DEV);
}

private List<String> getCurrentProfile() {
String[] activeProfiles = environment.getActiveProfiles();
return Arrays.stream(activeProfiles).toList();
}
}

0 comments on commit 0642d8e

Please sign in to comment.