쿠키와 세션이 필요한 이유
쿠키
세션
쿠키와 세션 비교
// 스프링 시큐리티
implementation 'org.springframework.boot:spring-boot-starter-security'
// Thymeleaf (뷰 템플릿 엔진)
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
package com.sparta.springcore.security;
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 // 스프링 Security 지원을 가능하게 함
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.headers().frameOptions().disable();
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.defaultSuccessUrl("/")
.permitAll()
.and()
.logout()
.permitAll();
}
}
- 해당 코드를 추가함으로써 자동으로 로그인페이지가 만들어진다. 아이디는 user, 비밀번호는 콘솔에 찍혀있는 제공되는 비밀번호로 로그인 가능하다.
@Bean
public BCryptPasswordEncoder encodePassword() {
return new BCryptPasswordEncoder();
}
스프링 시큐리티를 이용한 로그인 처리 구현
로그인 처리 과정: 인증/인가 성공 시에만, Controller에게 회원 정보 전달