ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 2022-08-02 : Spring
    TIL ( Today I Learned ) 2022. 8. 2. 11:12

    1. Spring  Security

    1 ) SecurityContextHolder, SecurityContext, Authentication

    1. 아이디 / 패스워드 사용자 정보를 넣고 실제 가입된 사용자인지 체크한 후 인증에 성공하면 사용자의 principal과 credential 정보를 Authentication에 담는다.

    2. Spring Security에서 Authentication을 SecurityContext를 보관

    3. SecurityContext를 SecurityContextHolder에 담아 보관

    2 ) Spring Security에서 인증 처리를 코드로 풀면

    1. username과 password를 조홥해서 UsernamePasswordAuthenticationToken 인스턴스를 만든다

    2. 이 토큰( UsernamePasswordAuthenticationToken)은 검증을 위해 AuthenticationManager의 인스턴스로 전달

    3. AuthenticationManager 는 인증에 성공하면 Authentication 인스턴스를 리턴

    4. 이 authentication 을 SecurityContextHolder.getContext().setAuthentication(...)에 set해준다

     

    참고 자료 : https://flyburi.com/584

     

    2. Spring boot - Task Execution and Scheduling

    1. Executor

    - command 를 실행할 때마다 새로운 스레드에 동작

    - coomand 는 Runnable 인터페이스 객체이다

    - Executor 가 실행될 때는 쓰레드를 명시하지 않는다고 한다. 그 역할은 Runnable 인터페이스가 대신한다

    - executor.execute(new RunnableTask()); 이런식으로 command를 실행 시킨다.

     

    Runnable Interface

    - 자바에서 Thread를 구현할 때 Thread 클래스를 사용하거나 Runnable 인터페이스를 사용할 수 있다

    - 두 방법의 차이는 다중 상속이 가능한지에 대한 내용이며, 상황에 맞게 사용하는 것이 좋다

    - Thread 클래스를 사용하는 경우 다중 상속이 불가능하며, Runnable 인터페이스를 사용하는 경우 다중 상속이 가능하다

    - Runnable 인터페이스의 구현체가 create a thread를 할때, 구현체의 run 메서드를 실행시킨다.

     

    2. ThreadPoolTaskExecutor

    - Spring 에서 쓰레드를 관리해주는 역할을 하며, Executor를 최상위 인터페이스를 가지고 있다

    - 자바의 ThreadPoolExecutor를 사용하기 쉽게 만들어 사용할 수 있도록 해주는 일종의 Wrapper 클래스라고 보면 편하다

    - ThreadPoolTaskExecutor 클래스는 AsyncListenableTaskExecutor, SchedulingTaskExecutor 인터페이스를 상속받는다

    - 두 인터페이스 모두 AsyncTaskExecutor 인터페이스를 extends 하고 있다

    - 그리고 AsyncTaskExecutor 인터페이스는 Executor 인터페이스를 extends하고 있는 TaskExecutor 인터페이스를 extends 하고 있다.

     

    AsyncListenableTaskExecutor

    - add submit 할 때, ListenableFutures 기능을 추가하기 위해 만들어진 인터페이스

    - ListenableFutures는 Executor에 Task를 전달하고 돌려받는 Future타입의 종류 중 하나이다.

    - Runnable 인터페이스 객체인 task를 던지는 것을 볼 수 있다.

     

    SchedulingTaskExecutor

    - 반복된 루프를 개별 하위 작업으로 분할하여 이후 후속 작업을 submit 할 수 있다.

     

    AsyncTaskExecutor

    - TaskExecutor를 상속한 인터페이스이다

    - submit 메서드와 timeout 기능이 추가

    - 스프링에서는 @EnableAsync 어노테이션을 추가하면 디폴트로 SimpleAsyncTaskExecutor 구현체를 사용한다

    - SimpleAsyncTaskExecutor 객체는 매번 Thread를 생성하는 객체이기 때문에 쓰레드 풀내에서 지정된 갯수만큼만 실행시키고 싶다면 ThreadPool 설정 필요

    @Configuration
    @EnableAsync
    public class AsyncConfig extends AsyncConfigurerSupport {
    
    @Override
    public Executor getAsyncExecutor() {
      ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
      executor.setCorePoolSize(7);
      executor.setMaxPoolSize(42);
      executor.setQueueCapacity(11);
      executor.setThreadNamePrefix("MyExecutor-");
      executor.initialize();
      return executor;
    }
    }
    출처: https://wave1994.tistory.com/165 [훈훈훈:티스토리]

    - 스프링 부트에서는 아래와 같이 프로퍼티로 ThreadPoolTaskExecutor를 설정할 수 있다

    - 왜냐하면 Executor 객체가 Bean으로 등록되지 않았다면 ThreadPoolTaskExecutor 객체를 auto-configure하기 때문

     

    참고 자료 :

    1. https://wave1994.tistory.com/165

    2. https://keichee.tistory.com/382

     

    'TIL ( Today I Learned )' 카테고리의 다른 글

    2022-08-05 : JS  (0) 2022.08.05
    2022-08-03 : Spring  (0) 2022.08.03
    2022-08-02 : JAVA  (0) 2022.08.02
    2022-08-01 : webpack & React  (0) 2022.08.01
    2022-07-29 : JAVA & Spring  (0) 2022.07.29

    댓글

Designed by Tistory.