HandlerMethodArgumentResolver
옵션을 조금 더 플러스합니다. 다음 지원하기 위해 :
@RequestMapping(
value = WEB_HOOKS,
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<SomeDTO> getThoseDTOs(@CurrentClientId String clientId)
{
// Do something with clientId - it will be null if there was no authentication
}
우리는 우리의 응용 프로그램 컨텍스트에 등록 된 HandlerMethodArgumentResolver
가 필요합니다을 (나를 위해이는 WebMvcConfigurerAdapter
내부이었다). 내 HandlerMethodArgumentResolver
은 다음과 같습니다
public class OAuth2ClientIdArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(CurrentClientId.class) != null
&& parameter.getParameterType().equals(String.class);
}
@Override
public Object resolveArgument(
MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory)
throws Exception
{
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if(authentication == null) {
return null;
}
String clientId = null;
if (authentication.getClass().isAssignableFrom(OAuth2Authentication.class)) {
clientId = ((OAuth2Authentication) authentication).getOAuth2Request().getClientId();
}
return clientId;
}
}
그리고 @interface
정의 :
@Target({ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CurrentClientId {
}
그러면 클라이언트 ID 애플리케이션이 아닌 Principal 사용자가 리턴되었습니다. – BigDong
업데이트 된 버전 :'auth.getOAuth2Request(). getClientId()' – Cataclysm