랄라
[TIL 24일차]25.03.20 본문
게이트웨이에서 우선적으로 권한체크하는 인가처리 구현에 성공했다!!!!!
여러곳을 구글링하여 얻은정보로는
@EnableWebFluxSecurity 를 사용해야하는 Gateway에서는
ReactiveSecurityContextHolder를 사용하여 권한정보를 넣어주어야 하는것이었다!
처음에는 어떻게 써야하는건지 모르겠어서 . 으로 메서드를 찾아보니 withAuthentication()이라는 것이 존재했다.
뭔가 authentication을 넣어주는 기능을 할거같아서 한번 작성을해봤다.
private void setAuthenticationContext(Claims claims) {
String userId = claims.get("user_id", String.class);
String role = claims.get("role", String.class);
List<GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority(role));
Authentication authentication = new UsernamePasswordAuthenticationToken(userId, null, authorities)
ReactiveSecurityContextHolder.withAuthentication(authentication);
}
사실 이건 첫번째 실패코드이다... 약간 헤더에 값 넣는것처럼 이렇게 적으면 알아서 추가되리라 생각했는데 아니었다.
값이 들어가긴 한걸까? 싶어서 로그도 찍어봤는데 Mono? 라는 값이나왔다.
실패한 뒤에는 이게 자동설정되는게 아니라 리턴으로 전달을해야하나? 싶어서 코드를 수정해보았다.
return chain.filter(exchange).contextWrite(ReactiveSecurityContextHolder.withAuthentication(authentication))
Authentication 자체를 전달해주고 이렇게 필터체인을 반환할때 같이 넣어주면된다고해서 작성을해봤더니 성공했다!
이걸 구현하기위해 엄청많은시간이 소요되었다... 처음해보는거여서 더 시간이 오래걸린것 같기도하다.
오기가 생겨서 자꾸 새벽까지 도전해본 결과라고 생각하면 좀 뿌듯한 마음도 든다.
'내일배움캠프 > TIL' 카테고리의 다른 글
| [TIL 26일차]25.04.15 (0) | 2025.04.16 |
|---|---|
| [TIL 25일차]25.04.14 (0) | 2025.04.16 |
| [TIL 23일차]25.03.19 (0) | 2025.03.20 |
| [TIL 22일차]25.03.18 (0) | 2025.03.20 |
| [TIL 21일차]25.03.17 (0) | 2025.03.20 |