๐Spring Security๋?
ํํฐ๋ง์ผ๋ก ๋์ํ๋ ์คํ๋ง ๊ธฐ๋ฐ์ ์ดํ๋ฆฌ์ผ์ด์
์ ๋ณด์(์ธ์ฆ๊ณผ ๊ถํ)์ ๋ด๋นํ๋ ํ๋ ์์ํฌ
๋ง์ฝ ์คํ๋ง์ํ๋ฆฌํฐ๋ฅผ ์ฌ์ฉํ์ง ์์๋ค๋ฉด, ์์ฒด์ ์ผ๋ก ์ธ์
์ ์ฒดํฌํ๊ณ redirect ๋ฑ์ ๋ค์ํ ์ฒ๋ฆฌ๋ฅผ ํด์ผํ์ง๋ง ์คํ๋ง ์ํ๋ฆฌํฐ๋ ๋ณด์๊ณผ ๊ด๋ จํด์ ์ฒด๊ณ์ ์ผ๋ก ๋ง์ ์ต์
๋ค์ ์ง์ํด์ฃผ๊ธฐ ๋๋ฌธ์ ๊ฐ๋จํ ์ฝ๋ฉ๋ง์ผ๋ก ๊ตฌํ ๊ฐ๋ฅํ๋ค.
๐Spring Security ์ฌ์ฉ ๋ฐฉ๋ฒ
1. ๊ธฐ์กด Spring boot ํ๋ก์ ํธ์ Spring security ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
//...
//Spring security ๋ผ์ด๋ธ๋ฌ๋ฆฌ
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
//..
2. ์ํ๋ฆฌํฐ ๊ด๋ จ๋ ๋ก๊ทธ๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ํ ์ค์
application.yml
logging:
level:
org.springframework.security: DEBUG
Security ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ pom.xml์ ์ถ๊ฐํ๊ณ localhost:8080์ผ๋ก ์ ์ํ๋ฉด localhost:8080/login์ผ๋ก ๋ฆฌ๋ค์ด๋ ํธ๋๊ณ ๋ฐ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ ๋ก๊ทธ์ธ ํ์ด์ง๊ฐ ๋ฌ๋ค.
๊ทธ ๋ง์ ์ฆ์จ Security ์์ View, Controller ๋ฑ ์ด๋ ์ ๋์ ๊ตฌํ์ด ๋์ด ์๊ฒ ๋ค!
๐์ฌ์ฉ ์์
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http){
try {
http.csrf().disable()
.authorizeRequests()
.antMatchers().permitAll()
.anyRequest().denyAll();
return http.build();
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
csrf().disable : ํ ํฐ ๋ฐ๊ธ ๋ฐ์์ ์ ์ฅํ ์ ์๊ฒ ํ๋ ๊ทธ ๊ธฐ๋ฅ์ ๋
antMatchers(~).permitAll() : ~ ๋ถ๋ถ์ ํ์ฉ
.anyRequest().denyAll() : ๋ชจ๋ ์์ฒญ์ ํ์ฉํ์ง ์์
Filter์์ ๊ฑฐ๋ถ๊ฐ ๋๋ฉด, Dispatcher๊น์ง ๊ฐ์ง๋ ์๊ณ ๋ฐ๋ก Filter์์ ๋ฐํ๋๋ค.
์ ์ฝ๋๋ ๋ชจ๋ ์์ฒญ์ ๋ํ์ฌ denyํ๊ธฐ ๋๋ฌธ์, Filter์์ ๋ฐํ๋์ด ๋ฐ ์ฌ์ง๊ณผ ๊ฐ์ด ์ก์ธ์ค ๊ฑฐ๋ถ ์๋ฌ๊ฐ ๋จ๊ฒ ๋๋ค.
'โก๐ฉโ๐ป โก > ยบSpring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] ์คํ๋ง ๋ฌธ์ํ(Spring DOC) - Swagger UI (0) | 2023.12.26 |
---|---|
[Spring JPA] JpaRepository ์ธํฐํ์ด์ค findById() ๋ฉ์๋ ์ฌ์ฌ์ฉ (0) | 2023.12.21 |
[Spring] ORM, JPA, Spring Data JPA (0) | 2023.12.21 |
[Spring] ๋กฌ๋ณต(Lombok) ๋ผ์ด๋ธ๋ฌ๋ฆฌ (0) | 2023.12.20 |
[Spring] ์์กด์ฑ ์ฃผ์ (Dependency Injection, DI) (1) | 2023.12.20 |