[02.09] 내일배움캠프[Spring] TIL-68

박상훈·2023년 2월 9일
0

내일배움캠프[TIL]

목록 보기
68/72

[02.08] 내일배움캠프[Spring] TIL-67

1. Spring Project

오늘 한 일

    1. 새로 작업한 와이어프레임을 바탕으로 UML, ERD 새로 작업했다.
    1. 새로 작업한 내용을 바탕으로 본격 프로젝트에 들어갈 구조를 바탕으로 깃과 프로젝트를 생성했다.



오늘 배운 상식과 팁!

  • aplication.properties와 같이 설정파일은 호스팅 하기전 로컬 서버로 돌리게 되면 mysql, h2등이 설정이 개인마다 다르기 때문에 항상 변경사항으로 취급된다.
  • 따라서 Git.ignore에 추가해서 커밋을 막아준다.
  • 보안상의 이유도 있다!

  • 우리 프로젝트 같은 경우나, 현 트렌드가 프론트 개발자 VsCode 작성과 , SpringBoot를 사용한 백엔드 서버의 통합으로 이뤄지는데, 이 설정을 Config파일로 풀어주는 방법을 배웠다.
package com.sparta.soomtut.config;

import com.sparta.soomtut.util.jwt.JwtProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig implements WebMvcConfigurer {
    private final JwtProvider jwtProvider;

    public static final String ALLOWED_METHOD_NAMES = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,PATCH";

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring()
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations());
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.httpBasic().disable()
                .csrf().disable()
                .formLogin().disable()
//                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeHttpRequests()
                .requestMatchers("/").permitAll()
                .anyRequest().authenticated();

        return http.build();
    }

    @Override
    public void addCorsMappings(final CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods(ALLOWED_METHOD_NAMES.split(","))
                .allowedOrigins("http://localhost:3000");
    }
}
  • PostMan으로 ajax딴 스크립트 가져오기!

profile
기록하는 습관

0개의 댓글