π Spring Securityμ Spring Bootμ κ΄κ³
λͺ©μ°¨
ν΅μ¬ κ°λ
Spring Securityλ Spring Bootμ ν¬ν¨λ κ²μ΄ μλλΌ,
Spring Bootκ° Spring Securityλ₯Ό λ§€μ° μ½κ² μ¬μ©ν μ μλλ‘ μ§μνλ ꡬ쑰μ λλ€.
κ°λ¨ν λ§ν΄:
- Spring Security == 보μ μ λ¬Έ νλ μμν¬ (λ 립μ )
- Spring Boot == μ¬λ¬ Spring νλ‘μ νΈλ₯Ό μ½κ² 쑰립ν΄μ£Όλ λꡬ
Spring Securityμ λ 립μ±
π¦ λ³κ°μ νλ‘μ νΈ
Spring Securityλ βSpring μνκ³βμ μν λ 립μ μΈ νλ‘μ νΈμ λλ€.
Spring μνκ³
βββ Spring Framework (Core)
βββ Spring Boot (Auto-Configuration)
βββ Spring Security (보μ)
βββ Spring Data JPA (λ°μ΄ν° μ κ·Ό)
βββ Spring Web MVC (μΉ)
βββ Spring Cloud (λ§μ΄ν¬λ‘μλΉμ€)
βββ ... κΈ°ν νλ‘μ νΈλ€
π°οΈ μμ¬μ λ§₯λ½
- Spring Security: 2003λ βAcegi Securityβλ‘ μμ
- Spring Boot: 2014λ 첫 릴리μ¦
Spring Bootκ° νμνκΈ° 10λ μ΄μ μ λΆν° Spring Securityλ μ‘΄μ¬νμ΅λλ€.
π― λ 립μ μ¬μ© κ°λ₯
Spring Securityλ Spring Boot μμ΄λ μ¬μ©ν μ μμ΅λλ€.
<!-- Spring Framework + Spring Security (Spring Boot μμ΄) -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>6.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>6.2.0</version>
</dependency>
νμ§λ§ μ΄ κ²½μ° λͺ¨λ μ€μ μ κ°λ°μκ° μ§μ ν΄μΌ ν©λλ€ (λ§€μ° λ³΅μ‘ν¨).
Spring Bootμ μν
π Auto-Configurationμ ν
Spring Bootλ 볡μ‘ν μ€μ μ μλννμ¬ κ°λ°μμ μΆμ νκΈ°μ μΌλ‘ κ°μ νμ΅λλ€.
β Spring Boot μ΄μ (μ ν΅μ μΈ Spring)
<!-- web.xmlμ νν° λ±λ‘ -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
// SecurityConfig.java - μλ§μ λΉ μ€μ νμ
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public AuthenticationManager authenticationManager() {
// 볡μ‘ν μ€μ ...
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public UserDetailsService userDetailsService() {
// μ¬μ©μ μ 보 λ‘λ μ€μ ...
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// νν° μ²΄μΈ μλ ꡬμ±
http
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.logout();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// μΈμ¦ λ§€λμ μ€μ ...
}
}
β Spring Boot μ¬μ© μ
// build.gradle - λ¨ ν μ€μ μμ‘΄μ± μΆκ°
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
}
<!-- λλ pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
μ΄κ²λ§μΌλ‘ λ! Spring Bootκ° μλμΌλ‘:
- Spring Security νν° μ²΄μΈ λ±λ‘
- κΈ°λ³Έ λ‘κ·ΈμΈ νμ΄μ§ μμ±
- μΈμ κΈ°λ° μΈμ¦ μ€μ
- CSRF λ³΄νΈ νμ±ν
- κΈ°λ³Έ μ¬μ©μ κ³μ μμ± (username: user, password: μ½μμ μΆλ ₯)
Using generated security password: 8e557245-73e2-4286-969a-ff57fe326336
ποΈ μ»€μ€ν°λ§μ΄μ§λ κ°λ¨
νμν λΆλΆλ§ μ€λ²λΌμ΄λνλ©΄ λ©λλ€.
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll()
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
)
.formLogin(Customizer.withDefaults())
.oauth2Login(Customizer.withDefaults()); // OAuth2λ μ½κ²!
return http.build();
}
}
μ€μ μμ
π μμ‘΄μ± κ΄κ³ μ΄ν΄νκΈ°
Spring Bootλ μ¬λ¬ Spring νλ‘μ νΈλ₯Ό 쑰립νμ¬ μ½κ² μ¬μ©νκ² ν΄μ£Όλ β쑰립 λꡬβμ λλ€.
βββββββββββββββββββββββββββββββββββββββ
β Spring Boot β
β (쑰립 λꡬ / λ°μ²) β
βββββββββββββββββββββββββββββββββββββββ
β μ νμ μΌλ‘ ν΅ν©
βββββββββββ¬ββββββββββ¬ββββββββββ
β β β β
βββββΌββββ ββββΌββββ βββββΌββββ ββββΌββββ
βSpring β βSpringβ βSpring β βSpringβ
βSecurityβ βData β β Web β βCloud β
β β β JPA β β MVC β β β
βββββββββ ββββββββ βββββββββ ββββββββ
π οΈ μ€μ νλ‘μ νΈ μ€μ
1. κΈ°λ³Έ μΉ μ ν리μΌμ΄μ
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
μ΄ μνμμλ 보μμ΄ μ ν μμ΅λλ€ (λͺ¨λ μλν¬μΈνΈ 곡κ°).
2. 보μ μΆκ°
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security' // μΆκ°
}
μ΄μ λͺ¨λ μλν¬μΈνΈκ° 보νΈλ©λλ€ (κΈ°λ³Έ νΌ λ‘κ·ΈμΈ νμ±ν).
3. JWT μΈμ¦μΌλ‘ νμ₯
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.3'
}
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable()) // Statelessμ΄λ―λ‘ CSRF λΉνμ±ν
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
.addFilterBefore(jwtAuthenticationFilter(),
UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
π μ νμ μμ
κ°λ°μλ build.gradle
λλ pom.xml
μ ν΅ν΄ νμν κ²λ§ μ νν©λλ€.
νμν κΈ°λ₯ | μΆκ°ν Starter |
---|---|
μΉ μ ν리μΌμ΄μ | spring-boot-starter-web |
보μ | spring-boot-starter-security |
λ°μ΄ν°λ² μ΄μ€ (JPA) | spring-boot-starter-data-jpa |
OAuth2 ν΄λΌμ΄μΈνΈ | spring-boot-starter-oauth2-client |
OAuth2 리μμ€ μλ² | spring-boot-starter-oauth2-resource-server |
μ 리
π― ν΅μ¬ μμ½
Spring Security | Spring Boot |
---|---|
λ 립μ μΈ λ³΄μ μ λ¬Έ νλ μμν¬ | μ¬λ¬ Spring νλ‘μ νΈλ₯Ό 쑰립νλ λꡬ |
2003λ λΆν° μ‘΄μ¬ (Acegi Security) | 2014λ νμ |
Spring Boot μμ΄λ μ¬μ© κ°λ₯ | Security μμ΄λ μ¬μ© κ°λ₯ |
μΈμ¦/μΈκ°μ λͺ¨λ κΈ°λ₯ μ 곡 | Auto-ConfigurationμΌλ‘ μ€μ μλν |
π κ΄κ³ μ 리
Spring Security β Spring Bootμ μΌλΆ
Spring Security = Spring μνκ³μ λ
립 νλ‘μ νΈ
Spring Boot = Spring Securityλ₯Ό μ½κ² μ¬μ©νκ² ν΄μ£Όλ μ§μ λꡬ
π‘ μ€λ¬΄ κ΄μ
βSpring Bootλ₯Ό μ¬μ©νλ€βλ κ²μ:
- Spring Frameworkλ₯Ό κΈ°λ°μΌλ‘
- νμν Spring νλ‘μ νΈλ€(Security, Data JPA λ±)μ μ ννμ¬
- Auto-Configurationμ λμμΌλ‘ λΉ λ₯΄κ² κ°λ°νλ κ²
βSpring Securityλ₯Ό μ¬μ©νλ€βλ κ²μ:
- μ ν리μΌμ΄μ μ μΈμ¦/μΈκ° κΈ°λ₯μ μΆκ°νλ κ²
- Spring Bootκ° μμ΄λ κ°λ₯νμ§λ§, Spring Bootμ ν¨κ» μ°λ©΄ ν¨μ¬ νΈλ¦¬ν¨
π λ€μ νμ΅ μ£Όμ
- Spring Boot Auto-Configuration λ΄λΆ λμ μ리
-
spring-boot-starter-security
κ° μλ μ€μ νλ Beanλ€ - Custom Auto-Configuration λ§λ€κΈ°
- Spring Boot Actuatorμ Security ν΅ν©
κ²°λ‘ : Spring Securityλ Spring Bootμ μΌλΆκ° μλλΌ, Spring Bootκ° κ°λ ₯νκ² μ§μνκ³ ν΅ν©ν΄ μ£Όλ λ³κ°μ 보μ μ λ¬Έ νλ μμν¬μ λλ€.