关注分享主机优惠活动
国内外VPS云服务器

【微信开发】SpringBoot集成微信小程序授权登录。

文章目录【微信开发】SpringBoot综合微信小程序授权登录1。Springboot后端(1)准备(2)相关配置类(3)相关实体类(4)处理后端逻辑2。Uniapp前端(1)授权登录(2)效果风格微信微信官方账号。

【微信开发】SpringBoot集成微信小程序授权登录。我这里靠的是第三方,是目前最火的微信开发工具,WxJava。

1.SprinBoot后端(1)准备工作引入相关依赖。

com . github . binary wangweixin -Java -pay 4 . 1 . 0配置应用程序. yml

# ----------------系统配置#服务配置pay -平台:#微信wx: pay: appid: secret。

进口龙目岛。数据;导入org . spring framework . boot . context . properties . configuration properties;/* * * wxpayproperties。* * @ AuthorBinarywang */@ data @ Configuration Properties(prefix = " pay -platform . wx . pay ")公共类wxpayproperties {/* * *设置微信微信官方账号的appid或appId */私有字符串appId;私有字符串秘密;/* * *微信支付商户号*/私串mchId/* * *微信支付商户密钥*/私有字符串mchKey/* * *服务商模式下的子商户公共账户ID,请不要在普通模式下配置,请删除配置文件*/私有字符串subAppId中的对应项;/* * *服务商模式下的子商户号,请不要在普通模式下配置,最好删除配置文件*/private string Sub macid中的对应项;/* * * API client _ cert . p12文件的绝对路径,或者如果放在项目中,请在classpath开头指定*/private String key path:/* * *支付回调地址*/private String notifyUrl;}属性配置类

