对于我的应用程序,我使用Spring安全性来保护API Rest。为此,我使用oauth2系统(使用AuthorizationServerConfiguration,OAuth2SecurityConfiguration和ResourceServerConfiguration)
我没有找到如何修改我的反应联系的主体。
暂时,我有:
{
"access_token": "0d1dded8-3631-472a-ba63-89d67d133112",
"token_type": "bearer",
"refresh_token": "d9f4cc5d-748b-461f-b475-3bba95b512dc",
"expires_in": 29162,
"scope": "read write trust"
}
我想要这样的东西:
{
"errorLevel":"OK",
"errorMsg":"You are now connected",
"data": { // access_token, token_type... }
}
我的第一个想法是实现我的accessTokenConverter并将其添加到端点中:
endpoints.accessTokenConverter(new CustomAccessTokenConverter());
但是,他没有使用我的AccessTokenConverter。
那么,我如何修改我的回复?
答案 0 :(得分:2)
对于较新版本的spring,您可以使用ResponseBodyAdvice https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdvice.html来更改响应。
实施如下:
@ControllerAdvice
public class OauthReponseAdvice implements ResponseBodyAdvice<SomeResponseType> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public SomeResponseType beforeBodyWrite(SomeResponseType body, MethodParameter returnType, MediaType selectedContentType, Class<? extends
HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
/// Modify body here
return body;
}
}