|
@@ -8,6 +8,7 @@
|
|
|
|
|
|
package com.zskk.shop.controller;
|
|
|
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.security.InvalidKeyException;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
@@ -18,6 +19,7 @@ import javax.crypto.NoSuchPaddingException;
|
|
|
import javax.servlet.http.Cookie;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.ui.Model;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
@@ -26,6 +28,7 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.zskk.shop.controller.bean.UserBean;
|
|
|
import com.zskk.shop.controller.bean.UserInfoBean;
|
|
|
import com.zskk.shop.exception.ErrorConstant;
|
|
|
+import com.zskk.shop.exception.ZSKKError;
|
|
|
import com.zskk.shop.exception.ZSKKException;
|
|
|
import com.zskk.shop.utils.LogUtil;
|
|
|
import com.zskk.shop.utils.SecretUtil;
|
|
@@ -34,6 +37,12 @@ public class AbstractController {
|
|
|
private static final String SECRET_KEY = "870aee09e19544039890a011a0247b74";
|
|
|
private static final String USER_COOKIE_NAME = "ptkey";
|
|
|
|
|
|
+ @Value("${zskk.uccenter}")
|
|
|
+ private String uccenter;
|
|
|
+
|
|
|
+ @Value("${zskk.myhost}")
|
|
|
+ private String myhost;
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前登录用户 未登录抛异常
|
|
|
*
|
|
@@ -45,20 +54,27 @@ public class AbstractController {
|
|
|
|
|
|
/**
|
|
|
* 获取用户信息 为登录抛出异常
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
public UserInfoBean getUserInfo() {
|
|
|
+ String value = null;
|
|
|
try {
|
|
|
- String value = getCookieValue(USER_COOKIE_NAME);
|
|
|
- if (value == null) {
|
|
|
- throw new ZSKKException(ErrorConstant.NO_LOGIN_ERROR);
|
|
|
- }
|
|
|
- UserInfoBean user = JSON.parseObject(value, UserInfoBean.class);
|
|
|
- return user;
|
|
|
+ value = getCookieValue(USER_COOKIE_NAME);
|
|
|
} catch (Exception e) {
|
|
|
- LogUtil.error(e.getMessage(), e);
|
|
|
- throw new ZSKKException(ErrorConstant.NO_LOGIN_ERROR);
|
|
|
+ ZSKKError error = new ZSKKError(ErrorConstant.NO_LOGIN_ERROR.getCode(),
|
|
|
+ uccenter + "/login/authorize?redirect=" + this.getRefererUrl());
|
|
|
+ throw new ZSKKException(error);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (value == null) {
|
|
|
+ ZSKKError error = new ZSKKError(ErrorConstant.NO_LOGIN_ERROR.getCode(),
|
|
|
+ uccenter + "/login/authorize?redirect=" + this.getRefererUrl());
|
|
|
+ throw new ZSKKException(error);
|
|
|
}
|
|
|
+ UserInfoBean user = JSON.parseObject(value, UserInfoBean.class);
|
|
|
+ return user;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -69,7 +85,7 @@ public class AbstractController {
|
|
|
public UserBean getUserNoException() {
|
|
|
try {
|
|
|
String value = getCookieValue(USER_COOKIE_NAME);
|
|
|
- return value == null? null: JSON.parseObject(value, UserInfoBean.class).getUser();
|
|
|
+ return value == null ? null : JSON.parseObject(value, UserInfoBean.class).getUser();
|
|
|
} catch (Exception e) {
|
|
|
return null;
|
|
|
}
|
|
@@ -89,10 +105,11 @@ public class AbstractController {
|
|
|
|
|
|
private String getCookieValue(String name) throws InvalidKeyException, NoSuchAlgorithmException,
|
|
|
NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
|
|
|
-
|
|
|
- ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
- HttpServletRequest request = requestAttributes.getRequest();
|
|
|
- Cookie[] cookies = request.getCookies();
|
|
|
+
|
|
|
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
|
|
|
+ .getRequestAttributes();
|
|
|
+ HttpServletRequest request = requestAttributes.getRequest();
|
|
|
+ Cookie[] cookies = request.getCookies();
|
|
|
if (cookies != null && cookies.length > 0) {
|
|
|
for (Cookie cookie : cookies) {
|
|
|
if (USER_COOKIE_NAME.equals(cookie.getName())) {
|
|
@@ -103,8 +120,18 @@ public class AbstractController {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ private String getRefererUrl() {
|
|
|
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
|
|
|
+ .getRequestAttributes();
|
|
|
+ HttpServletRequest request = requestAttributes.getRequest();
|
|
|
+ try {
|
|
|
+ return URLEncoder.encode(request.getHeader("Referer"), "UTF-8");
|
|
|
+ } catch (Exception e) {
|
|
|
+ return request.getHeader("Referer");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|