티스토리 뷰

개발일기

20190304

Jinhyy 2019. 3. 4. 18:00

1. Security

-인증순서-

a. 유저정보 가져오기(UserDetailsService.loadUserByUsername() 등)

b. AuthenticationProvider 에서 authenticate(Authentication authentication)메소드로

가지고온 디비유저정보와 authenticate(Authentication authentication)로 

접속한유저 정보(authentication)를 비교하여 비밀번호가 같은지 비교하게되고 

인증에 성공하게되면 Authentication객체 를 돌려준게된다.


--> 아이디/패스워드 사용자 정보를 넣고 실제 가입된 사용자인지 체크한후 

인증에 성공하면 사용자의 principal과 credential 정보를 Authentication에 담습니다.


**

디비에서 유저정보를 불러오려면 UserDetailsService interface를 직접 구현하면 된다.

비밀번호 비교를 직접 구현하려면 AuthenticationProvider 구현하면 된다.

AuthenticationProvider을 따로 구현안해도 UserDetailsService로 유저정보를 추가할수 있다.


c. Spring Security에서 방금 담은 Authentication을 SecurityContext에 보관한다.

d. 이 SecurityContext를 SecurityContextHolder에 담아 보관하게 됩니다.


2. Security 정리

결론

스프링 시큐리는 크게 2가지 인증/인가 처리를 도와준다. (AuthenticationProvider,UserDetailsService)

o.s.security.core.Authentication은 시큐리티에서 접근한 외부에서 접근한 정보를 보관하는 역할을한다.(이름,권한,인증여부등을 조회함)

이러한 Authentication 정보는 o.s.s.core.context.SecurityContextHolder안에서 가지고온다.

그리고 Authentication의 정보는 최초 SecurityContextHolder 맨처음에 접근하는 세션이 이곳에 먼저 저장되어져 위에와같은 로직이 진행되는 것이다.

이것들이 필터체인에 순서대로적용된다. 이순서가 필터중에서 가장먼저 실행됨.

최종적으로 SecurityContextHolder.getContext().getAuthentication()으로 유저 정보를 불러오게 된다.

스프링 시큐리가 제공해주는 authentication-provider을 이용해 인증을 진행하게되는데 유저 인증은 UserDetailsService에 UserDetails객체를 이용해 인증을 진행하게 된다.

을 사용하게되면 인증을 알아서 진행하는듯, 단 유저정보를 가지고올수있는 제약이걸림 사실상 사용못함(userdetailsserver을 구현해서 사용해야함).

그렇게 UserDetailsService에서 데이터를 가지고와서 DaoAuthenticationProvider에서 인증과정을 거친다.(UserDetailsService의 loadUserByUsername메소드를 이용함)

만약에 UserDetailsService를 커스텀 한다면 loadUserByUsername에서 회원정보를 꼭리턴해줘야한다. 왜냐하면 아까 말했듯이 이것을 통해 사용자가 입력한 암호를 비교 하기때문이다.



Authentication 객체

--> 인증에 성공한 유저의 정보가 들어있어야 한다.

--> SecurityContext, SecurityContextHolder 안에서 찾을 수 있게 된다.


AuthenticationProvider 객체

--> authenticate 메소드를 통하여 현재 접속시도하는 계정정보를

UserDetails.loadByUserName() 메소드로 DB의 계정정보와 비교하여

인증에 성공할 시, Authentication 객체를 리턴해준다.


Jwts 객체

Authentication 객체를 이용하여 UserPrincipal 객체를 얻어온 후,

jwt 토큰 객체를 생성한다.


ReponseEntity 객체

--> Rest한 응답을 줄때 사용하는 객체로, 상태코드,헤더,바디를

설정하여 리턴할 수 있다.


3. http Options 메소드 

--> 해당 웹 요청이 처리할 수 있는 메소드를 확인


4. 정적 내부 클래스 vs 그냥 내부 클래스

static 내부 클래스는 바깥 쪽 클래스의 객체가 생성되지 않아도 바로 접근가능하다.


'개발일기' 카테고리의 다른 글

20190308  (0) 2019.03.08
20190306  (0) 2019.03.06
20190305  (0) 2019.03.05
20190228  (0) 2019.02.28
20190227  (0) 2019.02.27
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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 29 30
글 보관함