导入com . github . binary Wang . wx pay . config . wx pay config;导入com . github . binary Wang . wx pay . service . wx pay service;导入com . github . binary Wang . wx pay . service . impl . wxpayserviceimpl;进口龙目岛。AllArgsConstructor导入org . Apache . commons . lang 3 . string utils;import org . spring framework . boot . auto configure . condition . condition alon class;import org . spring framework . boot . auto configure . condition . conditionalonmissingbean;import org . spring framework . boot . context . properties . enableconfigurationproperties;导入org . spring framework . context . annotation . bean;导入org . spring framework . context . annotation . configuration;/* * * * @作者二进制王*/@ Configuration @ conditional class(wxpayservice . class)@ EnableConfigurationProperties(WxPayProperties . class)@ AllArgsConstructorpublic class WxPayConfiguration { private WxPayProperties properties;@ Bean @ conditionalonmissingbean public WxPayService wx service(){ WxPayConfig pay config = new WxPayConfig();pay config . setappid(string utils . trim tonull(this . properties . getappid());pay config . setmchid(string utils . trim tonull(this . properties . getmchid()));pay config . setmchkey(string utils . trim tonull(this . properties . getmchkey()));pay config . setsubappid(string utils . trim tonull(this . properties . getsubappid());pay config . setsubmcid(string utils . trim tonull(this . properties . getsubmcid());pay config . set key path(string utils . trim tonull(this . properties . getkey path());//可以指定是否使用沙盒环境payconfig。setusesandboxenv(false);WxPayService WxPayService = new WxPayServiceImpl();wxpayservice . set config(pay config);返回wxPayService}} (3)相关实体类相关实体类可以用json map格式处理,这是我这里的个人习惯。

进口龙目岛。数据;导入lombok . experimental . accessors;/* * *接口调用凭证* https://developers . weixin . QQ . com/mini program/dev/API -back end/open -API/access -token/auth . getaccessstoken . html * * @ author tell sea * @ date 2021/05/20 */@ Data @ Accessors(chain = true)公共类AccessToken {私有字符串access token;/* * *凭证有效时间,单位:秒。当前值在7200秒内。*/private Integer expi resi;私有整数errCode私有字符串errMsg}导入龙目岛。数据;导入lombok . experimental . accessors;/* * * OpenID * https://developers . weixin . QQ . com/mini program/dev/API -back end/open -API/log in/auth . code 2 session . html * * @ authtellsea * @ date 2021/05/20 */@ Data @ Accessors(chain = true)public class code 2 session { private String OpenID;私有字符串sessionKey私有字符串unionId私有整数errCode私有字符串errMsg}导入龙目岛。数据;导入lombok . experimental . accessors;/* * *微信登录* * @ AuthorTellsea * @ date 2021/05/19 */@ data @ accessors(chain = true)公共类weixinlogin {私有字符串代码;私有字符串encryptedData私弦四;私有字符串昵称;私有字符串avatarUrl私有整数性别;}龙目岛。数据;导入lombok . experimental . accessors;/* * *微信令牌* * @ AuthorTellsea * @ date 2021/05/20 */@ data @ accessors(chain = true)公共类weixin令牌{公共静态字符串令牌;(4)处理后端逻辑控制层

包com . ruo yi . business . appuser . controller;导入cn . Hu tool . core . lang . validator;导入com . Alibaba . fast JSON . JSON object;导入com . baomi dou . mybatisplus . core . conditions . query . lambdaquerywrapper;导入com . ruoyi . business . appuser . service . weixinservice;导入com . ruo yi . business . appuser . VO . wx . code 2 session;导入com . ruo yi . business . appuser . VO . wx . order info;导入com . ruo yi . business . appuser . VO . wx . weixinlogin;导入com . ruoyi . common . constant . constants;导入com . ruoyi . common . constant . user constants;导入com . ruoyi . common . core . domain . Ajax result;导入com . ruoyi . common . core . domain . entity . sys user;导入com . ruoyi . common . core . domain . model . loginuser;导入com . ruoyi . common . utils . security utils;导入com . ruo yi . common . utils . servlet utils;导入com . ruoyi . framework . web . service . sysloginservice;导入com . ruoyi . framework . web . service . syspermissionservice;导入com . ruo yi . framework . web . service . token service;导入com . ruoyi . system . service . isysuserservice;导入com . zhhy . tool . utils . integer utils;导入io . swagger . annotations . API;导入io . swagger . annotations . API operation;导入lombok . extern . SLF 4j . SLF 4j;导入org . Apache . commons . codec . binary . base64;导入org . Apache . commons . lang 3 . string utils;导入org . spring framework . beans . factory . annotation . auto wired;导入org . spring framework . util . collection utils;导入org . spring framework . web . bind . annotation . *;导入javax . crypto . cipher;导入javax . crypto . spec . ivparameterspec;导入javax . crypto . spec . secretkeyspec;导入Java . nio . charset . standard charsets;导入Java . security . spec . algorithm parameter spec;导入Java . util . list;导入Java . util . set;/* * * @ AuthorTellSea * @ date 2021/11/09 */@ slf4j @ API(value = "微信API ",Tags = { "微信API " })@ rest controller @ request mapping("/au/weixin ")公共类auweixincontroller { @ auto wired private isysuserserviceuserservice;@ Autowiredprivate SysLoginService loginService;@ autowired private WeiXinService WeiXinService;@ Autowiredprivate SysPermissionService permissionService;@ autowired private token service token service;@ApiOperation("微信用户登录")@ post mapping(" log in ")public Ajax结果登录(@ requestbodyweixinlogin dto){ code 2 session code 2 session = weixinservice . code 2 session(dto . getcode());If(字符串实用程序。不为空(code2session。get OpenID())){//解析电话号码字符串phone numberbyte[]byEncrypdata = base64 . decode base 64(dto . getencrypteddata());byte[]byiv data = base64 . decode base 64(dto . getiv());byte[]by session key = base64 . decode base 64(code 2 session . getsession key());algorithm parameter spec iv spec = new IvParameterSpec(byIvdata);尝试{ SecretKeySpec key spec = new SecretKeySpec(bysession key," AES ");cipher cipher = cipher . getinstance(" AES/CBC/pkcs 5 padding ");cipher.init(密码。DECRYPT_MODE,keySpec,iv spec);String phone result = new String(cipher . do final(byEncrypdata),StandardCharsets。UTF _ 8);JSON object phone object = JSON object . parse object(phone result);phone number = phoneobject . getstring(" phone number ");} catch(Exception e){ e . printstacktrace();返回AjaxResult.error("手机号解密失败");}//根据openId查询该用户是否存在list list = userservice.list(新lambda查询包装器()。eq (sysuser:: get OpenID,code2Session.getOpenId())。或者()。eq(SysUser::getUserName,phoneNumber)。或者()。eq(SysUser::getPhonenumber,phone number));Ajax result Ajax = Ajax result . success();If(集合utils。isempty(list)){//添加新的用户字符串defaultPassword = " 111111SysUser user = new SysUser()。setopen id(code 2 session . get OpenID())。setUserName(电话号码)。setNickName(dto.getNickName())。setDeptId(0L)。setPassword(默认密码)。setPhonenumber(电话号码)。set avatar(dto . getavatarurl());if(integer utils . eq(dto . getgender(),0)){ user . setsex(" 2 ");} else if(integer utils . eq(dto . getgender(),1)){ user . setsex(" 0 ");} else if(integerutils . eq(dto . getgender(),2)){ user . setsex(" 1 ");} if(用户常量。不唯一。等于(用户服务。check username unique(user . get username())){返回Ajax结果。错误(“手机号已注册”);} else if(验证器。不为空(用户。getphonenumber())& & user constants。不唯一。等于(用户服务。check phone unique(user)){返回Ajax结果。错误(“手机号已被使用”);} user . setcreateby(security utils . get username());user . set password(security utils . encrypt password(user . get password()));//默认为角色用户user . setroleids(new Long[]{ 1L });userService.insertUser(用户);string token = log in service . log in(user . get username()、default password);ajax.put(常量。TOKEN,令牌);返回ajax} else if (list.size() == 1) {//更新用户信息:此处找到的信息之一可能是SysUser sysUser = list.get(0)在openId、userName、phonenumber三个字段之一中找到的;sysuser . set nickname(dto . get nickname());sysuser . set avatar(dto . getavatarurl());if(integer utils . eq(dto . getgender(),0)){ sysuser . setsex(" 2 ");} else if(integerutils . eq(dto . getgender(),1)){ sysuser . setsex(" 0 ");} else if(integer utils . eq(dto . get gender(),2)) {sysUser

未经允许不得转载:主机频道 » 【微信开发】SpringBoot集成微信小程序授权登录。

评论 抢沙发

评论前必须登录!