From 0081c1dd036d3df63f441971a43a82b9d17d00a6 Mon Sep 17 00:00:00 2001 From: yeseul106 <20191037@sungshin.ac.kr> Date: Thu, 25 Jan 2024 01:23:36 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]=20#122=20user=20repository=EC=97=90=20?= =?UTF-8?q?=EB=88=84=EB=9D=BD=EB=90=9C=20=EC=A1=B0=ED=9A=8C=20=EC=BF=BC?= =?UTF-8?q?=EB=A6=AC=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/common/config/SecurityConfig.java | 131 +++++++++--------- .../crew/main/entity/user/UserRepository.java | 8 ++ 2 files changed, 75 insertions(+), 64 deletions(-) diff --git a/main/src/main/java/org/sopt/makers/crew/main/common/config/SecurityConfig.java b/main/src/main/java/org/sopt/makers/crew/main/common/config/SecurityConfig.java index cf482cc0..e0a4cfdb 100644 --- a/main/src/main/java/org/sopt/makers/crew/main/common/config/SecurityConfig.java +++ b/main/src/main/java/org/sopt/makers/crew/main/common/config/SecurityConfig.java @@ -23,75 +23,78 @@ @EnableWebSecurity public class SecurityConfig { - private final JwtTokenProvider jwtTokenProvider; - private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint; + private final JwtTokenProvider jwtTokenProvider; + private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint; - private static final String[] SWAGGER_URL = { - "/swagger-resources/**", - "/favicon.ico", - "/api-docs/**", - "/swagger-ui/**", - "/swagger-ui.html", - "/swagger-ui/index.html", - "/docs/swagger-ui/index.html", - "/swagger-ui/swagger-ui.css", - }; + private static final String[] SWAGGER_URL = { + "/swagger-resources/**", + "/favicon.ico", + "/api-docs/**", + "/swagger-ui/**", + "/swagger-ui.html", + "/swagger-ui/index.html", + "/docs/swagger-ui/index.html", + "/swagger-ui/swagger-ui.css", + }; - private static final String[] AUTH_WHITELIST = { - "/health" - }; + private static final String[] AUTH_WHITELIST = { + "/health", + "meeting/v2/org-user/**" + }; - @Bean - @Profile("dev") - SecurityFilterChain devSecurityFilterChain(HttpSecurity http) throws Exception { - http.csrf((csrfConfig) -> csrfConfig.disable()) - .cors(Customizer.withDefaults()) - .sessionManagement( - (sessionManagement) -> sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) - .authorizeHttpRequests( - authorize -> authorize.requestMatchers(AUTH_WHITELIST).permitAll() - .requestMatchers(SWAGGER_URL).permitAll() - .anyRequest().authenticated()) - .addFilterBefore( - new JwtAuthenticationFilter(this.jwtTokenProvider, this.jwtAuthenticationEntryPoint), - UsernamePasswordAuthenticationFilter.class) - .exceptionHandling(exceptionHandling -> exceptionHandling - .authenticationEntryPoint(this.jwtAuthenticationEntryPoint)); - return http.build(); - } + @Bean + @Profile("dev") + SecurityFilterChain devSecurityFilterChain(HttpSecurity http) throws Exception { + http.csrf((csrfConfig) -> csrfConfig.disable()) + .cors(Customizer.withDefaults()) + .sessionManagement( + (sessionManagement) -> sessionManagement.sessionCreationPolicy( + SessionCreationPolicy.STATELESS)) + .authorizeHttpRequests( + authorize -> authorize.requestMatchers(AUTH_WHITELIST).permitAll() + .requestMatchers(SWAGGER_URL).permitAll() + .anyRequest().authenticated()) + .addFilterBefore( + new JwtAuthenticationFilter(this.jwtTokenProvider, this.jwtAuthenticationEntryPoint), + UsernamePasswordAuthenticationFilter.class) + .exceptionHandling(exceptionHandling -> exceptionHandling + .authenticationEntryPoint(this.jwtAuthenticationEntryPoint)); + return http.build(); + } - @Bean - @Profile("prod") - SecurityFilterChain prodSecurityFilterChain(HttpSecurity http) throws Exception { - http.csrf((csrfConfig) -> csrfConfig.disable()) - .cors(Customizer.withDefaults()) - .sessionManagement( - (sessionManagement) -> sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) - .authorizeHttpRequests( - authorize -> authorize.requestMatchers(AUTH_WHITELIST).permitAll() - .requestMatchers(SWAGGER_URL).permitAll() - .anyRequest().authenticated()) - .addFilterBefore( - new JwtAuthenticationFilter(this.jwtTokenProvider, this.jwtAuthenticationEntryPoint), - UsernamePasswordAuthenticationFilter.class) - .exceptionHandling(exceptionHandling -> exceptionHandling - .authenticationEntryPoint(this.jwtAuthenticationEntryPoint)); - return http.build(); - } + @Bean + @Profile("prod") + SecurityFilterChain prodSecurityFilterChain(HttpSecurity http) throws Exception { + http.csrf((csrfConfig) -> csrfConfig.disable()) + .cors(Customizer.withDefaults()) + .sessionManagement( + (sessionManagement) -> sessionManagement.sessionCreationPolicy( + SessionCreationPolicy.STATELESS)) + .authorizeHttpRequests( + authorize -> authorize.requestMatchers(AUTH_WHITELIST).permitAll() + .requestMatchers(SWAGGER_URL).permitAll() + .anyRequest().authenticated()) + .addFilterBefore( + new JwtAuthenticationFilter(this.jwtTokenProvider, this.jwtAuthenticationEntryPoint), + UsernamePasswordAuthenticationFilter.class) + .exceptionHandling(exceptionHandling -> exceptionHandling + .authenticationEntryPoint(this.jwtAuthenticationEntryPoint)); + return http.build(); + } - @Bean - CorsConfigurationSource corsConfigurationSource() { - CorsConfiguration configuration = new CorsConfiguration(); - configuration.setAllowedOrigins( - Arrays.asList("https://playground.sopt.org/", "http://localhost:3000/", - "https://sopt-internal-dev.pages.dev/")); - configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PATCH", "DELETE", "OPTIONS")); - configuration.addAllowedHeader("*"); - configuration.setAllowCredentials(false); + @Bean + CorsConfigurationSource corsConfigurationSource() { + CorsConfiguration configuration = new CorsConfiguration(); + configuration.setAllowedOrigins( + Arrays.asList("https://playground.sopt.org/", "http://localhost:3000/", + "https://sopt-internal-dev.pages.dev/")); + configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PATCH", "DELETE", "OPTIONS")); + configuration.addAllowedHeader("*"); + configuration.setAllowCredentials(false); - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); - source.registerCorsConfiguration("/**", configuration); - return source; - } + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", configuration); + return source; + } } diff --git a/main/src/main/java/org/sopt/makers/crew/main/entity/user/UserRepository.java b/main/src/main/java/org/sopt/makers/crew/main/entity/user/UserRepository.java index 01c48e40..90c4e8bc 100644 --- a/main/src/main/java/org/sopt/makers/crew/main/entity/user/UserRepository.java +++ b/main/src/main/java/org/sopt/makers/crew/main/entity/user/UserRepository.java @@ -1,12 +1,20 @@ package org.sopt.makers.crew.main.entity.user; +import java.util.Optional; import org.sopt.makers.crew.main.common.exception.UnAuthorizedException; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository { + Optional findByOrgId(Integer orgId); + default User findByIdOrThrow(Integer userId) { return findById(userId) .orElseThrow(() -> new UnAuthorizedException()); } + + default User findByOrgIdOrThrow(Integer orgUserId) { + return findByOrgId(orgUserId) + .orElseThrow(() -> new UnAuthorizedException()); + } }