gpt4 book ai didi

java - 自定义 Spring Oauth 错误

转载 作者:搜寻专家 更新时间:2023-11-01 03:34:31 24 4
gpt4 key购买 nike

我必须自定义我的 Spring OAuth 2 响应。现在就像:

{
"error": "invalid_token",
"error_description": "Invalid access token: INVALID"
}

但我需要额外的数据,例如:

{
"status": -5,
"error": "invalid_token",
"error_description": "Invalid access token: INVALID",
"errors":[
{"message":"clear message for customer"}
]
}

如何自定义获取请求的消息?

我试过这个:

我的 spring servlet xml:

<bean id="oauthAccessDeniedHandler"
class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" >
<property name="exceptionTranslator" ref="OauthErrorHandler" />
</bean>

<bean id="OauthErrorHandler" class="com.mypath.handler.OauthErrorHandler"/>

我的翻译:

import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

public class OauthErrorHandler extends DefaultWebResponseExceptionTranslator implements WebResponseExceptionTranslator {

@Override
public ResponseEntity<OAuth2Exception> translate(Exception e){
ResponseEntity<OAuth2Exception> responseEntity = null;
try {
responseEntity = super.translate(e);
} catch (Exception e1) {
e1.printStackTrace();
}
OAuth2Exception body = responseEntity.getBody();
body.addAdditionalInformation("status", "-5");
body.addAdditionalInformation("errors", "[{\"message\":\""+body.getLocalizedMessage()+"\"}]");
HttpHeaders headers = new HttpHeaders();
headers.setAll(responseEntity.getHeaders().toSingleValueMap());
return new ResponseEntity<>(body, headers, responseEntity.getStatusCode());
}

}

但 react 还是一样

最佳答案

我已经解决了扩展 OAuth2Exception 和使用自定义 JSON 序列化器和反序列化器的问题

@org.codehaus.jackson.map.annotate.JsonSerialize(using = OAuth2ExceptionJackson1Serializer.class)
@org.codehaus.jackson.map.annotate.JsonDeserialize(using = OAuth2ExceptionJackson1Deserializer.class)
@com.fasterxml.jackson.databind.annotation.JsonSerialize(using = OAuth2ExceptionJackson2Serializer.class)
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = OAuth2ExceptionJackson2Deserializer.class)
public class CustomOauthException extends OAuth2Exception {

private static final long serialVersionUID = 124661L;

public CustomOauthException(String msg) {
super(msg);
}
private String oaut_error_code;
private int http_error_code;


public CustomOauthException(String msg, Throwable t) {
super(msg, t);
}

@Override
public int getHttpErrorCode() {
return this.http_error_code;
}


public int getHttp_error_code() {
return http_error_code;
}

public void setHttp_error_code(int http_error_code) {
this.http_error_code = http_error_code;
}

@Override
public String getOAuth2ErrorCode() {
return oaut_error_code;
}



public void setOaut_error_code(String oaut_error_code) {
this.oaut_error_code = oaut_error_code;
}





}

关于java - 自定义 Spring Oauth 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35265027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com