본문 바로가기
2023 활동 - 4학년/[1월 ~ 4월] sw 아카데미 백엔드 과정

Spring Security Quickstart D-36

by 은행장 노씨 2023. 4. 1.

보안과 관련된 프로젝트이다. 보안은 설계에서 가장 중요한 부분 중 하나라고 생각한다. 

 

Spring Security

Spring Security를 이용해서 어떤 문제를 해결할 수 있을까?

1. 견고한 인증 프로세스를 만들 수 있다. 

인증과 인가처리만 제대로 해도 보안과 관련된 기본적인 것을 다 했다.

인증 - 사용자의 신원을 확인하는 과정

출처 : 프로그래머스

익명/ 인증 영역이 있다. 

익명은 사용자의 신원과 무관한 일만 해야한다. 예 ) 판매 상품 목록 전시

인증 영역 - 사용자 개인 정보 확인 및 변경 예 ) My Page

 

2. 권한 부여 및 권한 확인 제공

이런 일들을 인가 처리라고 하다. 

 

3. 크리덴셜(Credential) 보안

사용자의 개인 정보를 안전하게 보관해야 한다. 항상 최우선순위

민감 정보를 저장할 때 암호화를 해야한다.

-> 2차 피해를 지켜야 한다. 

 

4. 전송 레이어 보안

웹 서비스에서 SSL 보호를 적용하지 않는 것은 존재하지 않는다. 

주소창에 자물쇠 표시 

 

인증과 인가처리를 사용하는 서비스라면, 반드시 Spring Security 사용해야 한다. 

다양한 확장 기능과 자연스러운 통합이 가능하다. 

 


실습

1. 의존성 추가

//xml

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

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-test</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.thymeleaf.extras</groupId>
  <artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
// Configures > WebSecurityConfigure.java

@Configuration
@EnableWebSecurity
public class WebSecurityConfigure extends WebSecurityConfigurerAdapter {

  @Override
  public void configure(WebSecurity web) {
    web.ignoring().antMatchers("/assets/**");
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
        .antMatchers("/me").hasAnyRole("USER", "ADMIN")
        .anyRequest().permitAll()
        .and()
      .formLogin()
        .defaultSuccessUrl("/")
        .permitAll()
    ;
  }

}

 

  • ignoring()
    • 필요 없는 기능을 무시함, 일반적으로 정적 리소스 무시
    • FilterChainProxy는 Spring Security 진입점과 같은 역할을 한다. 
    • 효율성 측면에서 장점을 얻을 수 있다. 
  • Thymeleaf 확장
 

GitHub - thymeleaf/thymeleaf-extras-springsecurity: Thymeleaf "extras" integration module for Spring Security 3.x and 4.x

Thymeleaf "extras" integration module for Spring Security 3.x and 4.x - GitHub - thymeleaf/thymeleaf-extras-springsecurity: Thymeleaf "extras" integration module for Spring Secu...

github.com