티스토리 뷰

728x90

앞서 시큐리티의 인증과 인가에 대해서 이야기했다.

스프링 시큐리티에서는 세부적인 보안 기능을 설정할 수 있는 인증과 인가 API를 제공한다.

먼저 스프링 시큐리티 의존성을 추가해주고 스프링 시큐리티의 웹 보안 기능을 초기화 하고 설정해야 한다.

 

의존성 추가 (maven)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

스프링 시큐리티에 의존성을 추가하고 서버를 기동하면 스프링 시큐리티의 초기화 작업 및 보안 설정이 이루어진다.

그런데 기본적인 보안 기능 외에는 만드려는 애플리케이션에서 세부적이고 추가적인 보안 기능이 필요하기 때문에 스프링 시큐리티에서 제공하는 WebSecurityConfigureAdapter를 상속받아서 세부적인 보안이나 기능을 설정할 수 있다.

출처 - 인프런 스프링 시큐리티

설정 클래스는 Java 파일로 작성할 수 있다. 

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

@Slf4j
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //인가정책
        http
                .authorizeRequests()
                .anyRequest().authenticated();
        //인증정책
        http
                .formLogin();

    }
}

WebSecurityConfigureAdapter 를 상속받은 SecurityConfig 는 여러 메소드들을 오버라이딩 할 수 있는데 그 중에 인가와 인증을 설정할 수 있는 HttpSecurity http를 파라미터로 받는 메서드를 오버라이딩 해서 구현하면 된다.

인가와 인증 정책으로 설정을 나눴는데

인가에 대한 설정은 모든 요청에 대한  보안검사를 하고 인증은 form 로그인 방식을 사용하겠다고 하는 설정이다.

이렇게 설정을하고 어플리케이션을 실행시키면 스프링 시큐리티가 제공하는 로그인 페이지가 나온다.

 

초기에 개발을 할때 스프링 시큐리티를 사용하면 시큐리티가 제공하는 UUID 형식의 비밀번호를 서버 로그에서 알려주는데 자릿수가 길고 또 항상 복사해서 로그인을 해야하기 때문에 간단하게 application.properties나 yaml파일에 id나 password를 설정해 줄 수 있다.

....

Using generated security password: fb067f68-0e3a-449f-a6af-2b9eacc53dfa

This generated password is for development use only. Your security configuration must be updated before running your application in production.

....

설정파일

spring.security.user.name=user
spring.security.user.password=1111

ETC

스프링 시큐리티 5.7대 버전부터 WebSecurityConfigureAdapter는 Deprecated 되었다.

WebSecurityConfigureAdapter를 상속받아 오버라이딩으로 구현하는 것이 아니라 SecurityFilterChain 타입의 빈을 생성하는 것으로 대체한다고 한다.

https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter

 

Spring Security without the WebSecurityConfigurerAdapter

<p>In Spring Security 5.7.0-M2 we <a href="https://github.com/spring-projects/spring-security/issues/10822">deprecated</a> the <code>WebSecurityConfigurerAdapter</code>, as we encourage users to move towards a component-based security configuration.</p> <p

spring.io

 

참고

https://www.inflearn.com/course/%EC%BD%94%EC%96%B4-%EC%8A%A4%ED%94%84%EB%A7%81-%EC%8B%9C%ED%81%90%EB%A6%AC%ED%8B%B0/dashboard

 

스프링 시큐리티 - Spring Boot 기반으로 개발하는 Spring Security - 인프런 | 강의

초급에서 중.고급에 이르기까지 스프링 시큐리티의 기본 개념부터 API 사용법과 내부 아키텍처를 학습하게 되고 이를 바탕으로 실전 프로젝트를 완성해 나감으로써 스프링 시큐리티의 인증과

www.inflearn.com

 

728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함