Practice/Java

스프링)시큐리티

밍미a 2022. 12. 30. 02:18
728x90

오늘 공부한 내용

'Spring Security' 프레임워크는 스프링 서버에 필요한 인증 및 인가를 위해 많은 기능을 제공해 준다.

시큐리티를 사용하지 않으면 자체적으로 세션을 체크해야 하고 리다이렉트를 일일이 설정해주어야 한다.

스프링 시큐리티 아키텍쳐

스프링 시큐리티는 서블릿 필터를 기반으로 동작하는데, 요청이 들어오면 인증과 권한을 위한 필터들을 통과한다.

  • HTTP 기본 인증을 요청하면 BasicAuthenticationFilter를 통과한다.
  • HTTP Digest 인증을 요청하면 DigestAuthenticationFilter를 통과한다.
  • 로그인 폼에 의해 요청된 인증은 UseerPasswordAuthenticationFilter를 통과한다.
  • x509 인증을 요청하면 X509AuthenticationFilter를 통과한다.

1. 유저 자격을 기반으로 인증 토큰을 만든다.

  • username과 password를 요청으로 추출하여 유저 자격을 기반으로 인증 객체를 생성한다.
  • username과 password는 UsernamePassworkdAutehnticationToekn을 만드는데 사용된다.

2. Filter를 통해 AuthenticationToken을 AuthenticationManager에 위임한다.

  • UsernamePasswordAuthenticationToken오브젝트가 생성된 후, AuthenticationManager의 인증 메소드를 호출한다.
  • AuthenticationManager는 인터페이스로 정의되어있다.
  • 실제 프로그래밍 구현에서 AuthenticationManager를 사용하기 위해 설정에 AuthenticationManagerBuilder를 이용해 아래에 정의된 UserServiceDetails를 매핑시켜준다.
  • ProviderManager는 AuthenticationProvider를 구성하는 목록을 갖는다.
  • ProviderManager는 AuthenticationProvider에 각각의 목록들을 제공하고, password Authentication 객체를 기반으로 만들어진 인증을 시도한다.

3. AuthenticationProvider의 목록으로 인증을 시도한다.

  • 인증을 위해 제공하는 것들.
  • CasAuthenticationProvider
  • JaasAuthenticationProvider
  • DaoAuthenticationProvider
  • OpenIDAuthenticationProvider
  • RememberMeAuthenticationProvider
  • LdapAuthenticationProvider

4. UserDetailsService는 username 기반의 user details를 검색한다.

5. UserDetails를 이용해서 User객체에 대한 정보를 검색한다.

  • UserDetailsService는 인터페이스이므로, 우리가 인증하고자하는 비즈니스로직을 정의한 serivce레이어에서 구현을 실행하는 방식을 이용한다.

6. User객체의 정보들을 UserDetails가 UserDetailsService(LoginService)에 전달한다.

  • 데이터베이스에서 User 객체에 매핑된 정보를 가져와 UserDetailsService(LoginService)에 전달한다.
  • 전달된 User 객체의 정보와 사용자가 요청한 인증 정보(username, password)를 확인하는 로직을 LoginService에 구현한다.

인증 객체 또는 AuthenticationException

  • 유저의 인증이 성공하면, 전체 인증정보를 리턴한다.
    • 인증에 실패하면 AuthenticationException을 던진다.
  • 인증에 성공한 객체의 정보
    • authenticated - true
    • grant authorities list : 권한 정보
    • user credentials(username only) : username으로 인증된 사항
  1. 인증 끝
    • AuthenticationManager는 완전한 인증(Fully Populated Authentication)객체를 Authentication Filter에 반환한다.
  2. SecurityContext에 인증 객체를 설정한다.
    • AuthenticationFilter는 인증 객체를 SecurityContext에 저장한다.

https://twer.tistory.com/entry/Security-%EC%8A%A4%ED%94%84%EB%A7%81-%EC%8B%9C%ED%81%90%EB%A6%AC%ED%8B%B0%EC%9D%98-%EC%95%84%ED%82%A4%ED%85%8D%EC%B2%98%EA%B5%AC%EC%A1%B0-%EB%B0%8F-%ED%9D%90%EB%A6%84

 

[Security] 스프링 시큐리티의 아키텍처(구조) 및 흐름

Spring Security 스프링 시큐리티리란? 어플리케이션의 보안(인증 및 권한)을 담당하는 프레임워크 Spring Security를 사용하지 않으면 자체적으로 세션을 체크해야 한다. redirect를 일일이 설정해주어야

twer.tistory.com

https://twer.tistory.com/entry/Security-%EB%A1%9C%EA%B7%B8%EC%9D%B8%EA%B3%BC-%EA%B6%8C%ED%95%9C-%EC%84%A4%EC%A0%95

 

[Security] 로그인과 권한 설정

주의 : Spring Security 5.7+ (Spring Boot 2.7+)부터는 WebSecurityConfigurer이 Deprecated 되었다. 2022.09.05 - [Spring/Security] - [Security] WebSecurityConfigurerAdapter Deprecated [Security] WebSecurityConfigurerAdapter Deprecated 버전 Java 11 S

twer.tistory.com

https://twer.tistory.com/entry/JWT-%ED%9A%8C%EC%9B%90%EA%B0%80%EC%9E%85-%EB%A1%9C%EA%B7%B8%EC%9D%B8-%ED%86%A0%ED%81%B0-%EB%B0%9C%EA%B8%89-1

 

[JWT] 회원가입 / 로그인 / 토큰 발급 (1)

목표 회원가입 기능 로그인 기능 로그인 시 JWT 토큰 발급 JWT 인증, 인가 필터 생성 JWT의 회원 정보 가져오기 환경 java 17 spring boot 2.7 gradle 7.5 환경 설정 1. build.gradle - Web, Security, Lombok, JPA, mysql-conne

twer.tistory.com


더 공부할 내용

시큐리티의 활용방법, 데이터가 흘러가는 흐름...

동작구조

 


느낀점

이틀 내내 시큐리티를 공부했는데.. 시큐리티가 필터기반으로 동작한다는 것 정도밖에 이해를 하지 못했다.

어떻게 동작하는지 데이터가 어떻게 흘러가는지에 대해 이해가 잘 되지 않는다.

클래스의 이름은 또 왜이렇게 긴건지... 필터는 또 왜이렇게 많은건지.. 개발자들은 이걸 다 이해하고 외우고 있나..?