이번 새 프로젝트를 진행하면서 백엔드 부분에서 팀원들과 정한 내용이다.
java 17
springboot 3
MySQL
Swagger
Gradle
코드리뷰 후에 merge 하기
Test code 작성하기
SpringBoot 3 를 사용해본적 없기에 변경사항이 있는지 등등을 체크하기 위해 먼저 구동해보려고 한다.
먼저 SpringBoot3 는 Java 17 이상 버전을 사용해야한다.
SpringBoot 3 프로젝트를 만들고 실행했더니 에러가난다.
intellij 에서 sdk 설정을 변경하지 않았다.
변경하고 실행하니 잘 실행되었다.
package com.hello.hello.config;
import com.hello.hello.oauth.CustomOAuth2UserService;
import com.hello.hello.utils.JwtAuthenticationFilter;
import com.hello.hello.utils.JwtProvider;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SpringSecurityConfig {
private final JwtProvider jwtProvider;
private final CustomOAuth2UserService customOAuth2UserService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf().disable()
.headers().frameOptions().disable()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/**","/","/index.html")
// .antMatchers("/member","/h2-console/**","/login")
.permitAll()
// .antMatchers("/post/**")
// .hasRole("USER")
.anyRequest()
.permitAll()
.and()
.addFilterBefore(new JwtAuthenticationFilter(jwtProvider), UsernamePasswordAuthenticationFilter.class)
.exceptionHandling()
.accessDeniedHandler(new AccessDeniedHandler() {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException)
throws IOException, ServletException {
response.setStatus(403);
response.setCharacterEncoding("utf-8");
response.setContentType("test/html; charset=UTF-8");
response.getWriter().write("권한이 없는 사용자입니다.");
}
})
.authenticationEntryPoint(new AuthenticationEntryPoint() {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
response.setStatus(401);
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
response.getWriter().write("인증되지 않은 사용자입니다.");
}
})
.and()
.oauth2Login()
.userInfoEndpoint()
.userService(customOAuth2UserService)
.and()
.loginPage("/login")
.defaultSuccessUrl("/")
.failureUrl("/login?error")
;
return http.build();
}
}
이전 포스팅을 하면서 작성했던 SecurityConfig 파일인데
클래스를 생성하고 http.csrf() 를 입력하자마자 deprecated 가 떴다.
미리 해보길 잘했구나 싶은 생각도 들고 어떻게 변경해야할지 찾아보도록 하자.
기존에 사용하던 csrf() 가 deprecated 되었다.
dependencies 를 확인해보니 기존까지 사용하던 SpringSecurity 는 5 버전이였고
이번엔 6 버전이 들어가있었다.
기존에는 http.csrf().disable() 을 사용하여 csrf 보호 설정을 했으나
이제는 기본적으로 활성화가 되어있다고 한다.
또 기존에는 브라우저를 이용하지 않는 api 제작을 했었기 때문에 비활성화 했지만
이번엔 필요할것같다.
이것역시 deprecated 였다.
JWT 로그인을 고려하여 STATELESS 로 설정을 하려고 한다.
또 deprecated 다.
우선 모든 URL에 대해 permitAll 를 적용해놓고 개발을 시작할 예정이다.
package Hello.boot3.config;
import org.springframework.context.annotation.Bean;
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.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.sessionManagement(
(sessionManagement) -> sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests((authorizeRequests) -> authorizeRequests.anyRequest().permitAll());
return http.build();
}
}