java - Spring security oauth2 修改响应主体连接

标签 java spring spring-security spring-security-oauth2

对于我的应用程序,我使用 Spring security 来保护 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。

那么,我如何修改我的回复?

最佳答案

对于较新版本的 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;
   }
}

关于java - Spring security oauth2 修改响应主体连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45162065/

相关文章:

java - 代码其他部分缺少 Spring Boot 安全权限

java - SmsManager 在发送每条短信之间有延迟

java - 使用生产者-消费者模式处理巨大的 CSV 文件

session - 登录页面使用 SSL,未加密的页面看不到加密的 session cookie(Grails、Acegi)

java - 登录应用程序前进行 IP 地址验证

java.io.FileNotFoundException : class path resource [src/main/webapp/WEB-INF/spring/appServlet/servlet-context. xml]

spring - 菜单处理问题-基于Spring Security的子菜单

java - 还提取带有一些特殊字符的模式

java - 带有嵌套for循环的java代码的复杂性

java - String[] 中的空指针异常