本文主要是介绍自定义spring security oauth /auth/token的返回内容格式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
AuthServerResponse
package com.zy.platform.auth.web;import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;import java.io.Serializable;/*** @author javachen* @description response响应信息*/
@JsonSerialize(using = AuthServerResponseSerializer.class)
@Data
public class AuthServerResponse<T> implements Serializable {private Integer status;private String msg;private T data;}
AuthServerResponseSerializer
package com.zy.platform.auth.web;import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.springframework.security.oauth2.common.OAuth2AccessToken;import java.io.IOException;/*** @author javachen* @description 序列化token返回格式*/
public class AuthServerResponseSerializer extends StdSerializer<AuthServerResponse> {public AuthServerResponseSerializer() {super(AuthServerResponse.class);}@Overridepublic void serialize(AuthServerResponse value, JsonGenerator gen, SerializerProvider provider) throws IOException {OAuth2AccessToken oAuth2AccessToken = (OAuth2AccessToken) value.getData();gen.writeStartObject();gen.writeNumberField("status", value.getStatus());gen.writeStringField("msg", value.getMsg());gen.writeObjectFieldStart("data");gen.writeStringField("access_token", oAuth2AccessToken.getValue());gen.writeStringField("token_type", oAuth2AccessToken.getTokenType());gen.writeStringField("refresh_token", oAuth2AccessToken.getRefreshToken().getValue());gen.writeNumberField("expires_in",oAuth2AccessToken.getExpiresIn());gen.writeStringField("scope",oAuth2AccessToken.getScope().toString());gen.writeStringField("userName",oAuth2AccessToken.getAdditionalInformation().get("userName").toString());gen.writeNumberField("userId",Integer.parseInt(oAuth2AccessToken.getAdditionalInformation().get("userId").toString()));gen.writeStringField("dataScope",oAuth2AccessToken.getAdditionalInformation().get("dataScope").toString());gen.writeNumberField("deptId",Integer.parseInt(oAuth2AccessToken.getAdditionalInformation().get("deptId").toString()));gen.writeStringField("dataScopeIds",oAuth2AccessToken.getAdditionalInformation().get("dataScopeIds").toString());//gen.writeStringField("clientId","");gen.writeEndObject();gen.writeEndObject();}
}
AuthTokenAspect
package com.zy.platform.auth.aspect;import com.zy.platform.auth.web.AuthServerResponse;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Component;/*** @author javachen* @description 切面控制oauth2返回格式*/
@Component
@Aspect
public class AuthTokenAspect {private Logger logger = LoggerFactory.getLogger(getClass());/// @Around是可以改变controller返回值的@Around("execution(* org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(..))")public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {// 放行AuthServerResponse response = new AuthServerResponse();Object proceed = pjp.proceed();if (proceed != null) {ResponseEntity<OAuth2AccessToken> responseEntity = (ResponseEntity<OAuth2AccessToken>)proceed;OAuth2AccessToken body = responseEntity.getBody();if (responseEntity.getStatusCode().is2xxSuccessful()) {response.setStatus(200);response.setMsg("");response.setData(body);} else {response.setStatus(500);response.setMsg("");response.setData(body);}}return ResponseEntity.status(200).body(response);}
}
最终效果
这篇关于自定义spring security oauth /auth/token的返回内容格式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!