简体   繁体   English

Spring Security OAuth2修改响应主体连接

[英]Spring security oauth2 modify response body connexion

For my application, i'm using Spring security to secure API Rest. 对于我的应用程序,我正在使用Spring安全性来保护API Rest。 For this i use an oauth2 system (with AuthorizationServerConfiguration, OAuth2SecurityConfiguration and ResourceServerConfiguration) 为此,我使用了oauth2系统(具有AuthorizationServerConfiguration,OAuth2SecurityConfiguration和ResourceServerConfiguration)

I don't find how to modify the body of my response connexion. 我找不到如何修改我的响应连接主体的方法。

For moment, i have: 暂时,我有:

{
"access_token": "0d1dded8-3631-472a-ba63-89d67d133112",
"token_type": "bearer",
"refresh_token": "d9f4cc5d-748b-461f-b475-3bba95b512dc",
"expires_in": 29162,
"scope": "read write trust"
}

And i want something like that: 我想要这样的东西:

{
"errorLevel":"OK",
"errorMsg":"You are now connected",
"data": { // access_token, token_type... }
}

My first idea was to implement my accessTokenConverter and add it to the endpoints like that: 我的第一个想法是实现accessTokenConverter并将其添加到端点,如下所示:

endpoints.accessTokenConverter(new CustomAccessTokenConverter());

But, he don't use my AccessTokenConverter. 但是,他不使用我的AccessTokenConverter。

So, how i can modify my response? 因此,我如何修改我的回复?

For newer versions of spring you can use ResponseBodyAdvice https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdvice.html to change the response. 对于较新的spring版本,可以使用ResponseBodyAdvice https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdvice.html更改响应。

Implementation goes something like this : 实现过程如下:

@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;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM