티스토리 뷰

TIL(Today I Learn)

WIL 3주차(Week I Learn)

minji_6119 2022. 5. 30. 14:56

웹의 인증 및 인가의 개념

  • 인증 (Authentication): 사용자 신원을 확인하는 행위
  • 인가 (Authorization): 사용자 권한을 확인하는 행위

쿠키와 세션

쿠키와 세션 모두 HTTP 에 상태 정보를 유지(Stateful)하기 위해 사용됩니다. 즉, 쿠키와 세션을 통해 서버에서는 클라이언트 별로 인증 및 인가를 할 수 있게 됩니다.

  1. 쿠키
    • 클라이언트에 저장될 목적으로 생성한 작은 정보를 담은 파일 입니다.
  2. 세션
    • 서버에서 일정시간 동안 클라이언트 상태를 유지하기 위해 사용
    • 서버에서 클라이언트 별로 유일무이한 '세션 ID' 를 부여한 후 클라이언트 별 필요한 정보를 서버에 저장
    • 서버에서 생성한 '세션 ID' 는 클라이언트의 쿠키값('세션 쿠키' 라고 부름)으로 저장되어 클라이언트 식별에 사용됨
    • 클라이언트에 쿠키 값이 저장

'정보통신망법, 개인정보보호법' 에 의해 비밀번호는 암호화(Encryption)가 의무!

암호화 후 패스워드 저장이 필요.

  • 평문 → (암호화 알고리즘) → 암호문
@Service
public class UserService {
    private final PasswordEncoder passwordEncoder;
    private final UserRepository userRepository;
    private static final String ADMIN_TOKEN = "";

    @Autowired
    public UserService(PasswordEncoder passwordEncoder, UserRepository userRepository) {
        this.passwordEncoder = passwordEncoder;
        this.userRepository = userRepository;
    }

user의 password를 다룰 때 암호화가 필요하므로 Service의 생성자에 추가. UserService에 @RequireArgsConstroctor 어노테이션 사용하면 @Autowired 과정 생략 가능

 

스프링에서 DI (의존성 주입) 를 사용하는 이유가 무엇인가요?

    • 빈 (Bean): 스프링이 관리하는 객체
    • 스프링 IoC 컨테이너: '빈'을 모아둔 통

그런데 DI 를 사용하기 위해서는 객체 생성이 우선 되어야 했다. 바로 스프링 프레임워크가 필요한 객체를 생성하여 관리하는 역할을 대신해 준다

 

스프링 '빈' 등록 방법

  1. @Component
@Component
public class ProductService { ... }

@Component 클래스에 대해서 스프링이 해주는 역할은

 

1. ProductService 객체 생성

ProductService productService = new ProductService();

2. 스프링 서버가 뜰 때 스프링 IoC 에 '빈' 저장

스프링 IoC 컨테이너에 Bean (productService) 저장

 

    2. @Bean

  • 직접 객체를 생성하여 빈으로 등록 요청
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfiguration {

    @Bean
    public ProductRepository productRepository() {
        String dbUrl = "jdbc:h2:mem:springcoredb";
        String dbId = "sa";
        String dbPassword = "";

        return new ProductRepository(dbUrl, dbId, dbPassword);
    }
}

 

  • 스프링 서버가 뜰 때 스프링 IoC 에 '빈' 저장

1. @Bean 설정된 함수 호출
ProductRepository productRepository = beanConfiguration.productRepository();

2. 스프링 서버가 뜰 때 스프링 IoC 에 '빈' 저장
스프링 IoC 컨테이너에 빈 (productRepository) 저장

 

스프링 '빈' 사용 방법

1. @Autowired: 스프링 IoC 컨테이너에 의해 관리되는 클래스에서만 가능

  • 멤버변수 선언 위에 @Autowired → 스프링에 의해 DI (의존성 주입) 됨
@Component
public class ProductService {
		
    @Autowired
    private ProductRepository productRepository;

}
  • '빈' 을 사용할 함수 위에 @Autowired → 스프링에 의해 DI 됨
@Component
public class ProductService {

    private final ProductRepository productRepository;

    @Autowired
    public ProductService(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }
		
		// ...
}

2. @RequiredArgsConstructor 

@RequiredArgsConstructor // final로 선언된 멤버 변수를 자동으로 생성합니다.
@RestController // JSON으로 데이터를 주고받음을 선언합니다.
public class ProductController {

    private final ProductService productService;
    
    // 생략 가능
		// @Autowired
		// public ProductController(ProductService productService) {
		//     this.productService = productService;
		// }
}

생성자 선언이 1개 일 때만 생략 가능

 

3. ApplicationContext: 스프링 IoC 컨테이너에서 빈을 수동으로 가져오는 방법

@Component
public class ProductService {
    private final ProductRepository productRepository;

    @Autowired
    public ProductService(ApplicationContext context) {
        // 1.'빈' 이름으로 가져오기
        ProductRepository productRepository = (ProductRepository) context.getBean("productRepository");
        // 2.'빈' 클래스 형식으로 가져오기
        // ProductRepository productRepository = context.getBean(ProductRepository.class);
        this.productRepository = productRepository;
    }

		// ...		
}

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

TIL 220602  (0) 2022.06.02
TIL 220531  (0) 2022.06.01
TIL 220527  (0) 2022.05.27
TIL 220526  (0) 2022.05.26
TIL 220520  (0) 2022.05.21
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함