πνκ²½
IntelliJ Ultimate
Java 17
Spring boot 3.2.3
Gradle - Groovy
Dependencies:
Spring Web
Thymeleaf
Spring Data JPA
lombok
MariaDB 10.11
Spring Dev tool
Spring Security
πλ¬Έμ
κ΄λ¦¬μ νμ΄μ§λ₯Ό μλ―Ένλ `/member`λ₯Ό μ μΈν λͺ¨λ urlμ μμ²μ νμ©νλ €κ³ νλ€. μ μ 리μμ€μ μμ±λ λ€λ₯Έ urlμ μμ²μ λͺ¨λ νμ©νκΈ° μν΄ λ€μκ³Ό κ°μ΄ μμ±νλ€.
http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(request ->
request.requestMatchers(HttpMethod.POST, "/another/**")
.permitAll()
.requestMatchers(HttpMethod.GET, "/", "/another/**", "/member/login", "/css/**", "/img/**", "/js/**")
.permitAll()
.anyRequest()
.authenticated())
.exceptionHandling(exceptionHandler -> exceptionHandler.accessDeniedHandler(accessDeniedHandler()));
λ¨Όμ ν μ€νΈλ₯Ό μν΄ μμλ‘ csrfλ₯Ό λΉνμ±ννλ€.
λ©μΈ νμ΄μ§μΈ `/`μ `/another`μ νμ νμ΄μ§μ μ κ·Όμ `permitAll()`μ μ¬μ©ν΄ λͺ¨λ νμ©νλ€.
λ‘κ·ΈμΈ νμ΄μ§μ μ κ·Όν μ μλλ‘ `/member/login`λ₯Ό νμ©νλ€.
μ μ 리μμ€ λν μ κ·Ό μ νμ ν΄μ νκΈ° μν΄ `/css/**` `/img/**` `/js/**` μ κ°μ΄ μ€μ νλ€.
μ΄λ `GET` μμ²μΌλ‘ νμ΄μ§μ μ μνμ λ μμ±ν΄λ λͺ¨λ νμ΄μ§μ μ κ·Όμ΄ νμ©λμμΌλ `/another`μμ formμ 보λ΄κΈ° μν΄ `POST` λ°©μμΌλ‘ μμ²ν κ²½μ° access denied μλ¬κ° λ°μνλ€.
λ¬Έμ λ λ€μκ³Ό κ°λ€.
1. μ€μ ν urlμ λν΄ `GET` λ°©μμΌλ‘ μμ²ν κ²½μ° μ κ·Όμ΄ νμ©λμμΌλ `GET` μ΄μΈμ λͺ¨λ μμ² λ°©μμ λν΄ μλ¬κ° λ°μνμμ.
2. κΆνμ΄ μμΌλ©΄ λ€λ‘ ν겨λκ° μ μλλ‘ 403 handler μμ± ν μ€μ ν΄λμμΌλ―λ‘ λ§μ½ 403 μλ¬κ° λ°μνλ€λ©΄ ν΄λΉ handlerμ λ‘μ§μ΄ μ€νλμ΄μΌ νμ§λ§, μμ²μ νμ©νμ§ μμ λ€λ₯Έ urlμ²λΌ λ‘κ·ΈμΈ νμ΄μ§λ‘ νκ²¨μ Έ λμμ.
πν΄κ²°
`permitAll()`μ μμ±νμμλ λ€λ₯Έ `filter`κ° λμνλ λ¬Έμ λ‘ λ³΄κ³ , μ²μλΆν° 무μνκΈ° μν΄ λ€μκ³Ό κ°μ μ€μ μ μΆκ°νλ€.
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web -> web.ignoring()
.requestMatchers(PathRequest.toStaticResources().atCommonLocations())
.requestMatchers("/another/**"));
}
κ·Έλ¬λ ν΄λΉ μ€μ μ΄ λμνμ§ μκ³ κ°μ μ€λ₯κ° μ§μμ μΌλ‘ λ°μνλ€.
`Controller`μ μ΄λ―Έ `@PreAuthorize("hasRole('MEMBER')")`λ‘ κΆνμ κ²μ¦νκ³ μμΌλ―λ‘, κΆνμ΄ νμν νμ΄μ§λ§ μΈμ¦μ μꡬνκ³ λ€λ₯Έ λͺ¨λ νμ΄μ§μλ λ‘κ·ΈμΈνμ§ μμ μ¬μ©μλ μ κ·Όμ νμ©ν μ μλλ‘ λ‘μ§μ λ³κ²½νκΈ°λ‘ νλ€.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(request ->
request.requestMatchers("/member/**")
.hasRole("MEMBER")
.anyRequest()
.anonymous()
)
.exceptionHandling(exceptionHandler ->
exceptionHandler.accessDeniedHandler(accessDeniedHandler()))
.formLogin(formLogin ->
formLogin.loginPage("/member/login")
.loginProcessingUrl("/member/login")
.defaultSuccessUrl("/member")
.permitAll());
return http.build();
}
request.requestMatchers("/member/**")
.hasRole("MEMBER")
`requestMatcher`μ μμ±λ `/member/**` μ νμ νμ΄μ§μ λν΄μλ§ `MEMBER` κΆνμ μꡬνλ€.
.anyRequest()
.anonymous()
λ€λ₯Έ λͺ¨λ νμ΄μ§λ λ‘κ·ΈμΈνμ§ μμ μ¬μ©μ(anonymous)λ μ κ·Όν μ μλλ‘ νμ©νλ€.
μ λμνλ κ²μ νμΈν μ μλ€.
μ΄μ μ κ·Όμ μ μ΄ν `/member`λ‘ μ μν΄λ³΄μ.
`/member`λ‘ μ κ·Όν κ²½μ° `member/login`μΌλ‘ νκ²¨μ Έ λκ°λ κ²μ νμΈν μ μλ€.
'Java > Spring' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[SpringBoot] μ μ 리μμ€μ κΆν μꡬνμ§ μκΈ° (0) | 2024.03.28 |
---|---|
[SpringBoot] νμ΄μ§λ€μ΄μ (Pagination) (0) | 2024.03.26 |
[Spring] 리λ€μ΄λ μ ν νμκ° λ무 λ§μ΅λλ€. (0) | 2024.03.24 |
[Spring] μ€νλ§ μν리ν°(Spring Security) (0) | 2024.03.21 |
[SpringBoot] Spring Security μ¬μ© μ deprecated (0) | 2024.03.16 |