Super Kawaii Cute Cat Kaoani [인스타그램 클론 코딩][Spring boot] 2. Security 설정

[인스타그램 클론 코딩][Spring boot] 2. Security 설정

2024. 2. 16. 14:52
728x90
SMALL
  • Security 라이브러리를 등록하면 인증되지 않은 모든 사용자를 /login 으로 redirection 됨 
    • 상태 코드 302 = redirect
  • SecurityConfig를 통해 설정해줌
728x90

📌 SecurityConfig 세팅

package yerong.InstagramCloneCoding.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.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@EnableWebSecurity
@Configuration
public class SecurityConfig{
    @Bean
    public SecurityFilterChain filterChain (HttpSecurity http) throws Exception{
        return http
                .authorizeHttpRequests(authorizationRequest ->
                        authorizationRequest
                                .requestMatchers(
                                        AntPathRequestMatcher.antMatcher("/"),
                                        AntPathRequestMatcher.antMatcher("/user/**"),
                                        AntPathRequestMatcher.antMatcher("/image/**"),
                                        AntPathRequestMatcher.antMatcher("/subscribe/**"),
                                        AntPathRequestMatcher.antMatcher("/comment/**")
                                ).authenticated()
                                .anyRequest().permitAll()
                )
                .formLogin(formConfig ->
                        formConfig.loginPage("/auth/signin")
                                .defaultSuccessUrl("/"))
                .build();
    }
}

 

1.  @EnableWebSecurity 

  • 해당 파일로 Security를 활성화해줄 것임을 나타냄

2.  @Configuration

  • IoC에 등록 ➡️ 메모리에 뜸

3.  Security6 부터는 이런 형식으로 변경됨

@Bean
public SecurityFilterChain filterChain (HttpSecurity http) throws Exception{
        return http.build();
}

4. url에 권한을 줌

.authorizeHttpRequests(authorizationRequest ->
                        authorizationRequest
                                .requestMatchers(
                                        AntPathRequestMatcher.antMatcher("/"),
                                        AntPathRequestMatcher.antMatcher("/user/**"),
                                        AntPathRequestMatcher.antMatcher("/image/**"),
                                        AntPathRequestMatcher.antMatcher("/subscribe/**"),
                                        AntPathRequestMatcher.antMatcher("/comment/**")
                                ).authenticated()
                                .anyRequest().permitAll()
                )
  • authenticated() : 인증이 필요함(403 error)
  • anyRequest().permitAll() : 그 외의 모든 요청은 모두 허용

5.  인증이 필요한 페이지에 들어가면(403) 로그인 창으로 redirect 해줘야 함

.formLogin(formConfig ->
	formConfig.loginPage("/auth/signin")
		.defaultSuccessUrl("/"))

 

  • 권한이 없는 페이지들은 로그인 페이지로 넘겨줌
  • 로그인에 성공했다면 "/" 로 redirect 
반응형

 

 


PREV

 

[인스타그램 클론코딩] 1. 프로젝트 설정

📌 프로젝트 환경 설정 java 17, spring boot 3.2.2 mariaDB dependency : Spring Data JPA, lombok, web, security, mariaDB 💡 CSS 등 프론트 부분은 강의에서 제공된 자료를 사용했습니다! ✅ 화면 소개 📌 회원가입 화면

nyeroni.tistory.com

NEXT

 

[인스타그램 클론코딩] 3. 회원 가입 구현

더보기 ✔️ username, password, email, name을 입력 받고 회원 가입을 진행하도록 하겠음! 📌 User 엔티티 구현 package yerong.InstagramCloneCoding.domain.user; import jakarta.persistence.*; import lombok.*; import yerong.Instagram

nyeroni.tistory.com

SMALL

 

728x90
LIST

BELATED ARTICLES

more