전체 글
-
ServletInputStream은 1번만 읽을 수 있다프레임워크/Spring Boot 2024. 5. 8. 23:36
아래의 글은 다음과 같은 순서로 작성되었습니다.1. 예외의 발생2. 공식 문서에서 단서 찾기3. BufferedReader 동작 확인하기4. 그래도 2번 읽고 싶다면?5. 정리하기 1. 예외의 발생회원가입을 Spring Security의 필터 체인을 사용해서 구현 하고 있습니다. 첫 번째 필터에서 input에 대한 validation 처리를 위해서 request에서 body 정보를 읽습니다. 적절한 요청은 두 번째 필터로 전달을 하고, 회원가입 로직을 가지고 있는 서비스를 주입받아서 사용하려고 합니다.이 때, 필터 간의 데이터 전달을 위한 별도의 dto를 만들지 않기 위해서 기존의 리퀘스트에서 한번 더 바디를 읽으려고 합니다. 하지만 기대와 다르게 예외가 발생합니다? 2. 공식 문서에서 단서 찾..
-
필터에서 발생하는 예외 처리하기프레임워크/Spring Boot 2024. 5. 7. 23:45
Http 통신 중에 발생하는 예외를 처리하던 중 만난 이슈를 적어봅니다.필터에서 발생하는 예외 처리에 관한 내용입니다. 1. 예외가 발생하는 상황인증서버에서 유저 서버로부터 유저 정보를 가져오는 코드의 일부입니다.3개의 catch 문을 가지고 있는데 각각 매칭되는 유저가 없는 경우, 유저 서버의 동작에 문제 있을 때 그리고 그 외의 모든 문제의 경우를 나눠서 처리하고 있습니다. 문제의 발생은 이렇습니다.유저 서버를 실행하지 않은 상태로 ResourceAccessException을 발생시키고 적절한 처리를 하려고 ControlleAdvice를 정의해주었습니다. 하지만 advice에 걸리지 않고 여전히 프론트에서 에러 메세지를 바로 보는 상황이 발생합니다. 왜 예외를 못잡는가 고민하던 중 ...컨트롤러 어..
-
다양한 Oauth2 Provider 들을 다룰 수 있는 코드 만들기운영 중인 서비스/Coconut. 2024. 5. 7. 20:08
Oauth2 관련 작업을 하면 어딘가 비슷한 패턴이 계속 반복되기 때문에 어떻게 코드를 써야될지 고민을 많이 하게됩니다.이번에는 작성해둔 코드가 어딘가 아쉬움이 느껴져서 나름대로 개선을 해봅니다.사용측 코드가 완성된 상태가 아니기 때문에 흐름을 참고하는 용도로 봐주시면 좋겠습니다. 이번 글은 다음의 순서로 작성되었습니다.1. 개선 이전의 코드2. 개선 이후의 코드3. 정리하기 1. 개선 이전의 코드개발자 유미님의 채널에 스프링 시큐리티 관련한 구현 영상들이 많이 있어서 참고를 하였습니다 ( 개발자 유미 채널 )Oauth2 로그인 과정에서 User 관련 로직을 커스텀하기 위해서 CustomOauth2UserService를 만들어보았습니다.인터페이스만 상속받아서 구현할까 상속을 받을까 고민을 해보았는데, ..
-
Spring Security 다이어그램으로 flow 정리하기프레임워크/Spring Boot 2024. 5. 7. 02:15
이번에는 충분한 시간가지고 학습할 수 있는 상황이기 때문에 구현에 급급하기 보다는 이해를 하면서 가는 방향을 선택해봅니다 ㅎㅎ디버거로 열심히 따라다니면서 그려보았습니다. 덕분에 흐름이 머릿 속에 잡힌 것 같아 만족스럽습니다 ㅎㅎ 1. ID/PW를 사용해서 로그인 유저의 입력을 기반으로 Authentication을 구현하는 객체를 만들어서 사용합니다.서버 측의 유저 정보를 UserDetails 형태로 준비합니다.둘을 비교하여 적절한 id / pw를 유저가 입력했는지 확인합니다. 노란색으로 표시된 클래스는 커스터마이징이 된 클래스입니다. DaoAuthenticationProvider에서 유저가 입력한 id/pw 와 서버 측의 유저 정보를 비교합니다. 2. Oauth를 사용하는 로그인 기본적으로 Auth..
-
SecurityFilterChain을 여러개 쓰고자 한다면 securityMatcher!프레임워크/Spring Boot 2024. 5. 3. 17:43
스프링 문서를 돌고 돌다 찾은 키워드를 기록하고자 글을 적습니다. 시큐리티 공식 문서에 보면 경로에 맞는 적절한 필터체인을 구현해서 쓰라고 안내합니다. 하지만 어떻게가 빠져있습니다..밑으로 내려가면 Order를 정해서 사용할 수 있다고는 하나, Order를 사용해서 FilterChain을 구분하는 방법은 경로를 분리하기 위함이라기 보다는, 동일한 경로 내에서 우선순위를 정하는 용도가 아닐까 합니다. 그래서 돌고 돌다가 문서의 다른 곳에서 securityMatcher 메소드를 찾았습니다.3번에 보면 applicable only to 라는 문구가 있군요. 그리하여 id/pw 로그인 경로와 Oauth 로그인 경로를 분리해보았습니다.이후에 Oauth 관련 설정 내용이 많아진다면 Oauth Config는..
-
로그인을 어디서 처리할 것인가? 필터 vs 컨트롤러운영 중인 서비스/Coconut. 2024. 5. 2. 23:41
인증 서버에서 로그인을 구현하면서 고민한 내용을 적어봅니다.최종적으로 구현은 필터 위에 작업이 되었으나, 다시 한다면 컨트롤러에서 진행할 것 같습니다 ㅎㅎ 아래의 글은 다음의 순서로 진행됩니다.1. 필터 기반의 로그인 구현2. 필터 체인 구성3. 필터를 쓰는 것이 맞는 것인가? 4. 다시 만든다면? 코코넛 서비스의 주요 로그인은 Oauth를 사용할 예정이지만, 특별한 사용자들의 접근을 위해서 아이디/패스워드 형식의 로그인도 기능을 만들어 두었습니다. Spring Security의 기능을 최대한 사용하는 방향에서 작업을 하고, 인증 서버는 요청량이 많을 것을 감안해서 성능상에 이점을 가져갈 수 있는 방법을 선택해서 구현하였습니다. 1. 필터 기반의 로그인 구현Security Conifg 파일을 먼저 살..
-
RestTemplate는 응답 객체를 LinkedHashMap으로 파싱한다프레임워크/Spring Boot 2024. 5. 2. 15:00
RestTemplate으로 User서비스에서 데이터를 받아오는 과정에서 발생한 이슈를 정리합니다. 몇 줄 안되는 간단한 코드에서 생각보다 많은 케이스를 만나고 있습니다 ㅎㅎ분명히 응답이 잘 오는 것을 result 를 log로 찍어서 확인했는데. catch 문이 실행됩니다? 저의 응답 객체의 모양은 이렇습니다.성공시 result에 데이터가 담기고, 실패시 에러에 데이터가 담겨서 전달됩니다. 그런데 getResult() 를 하면 java.lang.ClassCastException 예외가 발생합니다.예외를 살펴보면 LinkedHashMap을 AuthUserDetails로 캐스트 할 수 없다고 합니다. 첫 depth의 CustomResponse 는 잘 파싱이 되었는데, 두번째 depth는 주어진 타입이아..
-
RestTemplate은 4xx를 예외로 처리한다.프레임워크/Spring Boot 2024. 5. 2. 02:25
RestTemplate 사용 중에 서버로부터 응답이 반환되어도 예외가 발생하는 것이 낯설어 기록하여봅니다. 다음의 순서로 작성되었습니다.1. 응답 객체의 형태2. 404를 반환 받을 때 1. 응답 객체의 형태현재 User 서버는 Request를 받아서 실행 중에 이슈가 생기면 advice를 통해서 적절한 응답을 반환하고 있습니다. 성공 케이스와 실패 케이스 모두 동일한 인터페이스를 사용해서 반환하고 있습니다.성공인 경우 result에 데이터가 들어가고, 예외가 발생한 경우는 error에 에러 객체를 넣어서 전달합니다. 2. 404를 반환받을 때이상함을 느낀 코드는 아래와 같습니다.restTemplate을 사용해서 User 데이터를 가져오려고 하고 있습니다. 요청한 유저가 존재하지 않는 경우를 처리하..