|
@@ -1,19 +1,19 @@
|
|
package com.iamberry.wechat.core.entity;
|
|
package com.iamberry.wechat.core.entity;
|
|
-
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
import javax.servlet.http.Cookie;
|
|
import javax.servlet.http.Cookie;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
-import javax.servlet.http.HttpSession;
|
|
|
|
-
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import com.iamberry.app.tool.des.MD5;
|
|
import com.iamberry.app.tool.des.MD5;
|
|
|
|
+import com.iamberry.jwt.TokenUtil;
|
|
|
|
+import com.iamberry.jwt.UserNotLoginException;
|
|
|
|
+import com.iamberry.jwt.WebJsonTokenUtil;
|
|
import com.iamberry.wechat.core.entity.member.Member;
|
|
import com.iamberry.wechat.core.entity.member.Member;
|
|
import com.iamberry.wechat.tools.NameUtils;
|
|
import com.iamberry.wechat.tools.NameUtils;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
/**
|
|
/**
|
|
* description : 微信端用户工具类
|
|
* description : 微信端用户工具类
|
|
@@ -23,55 +23,82 @@ import com.iamberry.wechat.tools.NameUtils;
|
|
@Component
|
|
@Component
|
|
public class WechatUtils {
|
|
public class WechatUtils {
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private AdminUtils adminUtils;
|
|
|
|
- public void setAdminUtils(AdminUtils adminUtils) {
|
|
|
|
- this.adminUtils = adminUtils;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- private MD5 md5;
|
|
|
|
- public void setMd5(MD5 md5) {
|
|
|
|
- this.md5 = md5;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
- * 将用户信息保存到session中,并做安全设置
|
|
|
|
|
|
+ * 将用户信息保存到session中,并做安全设置, 注意,如果了安全,将token写入cookie后,应该在后台保存一份token的签名,前台 保存value,后台保存签名
|
|
* @param object
|
|
* @param object
|
|
* @param request
|
|
* @param request
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
- public void saveUserToSession(Member object, HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
|
- // 一旦登陆成功,就改变sessionID的值,防止发生session fixation
|
|
|
|
- request.getSession().invalidate();
|
|
|
|
- HttpSession session = request.getSession(true);
|
|
|
|
- session.setAttribute("wechat_login_user", object);
|
|
|
|
- // 保存cookie,多项验证
|
|
|
|
- adminUtils.createCookie(response, "wechat_login_openid", md5.encrypt(object.getUserId()+""));
|
|
|
|
|
|
+ public static void saveUserToSession(Member object, HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
|
+ // 判断当前是否存在
|
|
|
|
+ Cookie[] cookies = request.getCookies();
|
|
|
|
+ if (cookies != null) {
|
|
|
|
+ for (Cookie cookie : cookies) {
|
|
|
|
+ if (cookie.getName().equals("WATERO_WEB_PF")) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ // 如果cookie的name以cookie value的前8位口头,那么是用户token保存方式
|
|
|
|
+ String cookieNmae = cookie.getName();
|
|
|
|
+ String cookieValue = new String(cookie.getValue().substring(0, 8));
|
|
|
|
+ if (!cookieNmae.startsWith(cookieValue)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ // 无法准确获取key的值,只能遍历,所以系统应该尽量减少cookie,最后只是一个
|
|
|
|
+ Map<String, Object> userMap = WebJsonTokenUtil.verifier(cookie.getValue(), cookieNmae.substring(8));
|
|
|
|
+ if ((Integer)userMap.get("status") == 2) {
|
|
|
|
+ cookie.setMaxAge(0);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 生成原始token
|
|
|
|
+ String token = TokenUtil.getToken();
|
|
|
|
+ // 截取8位作为key
|
|
|
|
+ if (token.length() <= 13) {
|
|
|
|
+ token = "1234567890ABC";
|
|
|
|
+ }
|
|
|
|
+ String secret = new String(token.substring(0, 8));
|
|
|
|
+ String userToken = WebJsonTokenUtil.getToken(object.getUserId(), object.getUserOpenid(), token.substring(8));
|
|
|
|
+ // 保存cookie, 多项验证
|
|
|
|
+ AdminUtils.createCookie(response, token, secret+userToken);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取当前用户
|
|
|
|
|
|
+ * 1、获取当前用户,注意,前端系统完全弃用session,防止后端服务器宕机,导致用户业务异常终止 <br>
|
|
|
|
+ * 2、调用此方法,不存在返回为空,所以无需判断,但是此方法在运行时会抛出异常,调用时,不能捕获,让给系统统一处理
|
|
* @param request
|
|
* @param request
|
|
* @return
|
|
* @return
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
|
+ * @author 献
|
|
|
|
+ * @Time 2016年11月24日
|
|
*/
|
|
*/
|
|
- public Member getUserBySession(HttpServletRequest request) throws Exception {
|
|
|
|
- Member info = (Member) request.getSession().getAttribute("wechat_login_user");
|
|
|
|
- if (info == null || info.getUserId() == null) {
|
|
|
|
- return null;
|
|
|
|
|
|
+ public static Member getUserBySession(HttpServletRequest request) {
|
|
|
|
+ //获取一个cookie数组
|
|
|
|
+ Cookie[] cookies = request.getCookies();
|
|
|
|
+ if (cookies == null) {
|
|
|
|
+ throw new UserNotLoginException();
|
|
}
|
|
}
|
|
- Cookie[] cookies = request.getCookies();//获取一个cookie数组
|
|
|
|
- if (cookies != null && cookies.length >= 1) {
|
|
|
|
- for (Cookie cookie : cookies) {
|
|
|
|
- if (cookie.getName().equals("wechat_login_openid")) {
|
|
|
|
- if ((info.getUserId().toString()).equals(md5.decrypt(cookie.getValue()))) {
|
|
|
|
- return info;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ for (Cookie cookie : cookies) {
|
|
|
|
+ if (cookie.getName().equals("WATERO_WEB_PF")) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ // 如果cookie的name以cookie value的前8位口头,那么是用户token保存方式
|
|
|
|
+ String cookieNmae = cookie.getName();
|
|
|
|
+ String cookieValue = new String(cookie.getValue().substring(0, 8));
|
|
|
|
+ if (!cookieNmae.startsWith(cookieValue)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ // 无法准确获取key的值, 只能遍历,所以系统应该尽量减少cookie数量, 最好只是一个
|
|
|
|
+ Map<String, Object> userMap = WebJsonTokenUtil.verifier(cookie.getValue(), cookieNmae.substring(8));
|
|
|
|
+ if (Integer.parseInt(userMap.get("status").toString()) == 2) {
|
|
|
|
+ Member member = new Member();
|
|
|
|
+ member.setUserId(Integer.parseInt(userMap.get("userid").toString()));
|
|
|
|
+ member.setUserOpenid(userMap.get("WC_OPENID").toString());
|
|
|
|
+ return member;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return null;
|
|
|
|
|
|
+ // 凡是没有登录的用户,都抛出异常
|
|
|
|
+ throw new UserNotLoginException();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -82,38 +109,60 @@ public class WechatUtils {
|
|
public String getOpenIdFunction(String tokenId, boolean flag) {
|
|
public String getOpenIdFunction(String tokenId, boolean flag) {
|
|
String urlTemp;
|
|
String urlTemp;
|
|
if (flag) {
|
|
if (flag) {
|
|
- urlTemp = NameUtils.WECHAT_BACK_URL + "?tokenId=" + tokenId;
|
|
|
|
|
|
+ urlTemp = NameUtils.getConfig("WECHAT_BACK_URL") + "?tokenId=" + tokenId;
|
|
} else {
|
|
} else {
|
|
urlTemp = tokenId;
|
|
urlTemp = tokenId;
|
|
}
|
|
}
|
|
- String url;
|
|
|
|
try {
|
|
try {
|
|
- url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
|
|
|
|
- + NameUtils.appId
|
|
|
|
- + "&redirect_uri="
|
|
|
|
- + URLEncoder.encode(urlTemp, "UTF-8")
|
|
|
|
- + "&response_type=code&scope=snsapi_base&state=iamberry#wechat_redirect";
|
|
|
|
- return url;
|
|
|
|
|
|
+ StringBuilder builder = new StringBuilder("https://open.weixin.qq.com/connect/oauth2/authorize?appid=");
|
|
|
|
+ builder.append(NameUtils.getConfig("appId"))
|
|
|
|
+ .append("&redirect_uri=")
|
|
|
|
+ .append(URLEncoder.encode(urlTemp, "UTF-8"))
|
|
|
|
+ .append("&response_type=code&scope=snsapi_base&state=iamberry#wechat_redirect");
|
|
|
|
+ return builder.toString();
|
|
} catch (UnsupportedEncodingException e) {
|
|
} catch (UnsupportedEncodingException e) {
|
|
- // TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- public static String getOpenIdFunction() {
|
|
|
|
- String redirect_url = NameUtils.WECHAT_BACK_URL;
|
|
|
|
- String url = null;
|
|
|
|
- try {
|
|
|
|
- url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
|
|
|
|
- + NameUtils.appId
|
|
|
|
- + "&redirect_uri="
|
|
|
|
- + URLEncoder.encode(redirect_url, "UTF-8")
|
|
|
|
- + "&response_type=code&scope=snsapi_base&state=iamberry#wechat_redirect";
|
|
|
|
- } catch (UnsupportedEncodingException e1) {
|
|
|
|
- // TODO Auto-generated catch block
|
|
|
|
- e1.printStackTrace();
|
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ String token = TokenUtil.getToken();;
|
|
|
|
+ System.out.println(token);
|
|
|
|
+ String sec = token.substring(8);
|
|
|
|
+ System.out.println(sec);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static MD5 md5 = new MD5("callback");
|
|
|
|
+
|
|
|
|
+ public static String setCallback(String callback) throws Exception {
|
|
|
|
+ return md5.encrypt(callback);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getCallback(String temp) throws Exception {
|
|
|
|
+ return md5.decrypt(temp);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 跳转链接
|
|
|
|
+ * @param callback 跳转链接
|
|
|
|
+ * @return
|
|
|
|
+ * @author 献
|
|
|
|
+ * @throws Exception
|
|
|
|
+ * @Time 2016年11月24日
|
|
|
|
+ */
|
|
|
|
+ public static String getOpenIdFunction(String callback) throws Exception {
|
|
|
|
+ String redirect_url = NameUtils.getConfig("WECHAT_BACK_URL");
|
|
|
|
+
|
|
|
|
+ if (callback != null) {
|
|
|
|
+ redirect_url = redirect_url + "?callback=" + setCallback(callback);
|
|
}
|
|
}
|
|
- return url;
|
|
|
|
|
|
+ StringBuilder builder = new StringBuilder("https://open.weixin.qq.com/connect/oauth2/authorize?appid=");
|
|
|
|
+ builder.append(NameUtils.getConfig("appId"))
|
|
|
|
+ .append("&redirect_uri=")
|
|
|
|
+ .append(URLEncoder.encode(redirect_url, "UTF-8"))
|
|
|
|
+ .append("&response_type=code&scope=snsapi_base&state=iamberry#wechat_redirect");
|
|
|
|
+
|
|
|
|
+ return builder.toString();
|
|
}
|
|
}
|
|
}
|
|
}
|