优化token返回前端的json为标准格式
This commit is contained in:
+30
-7
@@ -1,13 +1,12 @@
|
||||
package com.central.common.utils;
|
||||
|
||||
import com.central.common.model.Result;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
@@ -27,15 +26,39 @@ public class ResponseUtil {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void responseWriter(ObjectMapper objectMapper, HttpServletResponse response, String msg, int httpStatus) throws IOException {
|
||||
Map<String, String> rsp = new HashMap<>(2);
|
||||
response.setStatus(httpStatus);
|
||||
rsp.put("resp_code", String.valueOf(httpStatus));
|
||||
rsp.put("resp_msg", msg);
|
||||
Result result = Result.succeedWith(null, httpStatus, msg);
|
||||
responseWrite(objectMapper, response, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过流写到前端
|
||||
* @param objectMapper 对象序列化
|
||||
* @param response
|
||||
* @param obj
|
||||
*/
|
||||
public static void responseSucceed(ObjectMapper objectMapper, HttpServletResponse response, Object obj) throws IOException {
|
||||
Result result = Result.succeed(obj);
|
||||
responseWrite(objectMapper, response, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过流写到前端
|
||||
* @param objectMapper
|
||||
* @param response
|
||||
* @param msg
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void responseFailed(ObjectMapper objectMapper, HttpServletResponse response, String msg) throws IOException {
|
||||
Result result = Result.failed(msg);
|
||||
responseWrite(objectMapper, response, result);
|
||||
}
|
||||
|
||||
private static void responseWrite(ObjectMapper objectMapper, HttpServletResponse response, Result result) throws IOException {
|
||||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||
try (
|
||||
Writer writer = response.getWriter()
|
||||
) {
|
||||
writer.write(objectMapper.writeValueAsString(rsp));
|
||||
writer.write(objectMapper.writeValueAsString(result));
|
||||
writer.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.central.oauth.controller;
|
||||
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.utils.ResponseUtil;
|
||||
import com.central.oauth2.common.token.MobileAuthenticationToken;
|
||||
import com.central.oauth2.common.token.OpenIdAuthenticationToken;
|
||||
import com.central.oauth2.common.util.AuthUtils;
|
||||
@@ -13,7 +13,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.authentication.*;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
@@ -29,7 +28,6 @@ import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
* OAuth2相关操作
|
||||
@@ -99,7 +97,7 @@ public class OAuth2Controller {
|
||||
OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
|
||||
OAuth2AccessToken oAuth2AccessToken = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);
|
||||
oAuth2Authentication.setAuthenticated(true);
|
||||
writerObj(response, oAuth2AccessToken);
|
||||
ResponseUtil.responseSucceed(objectMapper, response, oAuth2AccessToken);
|
||||
} catch (BadCredentialsException | InternalAuthenticationServiceException e) {
|
||||
exceptionHandler(response, badCredenbtialsMsg);
|
||||
} catch (Exception e) {
|
||||
@@ -114,17 +112,7 @@ public class OAuth2Controller {
|
||||
|
||||
private void exceptionHandler(HttpServletResponse response, String msg) throws IOException {
|
||||
response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
||||
writerObj(response, Result.failed(msg));
|
||||
}
|
||||
|
||||
private void writerObj(HttpServletResponse response, Object obj) throws IOException {
|
||||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||
try (
|
||||
Writer writer = response.getWriter()
|
||||
) {
|
||||
writer.write(objectMapper.writeValueAsString(obj));
|
||||
writer.flush();
|
||||
}
|
||||
ResponseUtil.responseFailed(objectMapper, response, msg);
|
||||
}
|
||||
|
||||
private ClientDetails getClient(String clientId, String clientSecret) {
|
||||
|
||||
@@ -91,8 +91,8 @@
|
||||
},
|
||||
success: function (data) {
|
||||
//console.log(JSON.stringify(data));
|
||||
if (data.access_token) {
|
||||
config.putToken(data);
|
||||
if (data.resp_code === 0) {
|
||||
config.putToken(data.datas);
|
||||
layer.msg('登录成功', {icon: 1, time: 500}, function () {
|
||||
location.replace('./');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user