[스프링 부트 3 백엔드 개발자 되기: 자바 편] 책 수정 해야 할 곳(추가 중)
https://product.kyobobook.co.kr/detail/S000201766024
[스프링 부트 3 백엔드 개발자 되기: 자바 편 | 신선영 - 교보문고
스프링 부트 3 백엔드 개발자 되기: 자바 편 |
product.kyobobook.co.kr](https://product.kyobobook.co.kr/detail/S000201766024)
해당 책에 관한 내용입니다.
우선 느낀점 : 스프링 부트의 첫시작에서 부터 기본적인 게시판 사이클 1회 정도 예제로 돌려보는 정도?
하지만 스프링 시큐리티 + CICD(GithubAction) + 배포(AWS)를 곁들인..이라 생각하면 좋을 것 같다.
책이 2023년에 나왔지만 부트 3.1x 버전 업이 되다보니 책이랑 다르게 변경점이 조금 있다.
그래서 이 책을 구매해서 방황하는 누군가를 위해 써보려고 한다!
우선 이 책의 입구컷은 생각보다 짧은 시간에 맞이한다.
책에서 DB 테스트용으로 인메모리 DB인 H2를 가지고 우선 DB에 대해 테스트를 한다.
하지만 초반 h2 설정은 에러 뜨는 사람도 있기 때문에 삽질 시간을 많이 잡아먹는다..
(분명 테스트를 위한건데 시간을 잡아먹으면 의미가 없다)
그래서 나는 Mysql로 시작하였다. Mysql 에러는 다른 블로그에서 삽질 흔적이 많으니 찾아보기 쉽다.
H2 Database 이외엔 스프링 시큐리티 까지는 막힘이 없이 진행되었다.
* 저자의 깃허브를 발견했다..
답은 여기에 다 있다.
* 책은 3.0x에서 작성되었고 현재는 3.1x 버전 까지 나와있다.
그냥 귀찮으면 build.gradle 들어가서 3.0x대 버전으로 바꾸고 Gradle build 돌리는게 정신건강에 이롭다.
그럼 책과 동일하게 돌아간다.
밑은 3.1x버전 삽질의 흔적이다
1. SecurityConfig (p.209)
WebSecurityCustomizer와 SecurityFilterChain으로 시큐리티 설정을 하는 파트이다.
우선 gradle로 spring-security를 의존성 주입을 해줘야한다.
하지만 처음에 spring-security를 주입 해줘도 WebSecurityCustomizer가 없다고 떴다.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// 추가한 Dependencies
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
// 시큐리티
implementation 'org.springframework.boot:spring-boot-starter-security'
// implementation 'org.springframework.boot:spring-boot:spring-boot-starter-security'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' // thymeleaf, 스프링 시큐리티 사용위한 의존성 추가
implementation 'org.springframework.security:spring-security-test'
}
책에서 설정한 시큐리티 말고 이렇게 하니 됐다.
중간에 .requestMatcheres(toH2Console())은 위에서 말했듯 저는 h2를 안써서 저한텐 필요가 없어서 지웠습니다.
2. SecurityFilterChain(Spring 6.1이상) 설정
책에서는 이렇게 설정이 되어있었다.
원래는 저렇게 적는게 맞다.
Spring boot 3.1(spring 6.1) 이상 기준 .and() 가 deprecated 되어서 에러가 뜬다.
람다식으로 구현했다.
//2. 특정 HTTP 요청에 대한 웹 기반 보안 구성
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests((authorize) ->
authorize
.requestMatchers("/login","/signup","/user").permitAll()
.anyRequest().authenticated()
)
.formLogin((formLogin) ->
formLogin
.loginPage("/login")
.defaultSuccessUrl("/articles")
) // 폼 기반 로그인 설정
.logout((logout) ->
logout
.logoutSuccessUrl("/login")
.invalidateHttpSession(true)
)
.csrf((csrfConfig) ->
csrfConfig.disable() // csrf 비활성화
).build();
}
어쩌면 이게 더 깔끔한것같다.
3. AuthenticationManager
// 인증 관리자 관련 설정
@Bean
public AuthenticationManager authenticationManager(AuthenticationManagerBuilder auth) throws Exception{
auth
.userDetailsService(userDetailsService)
.passwordEncoder(bCryptPasswordEncoder());
return auth.build();
}
// 패스워드 인코더로 사용할 빈 등록
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
이 이후는 책을 아직 덜 봐서 진행하는대로 써보겠다.