Commit f21e0908 authored by yanzg's avatar yanzg

异常处理显示

parent 4499d371
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.CloudContans;
import com.yanzuoguang.cloud.helper.CookiesHelper;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.ExceptionHelper;
......@@ -57,6 +58,7 @@ public class WebAspect extends BaseRequestAspect {
long start = System.currentTimeMillis();
String name = joinPoint.getSignature().getName();
try {
CookiesHelper.tokenInit();
logger.info("[ {} ] request params is {}", name, joinPoint.getArgs());
Object result = executeMethod(joinPoint, name);
......@@ -80,6 +82,7 @@ public class WebAspect extends BaseRequestAspect {
throw e;
}
} finally {
CookiesHelper.tokenFinish();
Log.threadCommit();
saveInterLogs(joinPoint, responseResult);
}
......
package com.yanzuoguang.cloud.helper;
import com.yanzuoguang.token.TokenHelper;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.helper.UrlHelper;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
* 获取Cookies对象
*
* @author 颜佐光
*/
public class CookiesHelper {
/**
* 单个Cookies最大长度
*/
private static final int ITEM_MAX_SIZE = 1000;
/**
* 编码方式
*/
private static final String CHARSET = "UTF-8";
/**
* Cookies标记格式
*/
private static final String TAG_FORMAT = "%s_%d";
/**
* TOKEN缓存对象
*/
public static final String TOKEN_STRING_KEY = "T_S";
/**
* 读取缓存Cookies
*
* @param key 键值
* @return 缓存的键值
*/
public static String get(String key) {
HttpServletRequest request = getRequest();
Map<String, String> map = new HashMap<>();
int keyLength = 0;
// 遍历cookies找到对应的Cookies
for (Cookie item : request.getCookies()) {
if (item.getName().equals(key)) {
if (StringHelper.isNumber(item.getValue())) {
keyLength = StringHelper.toInt(item.getValue());
continue;
} else {
return UrlHelper.decoding(item.getValue(), CHARSET);
}
} else if (item.getName().startsWith(key)) {
map.put(key, item.getValue());
}
}
// 循环处理多个Cookeis
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keyLength; i++) {
String itemKey = String.format(TAG_FORMAT, key, i);
if (!map.containsKey(itemKey)) {
throw new CodeException("Cookies缺少[" + itemKey + "]");
}
sb.append(map.get(itemKey));
}
return UrlHelper.decoding(sb.toString(), CHARSET);
}
/**
* 写入缓存Cookies
*
* @param key 键
* @param value 值
*/
public static void set(String key, String value) {
HttpServletResponse response = getResposne();
String toValue = UrlHelper.encoding(value, CHARSET);
if (toValue.length() < ITEM_MAX_SIZE) {
// 创建cookie对象
Cookie cookie = new Cookie(key, value);
// 服务器把cookie响应给客户端,所有的cookie对象,都会在服务器端创建,通过http响应给客户端(浏览器)
response.addCookie(cookie);
} else {
int page = StringHelper.getPage(toValue.length(), ITEM_MAX_SIZE);
{
// 总Cookies数量
Cookie cookie = new Cookie(key, String.valueOf(page));
response.addCookie(cookie);
}
for (int i = 0; i <= page; i++) {
String itemKey = String.format(TAG_FORMAT, key, i);
int start = i * ITEM_MAX_SIZE;
int size = Math.min(page - start, ITEM_MAX_SIZE);
int end = start + size - 1;
String itemValue = toValue.substring(start, end);
Cookie cookie = new Cookie(itemKey, itemValue);
response.addCookie(cookie);
}
}
}
/**
* 获取请求对象
*
* @return Servlet请求对象
*/
public static HttpServletRequest getRequest() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes())
.getRequest();
return request;
}
/**
* 获取请求对象
*
* @return Servlet请求对象
*/
public static HttpServletResponse getResposne() {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes())
.getResponse();
return response;
}
/**
* 登录结束处理
*/
public static void tokenFinish() {
String tokenResponse = TokenHelper.getTokenString();
if (!StringHelper.isEmpty(tokenResponse)) {
set(TOKEN_STRING_KEY, tokenResponse);
}
TokenHelper.remove();
}
/**
* 登录初始化
*/
public static void tokenInit() {
String tokenRequest = get(CookiesHelper.TOKEN_STRING_KEY);
if (!StringHelper.isEmpty(tokenRequest)) {
TokenHelper.setTokenString(tokenRequest);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment