移动端登录

This commit is contained in:
Paidax
2025-04-03 15:17:15 +08:00
parent eb3a046c5c
commit c3d023c374
2 changed files with 46 additions and 10 deletions
@@ -13,7 +13,10 @@ import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.util.NutMap;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.Base64;
/**
* @version 1.0
@@ -26,6 +29,9 @@ import java.net.URLEncoder;
@IocBean
public class WeChatUtils {
private static final String ALGORITHM = "AES";
private static final String KEY = "4eXvXq1ZJmZj8z5z6yXvXq1ZJmZj8z5z";
@Inject
private RedisService redisService;
@@ -106,4 +112,20 @@ public class WeChatUtils {
return accessToken;
}
public String encrypt(String data) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public String decrypt(String encryptedData) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(decryptedBytes);
}
}
@@ -9,6 +9,7 @@ import io.v.nutz.sys.models.Sys_user;
import io.v.nutz.sys.services.SysUserService;
import io.v.nutz.web.commons.base.Globals;
import io.v.nutz.web.commons.slog.SLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
@@ -28,10 +29,10 @@ import org.nutz.mvc.annotation.Param;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
/**
@@ -40,6 +41,7 @@ import java.util.Optional;
* @author jug
* @date 2023/07/06
*/
@Slf4j
@IocBean
@At("/platform/wechat")
public class SysWechatController {
@@ -110,13 +112,24 @@ public class SysWechatController {
Sys_user sysUser = dao.fetch(Sys_user.class, Cnd.where("openId", "=", openid));
if (sysUser == null) {
String s = Globals.AppDomain + "/platform/wechat/doLogin?openid=" + openid;
// String s = "http://zhgh.hrbnu.edu.cn/sso/login?openid=" + openid;
if (StrUtil.isNotBlank(redirect)) {
s += "&redirect=" + URLEncoder.encode(redirect, StandardCharsets.UTF_8);
// String s = Globals.AppDomain + "/platform/wechat/doLogin?openid=" + openid;
//// String s = "http://zhgh.hrbnu.edu.cn/sso/login?openid=" + openid;
// if (StrUtil.isNotBlank(redirect)) {
// s += "&redirect=" + URLEncoder.encode(redirect, StandardCharsets.UTF_8);
// }
// return ">>:" + Globals.CasAddress + "/login?service=" + URLEncoder.encode(s, StandardCharsets.UTF_8);
//// return ">>:" + Globals.CasAddress + "/login;
String encrypt = null;
try {
encrypt = weChatUtils.encrypt(openid);
} catch (Exception e) {
log.error(">>> openid 加密失败:{}", openid);
}
try {
return ">>:" + Globals.AppDomain + "mobile/login?state=" + URLEncoder.encode(encrypt, "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error(">>> 请求转发失败:{}", openid);
}
return ">>:" + Globals.CasAddress + "/login?service=" + URLEncoder.encode(s, StandardCharsets.UTF_8);
// return ">>:" + Globals.CasAddress + "/login;
}
String loginName = Optional.ofNullable(sysUser).map(Sys_user::getLoginname).orElse(null);
@@ -147,7 +160,7 @@ public class SysWechatController {
@Ok("re")
public String doLogin(HttpSession session,
@Param("redirect") String redirect,
@Param("openid") String openid,
@Param("state") String state,
HttpServletRequest request) {
Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
@@ -164,8 +177,9 @@ public class SysWechatController {
try {
loginUtil.doLogin(loginName, request, session, LoginUtil.LoginOrigin.WEI_XIN);
//绑定openId
if (StrUtil.isNotBlank(openid)) {
dao.update(Sys_user.class, Chain.make("openId", openid), Cnd.where("loginName", "=", loginName));
if (StrUtil.isNotBlank(state)) {
String openId = weChatUtils.decrypt(state);
dao.update(Sys_user.class, Chain.make("openId", openId), Cnd.where("loginName", "=", loginName));
}
if (StrUtil.isNotBlank(redirect)) {
return ">>:" + redirect;