포트폴리오/SPRINGBOOT 옛날

3-05 : spring security

서버관리자 페페 2022. 10. 9. 10:23

단 하나의 맥락

: 스프링의 하위 프레임워크인 SpringSecurity를 이용하여 Authenticate(인증-로그인) 및 Authorize(권한 분배)를 추가한다

 


Semiflow Bundle

: gradle에 시큐리티 설치

: 로그인 없이 게시물 조회 가능 하도록 SecurityConfig(신규 작성) 에서 설정

: H2-console 예외 처리

 


 

build.gradle에 설치

> build.gradle의 dependencies에서

 

  • implementation 'org.springframework.boot:spring-boot-starter-security'
  • implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'

 

refresh 및 로컬서버 재시작

 

 

 

-

 

security 설정으로 비로그인에도 게시물 조회 가능하도록 authorize

 

 

> SecurityConfig 신규 작성

 

// import org.springframework.context.annotation.Configuration;

// import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

 

@Configuration

@EnableWebSecurity

public class SecurityConfig {} 클래스 내부에

 

 

 

// import org.springframework.context.annotation.Bean;

// import org.springframework.security.web.SecurityFilterChain;

// import org.springframework.security.config.annotation.web.builders.HttpSecurity;

 

 

@Bean

public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {}

  • A1 : http.authorizeRequests().antMathcers("/**").permitAll()
  • ;
  • return http.build();

 

 

 

-

 

H2-console CRSF 검증 해제, XFrameOption 설정

 

 

> SecurityConfig에서

 

  • http.authorizeRequests().antMatchers("/**").permitAll()와
  • ; return http.build(); 사이에

 

  • .and()
  •     .csrf().ignoringAntMatchers("/h2-console/**")

를 넣어 crsf 검증을 하지 않도록 한다

 

 

 

> SecurityConfig에서

 

  • .and()
  •     .csrf().ignoringAntMatchers("/h2-console/**") 하단에

 

// import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;

 

  • .and()
  •     .headers()
  •     .addHeaderWriter(new XFrameOptionsHeaderWriter (
  •         XFrameOptionsHeaderWriter.XFrameOptionSMode.SAMEORIGIN))

를 넣는다

 

 

 

 

 

 

package com.mysite.sbb;

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.web.SecurityFilterChain;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/**").permitAll()
            .and()
                .csrf().ignoringAntMatchers("/h2-console/**")
            .and()
                .headers()
                .addHeaderWriter(new XFrameOptionsHeaderWriter(
                        XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))
            ;
        return http.build();      
    }
}

 

'포트폴리오 > SPRINGBOOT 옛날' 카테고리의 다른 글

3-07 : 로그인과 로그아웃  (0) 2022.10.09
3-06 : 회원가입  (0) 2022.10.09
3-04 : 답변 갯수 표시  (0) 2022.10.09
3-03 : 일련번호 추가 수정  (0) 2022.10.09
3-02 : paging  (0) 2022.10.06