优化token返回前端的json为标准格式

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