[์คํ๋ง ์ํ๋ฆฌํฐ ๊ธฐ๋ณธ API & Filter ์ดํด] ํ๋ก์ ํธ ๊ตฌ์ฑ ๋ฐ ์์กด์ฑ ์ถ๊ฐ
๐ ์ด ๊ธ์ ์ธํ๋ฐ ์ธํฐ๋ท ๊ฐ์ ์คํ๋ง ์ํ๋ฆฌํฐ - Spring Boot ๊ธฐ๋ฐ์ผ๋ก ๊ฐ๋ฐํ๋ Spring Security ์์ ๊ธฐ๋ฐ๋ ๊ฒ์ผ๋ก ์ฌ๊ธฐ์ ์ธ์ฉ๋๋ PPT ์ด๋ฏธ์ง ๋ํ ๋ชจ๋ ํด๋น ๊ฐ์์์ ๊ฐ์ ธ์์์ ์๋ฆฝ๋๋ค.
๐น ๊ฐ๋ฐํ๊ฒฝ
- IDE : Intellij IDEA Community
- Language : Java 8
- Framework : Spring Boot
- DataBase : MySQL
๐น ์ฌ์ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- Thymeleaf
- Lombok
๐น ๋ณด์์ ์ฑ ์ค์
- ์์ ๋ฐ ๊ถํ ์ค์
- ๋ง์ดํ์ด์ง
- ์์์ค์ : /mypage
- ๊ถํ๋งคํ : ROLE_USER
- ๋ฉ์์ง
- ์์์ค์ : /message
- ๊ถํ๋งคํ : ROLE_MANAGER
- ํ๊ฒฝ์ค์
- ์์์ค์ : /config
- ๊ถํ๋งคํ : ROLE_ADMIN
- ๊ด๋ฆฌ์
- ์์์ค์ : /admin/**
- ๊ถํ๋งคํ : ROLE_ADMIN
- ๋ง์ดํ์ด์ง
- ์ฌ์ฉ์ ๋ฑ๋ก ๋ฐ ๊ถํ๋ถ์ฌ
- ๊ถํ๊ณ์ฆ์ ์ฉ
- ROLE_ADMIN > ROLE_MANAGER > ROLE_USER
- ๋ฉ์๋ ๋ณด์ ์ค์
- ๋ฉ์๋ ๋ณด์ - ์๋น์ค ๊ณ์ฆ ๋ฉ์๋ ์ ๊ทผ ์ ์ด
- io.security.corespringsecurity.aopsecurity.AopMethodService.methodSecured
- ํฌ์ธํธ์ปท ๋ณด์ - ํฌ์ธํธ์ปท ํํ์์ ๋ฐ๋ฅธ ๋ฉ์๋ ์ ๊ทผ ์ ์ด
- execution(public * io.security.corespringsecurity.aopsecurity.*Service.pointcut*(..))
- ๋ฉ์๋ ๋ณด์ - ์๋น์ค ๊ณ์ฆ ๋ฉ์๋ ์ ๊ทผ ์ ์ด
- IP ์ ํํ๊ธฐ
๐น ํ๋ก์ ํธ ์์ฑ
Intellij IDEA Community๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ https://start.spring.io๋ฅผ ์ฌ์ฉํ์ฌ ์คํ๋ง ๋ถํธ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๋ค.
- Maven Project
- Spring Boot version : 2.7.15
- Project Metadata
- Group : io.security
- Artifact : basicsecurity
- Packaging : Jar
- Java : 8
- Dependencies
- Spring Web
ํ๋ก์ ํธ GENERATE > ์์ถ ํด์ > Intellij์์ ํด๋ ์คํ
๐น ์คํ๋ง๋ถํธ ํฌํธ ๋ณ๊ฒฝ
src > main > java > io > security > basicsecurity > BasicsecurityApplication
์๋ฒ ๊ฐ๋ ์ 8080 ํฌํธ๋ฅผ ์ด๋ฏธ ์ฌ์ฉํ๊ณ ์๋ค๊ณ ํ๋ค.
์ฌ์ฉํ์ง ์๋ ํฌํธ๋ก ๋ณ๊ฒฝํด ์ฃผ๊ธฐ ์ํด
src > main > resources > application.properties ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ด ์ ๋ ฅํด ์ค๋ค.
server.port = 8086
๐น ์คํ๋ง ์ํ๋ฆฌํฐ ์์กด์ฑ ์ถ๊ฐ
src > main > java > io > security > basicsecurity > SecurityController ์์ฑ
@RestController
public class SecurityController {
@GetMapping("/")
public String index(){
return "home";
}
}
pom.xml ์คํ๋ง ์ํ๋ฆฌํฐ ์์กด์ฑ ์ฃผ์
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
http://localhost:8086 ์ ์ ์ ์คํ๋ง ์ํ๋ฆฌํฐ ๋ก๊ทธ์ธ ํ์ด์ง๊ฐ ์ ์ฉ๋๋ค.
์คํ๋ง ์ํ๋ฆฌํฐ ์ ์ฉ ์ ๊ธฐ๋ณธ ๊ณ์ ์ด ํ๋ ์ ๊ณต๋๋ค.
- Username : user
- Password : ๋๋ค ๋ฌธ์์ด
๐น ์คํ๋ง ์ํ๋ฆฌํฐ ์์กด์ฑ ์ถ๊ฐ ์ ์ผ์ด๋๋ ์ผ๋ค
- ์๋ฒ๊ฐ ๊ฐ๋๋๋ฉด ์คํ๋ง ์ํ๋ฆฌํฐ์ ์ด๊ธฐํ ์์ ๋ฐ ๋ณด์ ์ค์ ์ด ์ด๋ฃจ์ด์ง๋ค.
- ๋ณ๋์ ์ค์ ์ด๋ ๊ตฌํ์ ํ์ง ์์๋ ๊ธฐ๋ณธ์ ์ธ ์น ๋ณด์ ๊ธฐ๋ฅ์ด ํ์ฌ ์์คํ
์ ์ฐ๋๋์ด ์๋ํ๋ค.
- ๋ชจ๋ ์์ฒญ์ ์ธ์ฆ์ด ๋์ด์ผ ์์์ ์ ๊ทผ ๊ฐ๋ฅ
- ์ธ์ฆ ๋ฐฉ์์ ํผ ๋ก๊ทธ์ธ ๋ฐฉ์๊ณผ httpBasic ๋ก๊ทธ์ธ ๋ฐฉ์์ ์ ๊ณต
- ๊ธฐ๋ณธ ๋ก๊ทธ์ธ ํ์ด์ง, ๊ธฐ๋ณธ ๊ณ์ ํ ๊ฐ ์ ๊ณต
๐น ๋ฌธ์ ์
- ๊ณ์ ์ถ๊ฐ, ๊ถํ ์ถ๊ฐ, DB ์ฐ๋ ๋ฑ
- ๊ธฐ๋ณธ์ ์ธ ๋ณด์ ๊ธฐ๋ฅ ์ธ์ ์์คํ ์์ ํ์๋ก ํ๋ ๋ ์ธ๋ถ์ ์ด๊ณ ์ถ๊ฐ์ ์ธ ๋ณด์๊ธฐ๋ฅ์ด ํ์
๐จ Error
pom.xml
Spring Security ์์กด์ฑ ์ฃผ์ ์ค
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Dependency 'org.springframework.boot:spring-boot-starter-security:2.7.15' not found ๋ฐ์
๐ก ํด๊ฒฐ๋ฐฉ๋ฒ
File > Invalidate Caches > Invalidate and Restart
๐ reference