콘텐츠로 이동

2022 08 18

2022-08-18

UriComponentsBuilder

  • UriComponentsBuilder
    • UriComponents를 Build 하도록 도와줌
  • ServletUriComponentsBuilder
    • 이전 요청의 URI를 재사용 하여 편리하게 URI를 사용하도록 함
    • HttpServletRequest 객체의 정보를 이용해 URI를 구서함
    • 이전 요청에서의 URI에서 현재 필요한 정보를 뽑아내 유연하게 URI 설정 가능

Swagger에서 URI를 어플리케이션 구동 시점에 가져올 수는 없을까?

  • ServletUriComponentsBuilder.fromCurrentContextPath()
    • 이전 요청에서 정보를 가져오는 특성상 구동 시점에서는 해당 메서드로 URI 정보를 가져올 수는 없음

Swagger에서 Http Request의 Authorization이 필요한 경우에 대해서만 request를 넣어줄 수는 없을까?

  • 목표
    • 난 우선 프로덕션 코드에 영향을 주지 않고! SwaggerConfig에서만 코드를 작성하여 http Header가 필수인 api에 대해서만 http request를 넣어주고 싶음
  • Reflection을 사용해서 http request가 필요한 handler(controller method)를 가져와보자

    • ApplicationContext를 주입받아서 Reflection을 사용하면 다음과 같이 인가가 필요한 메서드들을 가져올 수 있음
      private List<RequestMappingInfo> getAuthenticationRequiredHandlers() {
          List<RequestMappingInfo> authorizationRequiredHandler = new ArrayList<>();
      
          RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext
              .getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
          Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
      
          for (Map.Entry<RequestMappingInfo, HandlerMethod> requestInfos : map.entrySet()) {
              RequestMappingInfo requestMappingInfo = requestInfos.getKey();
              HandlerMethod handlerMethod = requestInfos.getValue();
              MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
              for (MethodParameter methodParameter : methodParameters) {
                  if (methodParameter.hasParameterAnnotation(AuthenticationPrincipal.class)) {
                      authorizationRequiredHandler.add(requestMappingInfo);
                  }
              }
          }
      
          return authorizationRequiredHandler;
      }
      
    • 아... 근데 진짜 이걸 swagger에 등록해주는게 어렵네
      • 우선 swagger docket을 생성할 때는 globalparameter에 한해서만 설정해줄수있도록 하고 있음
      • 어노테이션 써서 아마 비즈니스코드에 넣는것을 권장하고 만들어서 그런것 같음
      • 후... predicate라는 걸 엄청쓰네 나 이거 잘 모름
        • 함수형을 써서 설정값을 런타임에 가져오는 형식인것같은데...?