[Spring Blog]9. 시큐리티 커스터마이징

Mar 15, 2024
[Spring Blog]9. 시큐리티 커스터마이징
 
_core 만든 후 config, util 만들기
 
config안에 SecurityConfig 클래스 만들기
 
 
@Configuration
환경 설정과 관련된 파일이라는 것을 알려주는 어노테이션
 
@Bean 과 @Component의 차이
스프링이 객체를 제어하기 위해 객체를 bean으로 등록되어 있어야 한다.
@Controller, @Service, @Repository, @Bean, @Component 를 사용해 bean으로 등록할 수 있다.
bean
개발자가 직접 제어 불가능한 외부 라이브러리 사용시, 자동bean등록이 안된다. 특정 bean 명시적 등록 항상 Configuration과 함께 사용(사용하지 않으면 DI가 안된다.)
component
개발자가 직접 컨트롤이 가능한 내부 클래스에 사용, 자동bean호환이 된다. 일반적인 bean등록
 
스프링부트를 실행하면 스프링 컨테이너는 bean이 적용된 메서드를 자동으로 호출하지 않는다
 
SecurityConfig파일 생성
package shop.mtcoding.blog._core.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer; import org.springframework.security.web.SecurityFilterChain; @Configuration // 컴퍼넌트 스캔 public class SecurityConfig { @Bean SecurityFilterChain configure(HttpSecurity http)throws Exception{ http.authorizeHttpRequests(a -> { a.requestMatchers("/user/updateForm", "/board/**").authenticated().anyRequest().permitAll(); }); http.formLogin(f -> { f.loginPage("/loginForm"); } ); return http.build(); } }
Controller 인증 로직 삭제하기
notion image
로그인 페이지로 리다이렉트된다
http://localhost:8080/user/updateForm http://localhost:8080/board/1
notion image
인증 설정
@Bean SecurityFilterChain configure(HttpSecurity http)throws Exception{ http.authorizeHttpRequests(a -> { //인증이 필요한 부분 a.requestMatchers("/user/updateForm", "/board/**").authenticated().anyRequest().permitAll(); });
제외할 부분
@Bean public WebSecurityCustomizer ignore(){ // 제외 부분 return w -> w.ignoring().requestMatchers("/board/*", "/static/**", "/h2-console/**"); }
로그인페이지로 리다이렉트
http.formLogin(f -> { // 로그인페이지로 리다이렉트 f.loginPage("/loginForm"); } );
 
 
전체코드
package shop.mtcoding.blog._core.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer; import org.springframework.security.web.SecurityFilterChain; @Configuration // 컴퍼넌트 스캔 public class SecurityConfig { @Bean public WebSecurityCustomizer ignore(){ // 제외 부분 return w -> w.ignoring().requestMatchers("/board/*", "/static/**", "/h2-console/**"); } @Bean SecurityFilterChain configure(HttpSecurity http)throws Exception{ http.authorizeHttpRequests(a -> { //인증이 필요한 부분 a.requestMatchers("/user/updateForm", "/board/**").authenticated().anyRequest().permitAll(); }); http.formLogin(f -> { // 로그인페이지로 리다이렉트 f.loginPage("/loginForm"); } ); return http.build(); } }
 
Share article

Essential IT