3 回答
TA贡献1条经验 获得超0个赞
利用切面去修改返回的格式
@Component
@Aspect
public class AuthTokenAspect {
private Logger logger = LoggerFactory.getLogger(getClass());
/// @Around环绕某个方法(“postAccessToken”的这个方法)
@Around("execution(* org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(..))")
public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
// 放行
Response response = new Response();
Object proceed = pjp.proceed();
if (proceed != null) {
// ResponseEntity 为自定义的返回格式
ResponseEntity<OAuth2AccessToken> responseEntity = (ResponseEntity<OAuth2AccessToken>)proceed;
OAuth2AccessToken body = responseEntity.getBody();
if (responseEntity.getStatusCode().is2xxSuccessful()) {
response.setErrorcode(ErrorCode.OK.value);
response.setValue(ErrorCode.OK.memo);
response.setData(body);
} else {
logger.error("error:{}", responseEntity.getStatusCode().toString());
response.setErrorcode(ErrorCode.ERROR.value);
response.setValue("获取授权码失败");
}
}
return ResponseEntity
.status(200)
.body(response);
}
}
添加回答
举报