JAVA

[JAVA/SpringBoot] '스프링 시큐리티' 프레임워크 (2-5)

wonderson 2022. 5. 29. 10:12
반응형

[여기에 적은 거는 강의 내용을 다 적은게 아니라서 강의자료 보면서 이 게시글 참고하자.]

[스프링 심화 강의 2주차 5강 '스프링 시큐리티' 프레임워크]

 

- springCore 프로젝트에서 실습

 

스프링 시큐리티 적용

'스프링 시큐리티' 프레임워크는 스프링 서버에 필요한 인증 및 인가를 위해 많은 기능을 제공해 줌으로써 개발의 수고를 덜어 줍니다. 마치 '스프링' 프레임워크가 웹 서버 구현에 편의를 제공해 주는 것처럼요!

 

'스프링 시큐리티' 프레임워크 추가

- [코드스니펫] build.gradle

		// 스프링 시큐리티
    implementation 'org.springframework.boot:spring-boot-starter-security'

 

  • '스프링 시큐리티' 활성화
    • [코드스니펫] security > WebSecurityConfig
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.authorizeRequests()
                // 어떤 요청이든 '인증'
                .anyRequest().authenticated()
                .and()
                    // 로그인 기능 허용
                    .formLogin()
                    .defaultSuccessUrl("/")
                    .permitAll()
                .and()
                    // 로그아웃 기능 허용
                    .logout()
                    .permitAll();
    }
}

 

  • 잠깐! ‘WebSecurityConfigurerAdapter가 deprecated’ 되어 활성화 되지 않으셨나요?
    @Configuration
    public class SecurityConfiguration {
    
        @Bean
        public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
            http
                .authorizeHttpRequests((authz) -> authz
                    .anyRequest().authenticated()
                )
                .httpBasic(withDefaults());
            return http.build();
        }
    
    }
    
    👉 참고 링크 https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter
  • Spring security 5.7버전 이상부터 deprecated 되었습니다. 스프링 부트 버전을 Spring security가 5.7 아래 버전을 사용하는 버전으로 변경 하시거나 버전을 낮추지 않는다면 WebSecurityConfigurerAdapter 대신에 아래처럼 구현해주세요 :)

-- 위에 때문인지는 잘 모르겠지만 그래서 나는 build.gradle에서 springboot 버전을 2.7.0에서 2.6.8 로 버전 다운 그레이드 함

plugins {
    id 'org.springframework.boot' version '2.6.8'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

 

스프링 시큐리티의 default 로그인 기능

스프링 시큐리티에서 기본적으로 제공하는 로그인 html 기능 확인하기

  • Username: user
  • Password: spring 로그 확인 (서버 시작 시마다 변경됨)

 

반응형