자바/Spring 8

Spring Security CSRF와 CORS

CSRF(Cross Site Request Forgery) : 사용자가 웹에 로그인 한 상태에서 악성 스크립트 등을 통해 원하지 않는 변경(POST, PUT, DELETE)를 할 수 있는 것을 대비하기 위한 기능이다. (예시로, 페이스북에 로그인된 상태에서 원하지 않는 광고글이 게시되는 등) GET호출시에 CSRF 토큰을 발급하며, 변경(POST등) 메서드를 하고자 할 경우에는 헤더에 X_CSRF_TOKEN에 토큰값을 넘겨줘야 정상적으로 변경을 수행한다. 아래와 같이 hidden값으로 클라이언트에서 변경시 token값을 올려주도록 구현할 수 있다. Name: Add CORS(Cross-Origin Resource Sharing) : 보안상의 이유로 브라우저는 동일출처(도메인, 포트)가 아니면 호출을 제한..

자바/Spring 2023.09.17

Spring Security Authorization(권한 부여)

Authority : String형태(Read, Write 등)의 권한을 정의하거나 ROLE(Autority에는 ROLE_권한형태로 저장) 사용 AuthorizationFilter에서 AuthorizationManager(주로 AuthorityAuthorizationManager)에 의해 체크됨 mvcMatchers : *는 한 경로 이름 대체, **는 여러 경로이름 대체, 정규식 가능 antMatchers : 사용법은 유사하나 /hello 경로에 지정할 경우 /hello/ 경로는 제외된다(mvcMatcher는 둘다 포함됨) http.authorizeRequests() .mvcMatchers(HttpMethod.GET, "/a").authenticated() .mvcMatchers(HttpMethod.P..

자바/Spring 2023.09.17

Spring Security Authentication(인증)

Authentication(인증) : 사용자가 맞는지 확인 FormLogin FilterChainProxy FilterChainProxy에 보면 정의된 filters를 리스트로 가지고 있음 AuthenticationManager(인증관리자) : 주로 ProviderManager 사용됨 ProviderManager 는 다음 Build된 Provider를 이용해서 인증을 수행 OTP인증 등 인증방법을 변경하고 싶으면 Provider를 별도로 만들어서 추가해주자(HttpSecurity.authenticationProvider) SecurityContextHolder : 인증된 사용자 세부정보 저장하는 곳. 어떻게 채워지는지는 신경쓰지 않음 SecurityContext : Authentication 객체가 담겨..

자바/Spring 2023.09.17

Spring Security 3.1.1 적용

SecurityFilterChain : 기존에 2.x버전에서 WebSecurityConfigurerAdapter 상속받아 configure 재정의하던 방식에서 @Bean으로 생성하도록 변경되었다 @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http .csrf((csrf) -> csrf.disable()) .authorizeHttpRequests(request -> request.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() .requestMatchers(staticPath.toArray(new..

자바/Spring 2023.09.16

spring boot DB테스트, jdbcTemplate

spring boot에서 JdbcTemplate 사용 implementation 'org.springframework.boot:spring-boot-starter-jdbc' 위와 같이 jdbc 를 추가하고, @Configuration에 Bean을 추가하면 된다. private final DataSource dataSource; @Bean public ItemRepository itemRepository() { return new JdbcTemplateItemRepository(dataSource); } 실제 사용은 아래와 같이 public class JdbcTemplateItemRepositoryV1 implements ItemRepository { private final JdbcTemplate tem..

자바/Spring 2022.09.16

스프링 빈 주입

@ComponentScan 디폴트가 현재 클래스의 하위 패키지 검색하여 의존관계 자동 주입 (stereotype의 annotation 붙어 있는 Bean을 검색) Component 사용시 주의점 기본적으로 스프링의 Component는 싱글톤으로 생성되므로, Stateless하게 설계해야 한다. 즉, 컴포넌트로 들어갈 @Service, @Controller, @Repository 에는 클래스 변수 대신에 쓰레드에서 공유되지 않는 지역변수, 파라미터, ThreadLocal 등을 사용해야 함. 필드 변수값을 변경해서 사용하면 멀티쓰레드 환경에서 다른 쓰레드 값이 보여질 수 있음. @Autowired 컴포넌트 스캔하면서 자동으로 주입. 생성자가 하나이면 @Autowired 생략가능 테스트시 빈 주입방법1 : A..

자바/Spring 2022.06.10
반응형