微信第三方登录,未成功
This commit is contained in:
parent
39f22007d8
commit
b358cd7c64
@ -3,13 +3,12 @@ oauth:
|
||||
wx:
|
||||
miniapp:
|
||||
appId: wx9f9b7d747d261686
|
||||
appSecret: b55d707dbe4e98d5db23648694a8ce8e
|
||||
appSecret: 270f94059fa2d4abad7eacf5da25656f
|
||||
url: https://api.weixin.qq.com/sns/jscode2session
|
||||
pub:
|
||||
appId: appId
|
||||
appSecret: appSecret
|
||||
weixin:
|
||||
appId: wx97a8f7019d175ced
|
||||
appSecret: ca908a5464d6f311d2723d69ac1df0ea
|
||||
url: https://api.weixin.qq.com/sns/oauth2/access_token
|
||||
|
||||
tfa:
|
||||
phone:
|
||||
dysms:
|
||||
|
||||
@ -368,7 +368,7 @@ public class DpFlowCalc {
|
||||
* (flowProps.getdTb_M()) / Tf;
|
||||
flowProps.setdVFlowb(FormatUtil.format(Qn,Optional.of(5)));
|
||||
|
||||
flowProps.setdBk(BkTable());
|
||||
flowProps.setdBk(1);
|
||||
|
||||
//标况质量流量
|
||||
flowProps.setdMFlowb(FormatUtil.format(flowProps.getdVFlowb() * gasProps.dRhob,Optional.of(5)));
|
||||
|
||||
@ -4,14 +4,14 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class WxPubConstant {
|
||||
@Value("${oauth.wx.pub.appId}")
|
||||
public class WxAppConstant {
|
||||
@Value("${oauth.wx.weixin.appId}")
|
||||
private String appId;
|
||||
|
||||
@Value("${oauth.wx.pub.appSecret}")
|
||||
@Value("${oauth.wx.weixin.appSecret}")
|
||||
private String appSecret;
|
||||
|
||||
@Value("${oauth.wx.pub.url}")
|
||||
@Value("${oauth.wx.weixin.url}")
|
||||
private String url;
|
||||
|
||||
public String getUrl() {
|
||||
@ -1,5 +1,6 @@
|
||||
package com.ruoyi.oauth.wx.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.auth.common.domain.OauthUser;
|
||||
import com.ruoyi.auth.common.service.IOauthUserService;
|
||||
import com.ruoyi.common.annotation.Anonymous;
|
||||
@ -7,13 +8,14 @@ import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.framework.web.service.SysLoginService;
|
||||
import com.ruoyi.framework.web.service.TokenService;
|
||||
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
|
||||
import com.ruoyi.oauth.wx.service.Impl.WxMiniAppLoginServiceImpl;
|
||||
import com.ruoyi.oauth.wx.service.Impl.WxPubLoginServiceImpl;
|
||||
import com.ruoyi.system.service.ISysUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/oauth/wx")
|
||||
@ -26,17 +28,29 @@ public class WxLoginController extends BaseController {
|
||||
private WxMiniAppLoginServiceImpl wxMiniAppLoginServiceImpl;
|
||||
|
||||
@Autowired
|
||||
private WxPubLoginServiceImpl wxPubLoginServiceImpl;
|
||||
private WxPubLoginServiceImpl wxAppLoginServiceImpl;
|
||||
@Autowired
|
||||
|
||||
private ISysUserService sysUserService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private UserDetailsServiceImpl userDetailsServiceImpl;
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
@Autowired
|
||||
private SysLoginService sysLoginService;
|
||||
|
||||
@Anonymous
|
||||
@PostMapping("/login/{source}/{code}")
|
||||
public AjaxResult loginMiniApp(@PathVariable("source") String source, @PathVariable("code") String code) {
|
||||
String token = null;
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
if ("miniapp".equals(source)) {
|
||||
if ("miniapp".equals(source) ) {
|
||||
token = wxMiniAppLoginServiceImpl.doLogin(code, true);
|
||||
} else if ("pub".equals(source)) {
|
||||
token = wxPubLoginServiceImpl.doLogin(code, false);
|
||||
} else if ("weixin".equals(source)) {
|
||||
token = wxAppLoginServiceImpl.doLogin(code, false);
|
||||
} else {
|
||||
return error("错误的登录方式");
|
||||
}
|
||||
@ -44,6 +58,21 @@ public class WxLoginController extends BaseController {
|
||||
return ajax;
|
||||
}
|
||||
|
||||
|
||||
@Anonymous
|
||||
@PostMapping("/loginApp/{code}")
|
||||
public AjaxResult loginApp( @PathVariable("authResult") Object authResult) {
|
||||
String token = null;
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
|
||||
// token = wxAppLoginServiceImpl.doLogin(code, false);
|
||||
|
||||
|
||||
ajax.put(Constants.TOKEN, token);
|
||||
return ajax;
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/register/{source}/{code}")
|
||||
public AjaxResult register(@PathVariable("source") String source, @PathVariable("code") String code) {
|
||||
OauthUser oauthUser = oauthUserService.selectOauthUserByUserId(getUserId());
|
||||
@ -56,8 +85,8 @@ public class WxLoginController extends BaseController {
|
||||
oauthUser.setCode(code);
|
||||
if ("miniapp".equals(source)) {
|
||||
msg = wxMiniAppLoginServiceImpl.doRegister(oauthUser);
|
||||
} else if ("pub".equals(source)) {
|
||||
msg = wxPubLoginServiceImpl.doRegister(oauthUser);
|
||||
} else if ("weixin".equals(source)) {
|
||||
msg = wxAppLoginServiceImpl.doRegister(oauthUser);
|
||||
} else {
|
||||
return error("错误的注册方式");
|
||||
}
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
package com.ruoyi.oauth.wx.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
public class WxLoginResult {
|
||||
private String token;
|
||||
private Boolean isNewUser;
|
||||
private WxUserInfo userInfo;
|
||||
|
||||
@Data
|
||||
public static class WxUserInfo {
|
||||
private String nickname;
|
||||
private String avatar;
|
||||
private Integer gender;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package com.ruoyi.oauth.wx.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class WxLoginUser {
|
||||
private String openid;
|
||||
private String unionid;
|
||||
private String sessionKey;
|
||||
private String accessToken;
|
||||
private String refreshToken;
|
||||
private Long expiresIn;
|
||||
|
||||
// 用户信息(APP端获取)
|
||||
private String nickname;
|
||||
private String avatar;
|
||||
private Integer gender;
|
||||
private String country;
|
||||
private String province;
|
||||
private String city;
|
||||
}
|
||||
|
||||
@ -0,0 +1,109 @@
|
||||
package com.ruoyi.oauth.wx.service.Impl;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.auth.common.domain.OauthUser;
|
||||
import com.ruoyi.auth.common.service.IOauthUserService;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import com.ruoyi.common.utils.MessageUtils;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.framework.manager.AsyncManager;
|
||||
import com.ruoyi.framework.manager.factory.AsyncFactory;
|
||||
import com.ruoyi.framework.web.service.SysLoginService;
|
||||
import com.ruoyi.framework.web.service.TokenService;
|
||||
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
|
||||
import com.ruoyi.oauth.wx.constant.WxAppConstant;
|
||||
import com.ruoyi.oauth.wx.constant.WxMiniAppConstant;
|
||||
import com.ruoyi.oauth.wx.service.WxLoginService;
|
||||
import com.ruoyi.system.service.ISysUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class WxAppLoginServiceImpl implements WxLoginService {
|
||||
@Autowired
|
||||
private WxMiniAppConstant wxMiniAppConstant;
|
||||
|
||||
@Autowired
|
||||
private WxAppConstant wxAppConstant;
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
||||
@Autowired
|
||||
private UserDetailsServiceImpl userDetailsServiceImpl;
|
||||
|
||||
@Autowired
|
||||
private ISysUserService userService;
|
||||
|
||||
@Autowired
|
||||
private IOauthUserService oauthUserService;
|
||||
|
||||
@Autowired
|
||||
private SysLoginService sysLoginService;
|
||||
|
||||
@Override
|
||||
public String doLogin(String code, boolean autoRegister) {
|
||||
JSONObject doAuth = doAuth(
|
||||
wxAppConstant.getUrl(),
|
||||
wxAppConstant.getAppId(),
|
||||
wxAppConstant.getAppSecret(),
|
||||
code);
|
||||
|
||||
|
||||
String openid = doAuth.getString("openid");
|
||||
OauthUser selectOauthUser = oauthUserService.selectOauthUserByUUID(openid);
|
||||
SysUser sysUser = null;
|
||||
if (selectOauthUser == null) {
|
||||
if (autoRegister) {
|
||||
sysUser = new SysUser();
|
||||
sysUser.setUserName(openid);
|
||||
sysUser.setNickName(openid);
|
||||
sysUser.setPassword(SecurityUtils.encryptPassword(code));
|
||||
userService.registerUser(sysUser);
|
||||
OauthUser oauthUser = new OauthUser();
|
||||
oauthUser.setUserId(sysUser.getUserId());
|
||||
oauthUser.setOpenId(doAuth.getString("openid"));
|
||||
oauthUser.setUuid(doAuth.getString("openid"));
|
||||
oauthUser.setSource("WXMiniApp");
|
||||
oauthUser.setAccessToken(doAuth.getString("session_key"));
|
||||
oauthUserService.insertOauthUser(oauthUser);
|
||||
}
|
||||
} else {
|
||||
sysUser = userService.selectUserById(selectOauthUser.getUserId());
|
||||
}
|
||||
if (sysUser == null) {
|
||||
throw new ServiceException("该微信未绑定用户");
|
||||
}
|
||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(sysUser.getUserName(), Constants.LOGIN_SUCCESS,
|
||||
MessageUtils.message("user.login.success")));
|
||||
LoginUser loginUser = (LoginUser) userDetailsServiceImpl.createLoginUser(sysUser);
|
||||
sysLoginService.recordLoginInfo(loginUser.getUserId());
|
||||
return tokenService.createToken(loginUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String doRegister(OauthUser oauthUser) {
|
||||
if (StringUtils.isEmpty(oauthUser.getCode())) {
|
||||
return "没有凭证";
|
||||
}
|
||||
if (oauthUser.getUserId() == null) {
|
||||
return "请先注册账号";
|
||||
}
|
||||
JSONObject doAuth = doAuth(
|
||||
wxAppConstant.getUrl(),
|
||||
wxAppConstant.getAppId(),
|
||||
wxAppConstant.getAppSecret(),
|
||||
oauthUser.getCode());
|
||||
oauthUser.setOpenId(doAuth.getString("openid"));
|
||||
oauthUser.setUuid(doAuth.getString("openid"));
|
||||
oauthUser.setSource("WXMiniApp");
|
||||
oauthUser.setAccessToken(doAuth.getString("session_key"));
|
||||
oauthUserService.insertOauthUser(oauthUser);
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
@ -24,7 +24,7 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class WxMiniAppLoginServiceImpl implements WxLoginService {
|
||||
@Autowired
|
||||
private WxMiniAppConstant wxAppConstant;
|
||||
private WxMiniAppConstant wxMiniAppConstant;
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
@ -44,9 +44,9 @@ public class WxMiniAppLoginServiceImpl implements WxLoginService {
|
||||
@Override
|
||||
public String doLogin(String code, boolean autoRegister) {
|
||||
JSONObject doAuth = doAuth(
|
||||
wxAppConstant.getUrl(),
|
||||
wxAppConstant.getAppId(),
|
||||
wxAppConstant.getAppSecret(),
|
||||
wxMiniAppConstant.getUrl(),
|
||||
wxMiniAppConstant.getAppId(),
|
||||
wxMiniAppConstant.getAppSecret(),
|
||||
code);
|
||||
String openid = doAuth.getString("openid");
|
||||
OauthUser selectOauthUser = oauthUserService.selectOauthUserByUUID(openid);
|
||||
@ -88,9 +88,9 @@ public class WxMiniAppLoginServiceImpl implements WxLoginService {
|
||||
return "请先注册账号";
|
||||
}
|
||||
JSONObject doAuth = doAuth(
|
||||
wxAppConstant.getUrl(),
|
||||
wxAppConstant.getAppId(),
|
||||
wxAppConstant.getAppSecret(),
|
||||
wxMiniAppConstant.getUrl(),
|
||||
wxMiniAppConstant.getAppId(),
|
||||
wxMiniAppConstant.getAppSecret(),
|
||||
oauthUser.getCode());
|
||||
oauthUser.setOpenId(doAuth.getString("openid"));
|
||||
oauthUser.setUuid(doAuth.getString("openid"));
|
||||
|
||||
@ -15,17 +15,23 @@ import com.ruoyi.framework.manager.factory.AsyncFactory;
|
||||
import com.ruoyi.framework.web.service.SysLoginService;
|
||||
import com.ruoyi.framework.web.service.TokenService;
|
||||
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
|
||||
import com.ruoyi.oauth.wx.constant.WxPubConstant;
|
||||
import com.ruoyi.oauth.wx.constant.WxAppConstant;
|
||||
import com.ruoyi.oauth.wx.domain.WxLoginUser;
|
||||
import com.ruoyi.oauth.wx.service.WxLoginService;
|
||||
import com.ruoyi.system.service.ISysUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Service
|
||||
public class WxPubLoginServiceImpl implements WxLoginService {
|
||||
|
||||
@Autowired
|
||||
private WxPubConstant wxH5Constant;
|
||||
private WxAppConstant wxH5Constant;
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
@ -80,6 +86,59 @@ public class WxPubLoginServiceImpl implements WxLoginService {
|
||||
return tokenService.createToken(loginUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信用户信息
|
||||
*/
|
||||
private WxLoginUser getWechatUserInfo(String accessToken, String openid) {
|
||||
try {
|
||||
String url = String.format("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s",
|
||||
accessToken, openid);
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String response = restTemplate.getForObject(url, String.class);
|
||||
JSONObject json = JSONObject.parseObject(response);
|
||||
|
||||
if (json.containsKey("errcode")) {
|
||||
throw new ServiceException("获取微信用户信息失败: " + json.getString("errmsg"));
|
||||
}
|
||||
|
||||
WxLoginUser userInfo = new WxLoginUser();
|
||||
userInfo.setOpenid(json.getString("openid"));
|
||||
userInfo.setNickname(json.getString("nickname"));
|
||||
userInfo.setProvince(json.getString("province"));
|
||||
userInfo.setCity(json.getString("city"));
|
||||
userInfo.setCountry(json.getString("country"));
|
||||
userInfo.setUnionid(json.getString("unionid"));
|
||||
|
||||
return userInfo;
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("获取微信用户信息失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证微信token(可选)
|
||||
*/
|
||||
private boolean validateWechatToken(String accessToken, String openid) {
|
||||
try {
|
||||
String url = String.format("https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s",
|
||||
accessToken, openid);
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
|
||||
restTemplate.getMessageConverters().add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
||||
|
||||
String response = restTemplate.getForObject(url, String.class);
|
||||
JSONObject json = JSONObject.parseObject(response);
|
||||
|
||||
return json.getInteger("errcode") == 0;
|
||||
} catch (Exception e) {
|
||||
// 如果验证失败,可以根据需求决定是否允许登录
|
||||
// 返回 true 表示跳过验证,返回 false 表示验证失败
|
||||
System.err.println("微信token验证失败: " + e.getMessage());
|
||||
return true; // 根据安全要求调整
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String doRegister(OauthUser oauthUser) {
|
||||
if (StringUtils.isEmpty(oauthUser.getCode())) {
|
||||
@ -101,4 +160,9 @@ public class WxPubLoginServiceImpl implements WxLoginService {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject doAuth(String url, String appid, String secret, String code) {
|
||||
return WxLoginService.super.doAuth(url, appid, secret, code);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ public interface WxLoginService {
|
||||
StringBuilder builder = new StringBuilder(url);
|
||||
builder.append("?appid=").append(appid)
|
||||
.append("&secret=").append(secret)
|
||||
.append("&js_code=").append(code)
|
||||
.append("&code=").append(code)
|
||||
.append("&grant_type=").append("authorization_code");
|
||||
String getMessageUrl = builder.toString();
|
||||
String result = HttpUtils.get(getMessageUrl);
|
||||
@ -31,4 +31,5 @@ public interface WxLoginService {
|
||||
throw new ServiceException(jsonObject.getString("errmsg"), jsonObject.getIntValue("errcode"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user