Skip to content

Commit

Permalink
[🛠️ Refactor] RunnerLock 필드 변수명 변경 �+ Security Uri 상수화 + H2 웹 콘솔용 Web…
Browse files Browse the repository at this point in the history
…SecurityCustomizer 제거 (#226)

* refactor: RunnerLock 내부 필드 변수명 변경

* refactor: security permitall uri 상수화
  • Loading branch information
egg528 authored Mar 15, 2024
1 parent afc3176 commit fbfbdec
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ import io.raemian.api.auth.domain.CurrentUser
import io.raemian.api.auth.service.OAuth2UserService
import io.raemian.api.support.StateOAuth2AuthorizationRequestRepository
import io.raemian.api.support.TokenProvider
import io.raemian.api.support.constant.WebSecurityConstant
import jakarta.servlet.http.HttpServletResponse
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.autoconfigure.security.servlet.PathRequest
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.security.config.annotation.SecurityConfigurerAdapter
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.WebSecurityCustomizer
import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
Expand All @@ -26,7 +24,6 @@ import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCo
import org.springframework.security.web.DefaultSecurityFilterChain
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
import org.springframework.security.web.util.matcher.AntPathRequestMatcher
import org.springframework.web.filter.CorsFilter
import java.nio.charset.StandardCharsets

Expand All @@ -52,35 +49,20 @@ class WebSecurityConfig(
.httpBasic { it.disable() }
.addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter::class.java)
.exceptionHandling {
it
.authenticationEntryPoint { request, response, authException ->
// 유효한 자격증명을 제공하지 않고 접근하려 할때 401
response.sendError(HttpServletResponse.SC_UNAUTHORIZED)
}
it.authenticationEntryPoint { request, response, authException ->
// 유효한 자격증명을 제공하지 않고 접근하려 할때 401
response.sendError(HttpServletResponse.SC_UNAUTHORIZED)
}
.accessDeniedHandler { request, response, accessDeniedException ->
// 필요한 권한이 없이 접근하려 할때 403
response.sendError(HttpServletResponse.SC_FORBIDDEN)
}
}
.authorizeHttpRequests {
it.requestMatchers(AntPathRequestMatcher("/auth/**")).permitAll()
.requestMatchers(AntPathRequestMatcher("/oauth2/**")).permitAll()
.requestMatchers(AntPathRequestMatcher("/login/**")).permitAll()
.requestMatchers(AntPathRequestMatcher("/one-baily-actuator/**")).permitAll()
.requestMatchers(AntPathRequestMatcher("/log/**")).permitAll()
.requestMatchers(AntPathRequestMatcher("/open/life-map/**")).permitAll()
.requestMatchers(AntPathRequestMatcher("/goal/explore")).permitAll()
.requestMatchers(
AntPathRequestMatcher("/swagger*/**"),
AntPathRequestMatcher("/v3/api-docs/**"),
AntPathRequestMatcher("/swagger-resources/**"),
AntPathRequestMatcher("/webjars/**"),
).permitAll()
.requestMatchers(
AntPathRequestMatcher("/cheering/squad/**"),
AntPathRequestMatcher("/cheering/count/**"),
).permitAll()
.anyRequest().authenticated()
it.requestMatchers(*WebSecurityConstant.PUBLIC_URIS)
.permitAll()
.anyRequest()
.authenticated()
}
.oauth2Login {
it.tokenEndpoint { it.accessTokenResponseClient(accessTokenResponseClient()) }
Expand Down Expand Up @@ -110,17 +92,6 @@ class WebSecurityConfig(
return http.build()
}

@Bean
@ConditionalOnProperty(name = ["spring.h2.console.enabled"], havingValue = "true")
fun configureH2ConsoleEnable(): WebSecurityCustomizer {
return WebSecurityCustomizer {
it
.ignoring()
.requestMatchers(PathRequest.toH2Console())
.requestMatchers(AntPathRequestMatcher("/favicon.ico", "**/favicon.ico"))
}
}

@Bean
fun getPasswordEncoder(): PasswordEncoder {
return BCryptPasswordEncoder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.locks.ReentrantLock

class RunnerLock : ReentrantLock() {
private val watingThread = AtomicInteger(0)
private val watingThreadCount = AtomicInteger(0)

fun increase(): Int {
return watingThread.addAndGet(1)
return watingThreadCount.addAndGet(1)
}

fun decrease(): Int {
return watingThread.decrementAndGet()
return watingThreadCount.decrementAndGet()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.raemian.api.support.constant

object WebSecurityConstant {
val PUBLIC_URIS = arrayOf(
"/auth/**",
"/oauth2/**",
"/login/**",
"/one-baily-actuator/**",
"/log/**",
"/open/life-map/**",
"/cheering/squad/**",
"/cheering/count/**",
// for swagger
"/swagger*/**",
"/v3/api-docs/**",
"/swagger-resources/**",
"/webjars/**",
)
}

0 comments on commit fbfbdec

Please sign in to comment.