์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
- dockerhub
- spring scheduler
- spring
- Personal Access Token
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- node.js mysql ์ฐ๋
- java์ฑํ
- injection๊ณต๊ฒฉ
- Java
- ์๋ฐ
- ์๊ณ ๋ฆฌ์ฆ
- ๋์์ธํจํด
- ์ค๋ธ์
- Jenkins
- MySQL
- oauth
- ํฌ๋ก์ค์ฌ์ดํธ ์คํฌ๋ฆฝํธ
- ์ฝํ
- React
- docker
- ์ฟ ๋ฒ๋คํฐ์ค
- db์ ๋ต
- GoogleLogin
- SpringBoot
- @scheduler
- ์ฝ๋ฉํ ์คํธ
- Node.js
- db์ํคํ ์ณ
- CICD
- ๋ ผ๋ธ๋กํน
- Today
- Total
<Hello Hosung๐/>
[SpringBoot] Spring Security๋ฅผ ํ์ฉํ ์ธ์ฆ ๋ฐ ๊ถํ ๋ณธ๋ฌธ
[SpringBoot] Spring Security๋ฅผ ํ์ฉํ ์ธ์ฆ ๋ฐ ๊ถํ
์ข์ถฉ์ฐ๋ ๋ฐฑ์๋ ๊ฐ๋ฐ์ ์ผ๊ธฐ๐ง 2024. 11. 23. 22:34Spring Security๋ Spring ๊ธฐ๋ฐ ์ ํ๋ฆฌ์ผ์ด์
์์ ์ธ์ฆ(authentication) ๋ฐ ๊ถํ(authorization) ์ฒ๋ฆฌ๋ฅผ ์ํ ๊ฐ๋ ฅํ ํ๋ ์์ํฌ์
๋๋ค. ์ด ๋ธ๋ก๊ทธ์์๋ Spring Security๋ฅผ ์ค์ ํ๊ณ , ๊ธฐ๋ณธ์ ์ธ ์ธ์ฆ ๋ฐ ๊ถํ ๊ด๋ฆฌ ๋ฐฉ๋ฒ์ ์ค๋ช
ํฉ๋๋ค.
1. Spring Security ์๊ฐ
Spring Security๋ ์น ์ ํ๋ฆฌ์ผ์ด์
์์ ์ค์ํ ๋ณด์ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ํ๋ ์์ํฌ์
๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค:
์ธ์ฆ(Authentication): ์ฌ์ฉ์๊ฐ ๋๊ตฌ์ธ์ง ํ์ธํ๋ ๊ณผ์ .
๊ถํ(Authorization): ์ฌ์ฉ์๊ฐ ํน์ ๋ฆฌ์์ค์ ์ ๊ทผํ ์ ์๋ ๊ถํ์ ํ์ธํ๋ ๊ณผ์ .
2. Spring Security ์ค์ ํ๊ธฐ
2.1. ์์กด์ฑ ์ถ๊ฐ
Spring Security๋ฅผ ํ๋ก์ ํธ์ ์ถ๊ฐํ๋ ค๋ฉด pom.xml์ ์์กด์ฑ์ ์ถ๊ฐํด์ผ ํฉ๋๋ค.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- ํ์ํ ๊ฒฝ์ฐ JPA, Thymeleaf ๋ฑ์ ์์กด์ฑ๋ ์ถ๊ฐ -->
</dependencies>
Spring Boot ํ๋ก์ ํธ์์๋ spring-boot-starter-security๋ง ์ถ๊ฐํ๋ฉด ๊ธฐ๋ณธ์ ์ธ ๋ณด์ ์ค์ ์ด ์๋์ผ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
2.2. ๊ธฐ๋ณธ ์ธ์ฆ ์ค์
Spring Security๋ ๊ธฐ๋ณธ์ ์ผ๋ก HTTP ๊ธฐ๋ณธ ์ธ์ฆ์ ์ฌ์ฉํฉ๋๋ค. ์ด๋ฅผ ํตํด ์ ํ๋ฆฌ์ผ์ด์
์ ์ ๊ทผํ๋ ค๋ ์ฌ์ฉ์๋ ๋ก๊ทธ์ธ ํ๋ฉด์ ํตํด ์ธ์ฆ์ ๋ฐ๊ฒ ๋ฉ๋๋ค.
import org.springframework.context.annotation.Configuration;
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.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/public/**").permitAll() // ๊ณต๊ฐ URL์ ์ธ์ฆ ์์ด ์ ๊ทผ ๊ฐ๋ฅ
.anyRequest().authenticated() // ๋๋จธ์ง URL์ ์ธ์ฆ ํ์
.and()
.formLogin(); // ๊ธฐ๋ณธ ๋ก๊ทธ์ธ ํผ ์ฌ์ฉ
}
}
์ด ์ค์ ์ /public/** ๊ฒฝ๋ก๋ ์ธ์ฆ ์์ด ์ ๊ทผํ ์ ์๋๋ก ํ๊ณ , ๋ค๋ฅธ ๋ชจ๋ ์์ฒญ์ ์ธ์ฆ์ ์๊ตฌํ๋๋ก ๊ตฌ์ฑํฉ๋๋ค.
3. ์ฌ์ฉ์ ์ธ์ฆ ๊ตฌํ
Spring Security๋ ๊ธฐ๋ณธ์ ์ผ๋ก UserDetailsService๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๋ก๋ํฉ๋๋ค. ์ด๋ฅผ ๊ตฌํํ์ฌ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ ์ ์์ต๋๋ค.
3.1. UserDetailsService ๊ตฌํ
์ฌ์ฉ์ ์ธ์ฆ์ ์ํด UserDetailsService๋ฅผ ๊ตฌํํ ์ ์์ต๋๋ค.
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// ์ค์ DB์์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ์ฝ๋
if ("admin".equals(username)) {
return User.builder()
.username("admin")
.password("{noop}password") // {noop}์ ํจ์ค์๋ ์ธ์ฝ๋ฉ์ ํ์ง ์์์ ์๋ฏธ
.roles("ADMIN")
.build();
} else {
throw new UsernameNotFoundException("User not found");
}
}
}
3.2. ๋น๋ฐ๋ฒํธ ์ธ์ฝ๋ฉ
ํจ์ค์๋๋ฅผ ์์ ํ๊ฒ ์ ์ฅํ๋ ค๋ฉด ๋น๋ฐ๋ฒํธ๋ฅผ ์ธ์ฝ๋ฉํด์ผ ํฉ๋๋ค.
Spring Security๋ BCryptPasswordEncoder๋ฅผ ์ฌ์ฉํ์ฌ ๋น๋ฐ๋ฒํธ๋ฅผ ์์ ํ๊ฒ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PasswordConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
์ฌ์ฉ์ ์ธ์ฆ ๊ณผ์ ์์ ๋น๋ฐ๋ฒํธ๋ฅผ ์ธ์ฝ๋ฉ๋ ํํ๋ก ๋น๊ตํ ์ ์๊ฒ ๋ฉ๋๋ค.
4. ๊ถํ ๊ด๋ฆฌ
Spring Security์์๋ ๊ถํ ๊ด๋ฆฌ๋ ๊ฐ๋จํ๊ฒ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ๊ถํ์ @PreAuthorize, @Secured, ๋๋ hasRole() ๋ฑ์ ํํ์์ ์ฌ์ฉํ์ฌ ์ค์ ํ ์ ์์ต๋๋ค.
4.1. URL ๊ธฐ๋ฐ ๊ถํ ์ค์
Spring Security์์๋ HttpSecurity๋ฅผ ํตํด URL์ ๋ํ ๊ถํ์ ์ค์ ํ ์ ์์ต๋๋ค.
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN") // ๊ด๋ฆฌ์๋ง ์ ๊ทผ ๊ฐ๋ฅ
.antMatchers("/user/**").hasAnyRole("USER", "ADMIN") // ์ฌ์ฉ์ ๋๋ ๊ด๋ฆฌ์๊ฐ ์ ๊ทผ ๊ฐ๋ฅ
.anyRequest().authenticated() // ๋๋จธ์ง URL์ ์ธ์ฆ ํ์
.and()
.formLogin();
}
4.2. ๋ฉ์๋ ๊ธฐ๋ฐ ๊ถํ ์ค์
๋ฉ์๋ ๋ ๋ฒจ์์๋ ๊ถํ์ ์ค์ ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ํน์ ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ ์ ์ ๊ถํ์ ์ฒดํฌํ๋๋ก ์ค์ ํ ์ ์์ต๋๋ค.
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/admin/dashboard")
public String getAdminDashboard() {
return "Admin Dashboard";
}
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
@GetMapping("/user/profile")
public String getUserProfile() {
return "User Profile";
}
}
5. ๋ง๋ฌด๋ฆฌ
Spring Security๋ฅผ ํ์ฉํ๋ฉด ์ ํ๋ฆฌ์ผ์ด์
์์ ์ธ์ฆ๊ณผ ๊ถํ ๊ด๋ฆฌ๋ฅผ ๋งค์ฐ ์ฝ๊ฒ ์ค์ ํ ์ ์์ต๋๋ค. ์ฌ์ฉ์ ์ธ์ฆ๊ณผ ๊ถํ ์ค์ ์ ํตํด ๋ณด์์ ๊ฐํํ๊ณ , ์ธ๋ฐํ ๊ถํ ์ ์ด๊ฐ ๊ฐ๋ฅํฉ๋๋ค. ์์์ ์ค๋ช
ํ ๊ธฐ๋ณธ ์ค์ ์ ๋ฐํ์ผ๋ก ๋ ๋ณต์กํ ์๊ตฌ ์ฌํญ์ ์ฒ๋ฆฌํ ์ ์๋ ๊ธฐ๋ฐ์ ๋ง๋ จํ ์ ์์ต๋๋ค.
์ถ๊ฐ์ ์ผ๋ก, JWT ์ธ์ฆ, OAuth2 ์ธ์ฆ ๋ฑ ๊ณ ๊ธ ๊ธฐ๋ฅ๋ค๋ Spring Security์์ ์ง์ํ๋ฏ๋ก ํ์์ ๋ฐ๋ผ ํ์ฅํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค.
'๐ป Java > ใ คJava(SpringBoot)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] AOP ๊ฐ๋ ๋ถํฐ ๋ก๊น ์์ ๊น์ง ํ ๋ฒ์ ์ดํดํ๊ธฐ (0) | 2024.11.17 |
---|---|
[SpringBoot] ์์กด์ฑ ์ฃผ์ (Dependency Injection) ๊ฐ๋ ๊ณผ ํ์ฉ (5) | 2024.11.08 |
[SpringBoot]Scheduling (0) | 2024.08.16 |