微信登录思路及代码实现

微信登录思路及代码实现微信登录前端使用 uni app 配置好微信信息 直接使用 uni login 获取 codeuni login success asyncres 利用小程序

大家好,欢迎来到IT知识分享网。

微信登录

前端使用uni-app配置好微信信息,直接使用uni.login({})获取code

img

uni.login({ 
    success: async res => { 
    // 利用小程序登陆,配合code码进行用户登陆操作,并返回用户的token值 const result = await this.$u.api.getWxLogin({ 
    code: res.code }); console.log(result,result.token,"<<<") // 将token值进行本地缓存存储 uni.setStorage({ 
    key: 'token', data: result.token, success: async () => { 
    // 更新用户信息 // await this.$u.api.postUpdateUser({ 
    // sex, // photoUrl, // nickName // }); // 路由跳转至首页 uni.reLaunch({ 
    url: '/pages/index/index', }); } }); } }); 

image-20240411202435748

  1. 调用 wx.login() 获取 临时登录凭证code ,并回传到开发者服务器。
  2. 调用 auth.code2Session 接口,换取 用户唯一标识 OpenID 、 用户在微信开放平台帐号下的唯一标识UnionID(若当前小程序已绑定到微信开放平台帐号) 和 会话密钥 session_key。
  3. 之后开发者服务器可以根据用户标识来生成自定义登录态,用于后续业务逻辑中前后端交互时识别用户身份。

思路:

  1. 微信用户授权登录

    根据微信登录返回到服务器端的临时登录凭证code,调用微信接口获取session_key和openid,更新当前用户信息,生成用户唯一标识token

  2. 更新用户信息

JWT工具类

(1)JWT工具概述

JWT(Json Web Token)是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准。

JWT的声明一般被用来在身份提供者和服务提供者间传递被认证的用户身份信息,以便于从资源服务器获取资源。比如用在用户登录上

JWT最重要的作用就是对 token信息的防伪作用。

(2)JWT的原理

一个JWT由三个部分组成:公共部分、私有部分、签名部分。最后由这三者组合进行base64编码得到JWT。

image-20240411201413379

1、 公共部分

主要是该JWT的相关配置参数,比如签名的加密算法、格式类型、过期时间等等。

Key=ATGUIGU

2、 私有部分

用户自定义的内容,根据实际需要真正要封装的信息。

userInfo{用户的Id,用户的昵称nickName}

3、 签名部分

SaltiP: 当前服务器的Ip地址!{linux 中配置代理服务器的ip}

主要用户对JWT生成字符串的时候,进行加密{盐值}

最终组成 key+salt+userInfo è token!

base64编码,并不是加密,只是把明文信息变成了不可见的字符串。但是其实只要用一些工具就可以把base64编码解成明文,所以不要在JWT中放入涉及私密的信息。

(3) 在common-util添加依赖

<dependencies> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> </dependency> </dependencies> 

(4)在common-util添加JWT工具类

package com.atguigu.ssyx.common.utils.helper; import io.jsonwebtoken.*; import org.springframework.util.StringUtils; import java.util.Date; public class JwtHelper { 
    private static long tokenExpiration = 365*24*60*60*1000; private static String tokenSignKey = "ssyx"; public static String createToken(Long userId, String userName) { 
    String token = Jwts.builder() .setSubject("ssyx-USER") .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration)) .claim("userId", userId) .claim("userName", userName) .signWith(SignatureAlgorithm.HS512, tokenSignKey) .compressWith(CompressionCodecs.GZIP) .compact(); return token; } public static Long getUserId(String token) { 
    if(StringUtils.isEmpty(token)) return null; Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token); Claims claims = claimsJws.getBody(); Integer userId = (Integer)claims.get("userId"); return userId.longValue(); // return 1L; } public static String getUserName(String token) { 
    if(StringUtils.isEmpty(token)) return ""; Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token); Claims claims = claimsJws.getBody(); return (String)claims.get("userName"); } public static void removeToken(String token) { 
    //jwttoken无需删除,客户端扔掉即可。 } public static void main(String[] args) { 
    String token = JwtHelper.createToken(7L, "admin"); System.out.println(token); System.out.println(JwtHelper.getUserId(token)); System.out.println(JwtHelper.getUserName(token)); } } 

redis配置类

将当前用户登录信息放到redis中去,设置过期时间

import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.lang.reflect.Method; import java.time.Duration; / * Redis配置类 */ @Configuration @EnableCaching public class RedisConfig { 
    // 使用默认标签做缓存 @Bean public KeyGenerator wiselyKeyGenerator() { 
    return new KeyGenerator() { 
    @Override public Object generate(Object target, Method method, Object... params) { 
    StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { 
    sb.append(obj.toString()); } return sb.toString(); } }; } // 声明模板 /* ref = 表示引用 value = 具体的值 <bean class="org.springframework.data.redis.core.RedisTemplate" > <property name="defaultSerializer" ref = ""> </bean> */ // 工具类: @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { 
    RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); // 将Redis 中 string ,hash 数据类型,自动序列化! redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { 
    RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); //解决查询缓存转换异常的问题 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置序列化(解决乱码的问题),过期时间600秒 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofDays(365)) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; } } 

设置常量类

/ * Redis常量配置类 * set name admin */ public class RedisConst { 
    public static final String SKUKEY_PREFIX = "sku:"; public static final String SKUKEY_SUFFIX = ":info"; //单位:秒 public static final long SKUKEY_TIMEOUT = 24 * 60 * 60; // 定义变量,记录空对象的缓存过期时间 缓存穿透key的过期时间 public static final long SKUKEY_TEMPORARY_TIMEOUT = 10 * 60; //单位:秒 尝试获取锁的最大等待时间 public static final long SKULOCK_EXPIRE_PX1 = 1; //单位:秒 锁的持有时间 public static final long SKULOCK_EXPIRE_PX2 = 1; public static final String SKULOCK_SUFFIX = ":lock"; public static final String USER_KEY_PREFIX = "user:"; public static final String USER_CART_KEY_SUFFIX = ":cart"; public static final long USER_CART_EXPIRE = 60 * 60 * 24 * 7; public static final String SROCK_INFO = "stock:info:"; public static final String ORDER_REPEAT = "order:repeat:"; //用户登录 public static final String USER_LOGIN_KEY_PREFIX = "user:login:"; public static final String ADMIN_LOGIN_KEY_PREFIX = "admin:login:"; // public static final String userinfoKey_suffix = ":info"; public static final int USERKEY_TIMEOUT = 365; public static final String ORDER_SKU_MAP = "order:sku:"; //秒杀商品前缀 public static final String SECKILL_TIME_MAP = "seckill:time:map"; public static final String SECKILL_SKU_MAP = "seckill:sku:map"; public static final String SECKILL_SKU_LIST = "seckill:sku:list:"; public static final String SECKILL_USER_MAP = "seckill:user:map:"; public static final String SECKILL_ORDERS_USERS = "seckill:orders:users"; public static final String SECKILL_STOCK_PREFIX = "seckill:stock:"; public static final String SECKILL_USER = "seckill:user:"; //用户锁定时间 单位:秒 public static final int SECKILL__TIMEOUT = 60 * 60 * 1; } 
 redis: host: localhost port: 6379 database: 0 timeout:  password: lettuce: pool: max-active: 20 #最大连接数 max-wait: -1 #最大阻塞等待时间(负数表示没限制) max-idle: 5 #最大空闲 min-idle: 0 #最小空闲 

微信配置

wx: open: # 小程序微信公众平台appId app_id: wxcc651fcbab275e33 # 小程序微信公众平台api秘钥 app_secret: 5fa2eae7ff6ceda383e924c5f6 

读取微信配置

package com.atguigu.ssyx.user.utils; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ConstantPropertiesUtil implements InitializingBean { 
    @Value("${wx.open.app_id}") private String appId; @Value("${wx.open.app_secret}") private String appSecret; public static String WX_OPEN_APP_ID; public static String WX_OPEN_APP_SECRET; @Override public void afterPropertiesSet() throws Exception { 
    WX_OPEN_APP_ID = appId; WX_OPEN_APP_SECRET = appSecret; } } 

添加HttpClient工具类

用于给微信发送请求,获取openid和session_key

import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig.Builder; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContextBuilder; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.conn.ssl.X509HostnameVerifier; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import java.io.IOException; import java.net.SocketTimeoutException; import java.security.GeneralSecurityException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class HttpClientUtils { 
    public static final int connTimeout=10000; public static final int readTimeout=10000; public static final String charset="UTF-8"; private static HttpClient client = null; static { 
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(128); cm.setDefaultMaxPerRoute(128); client = HttpClients.custom().setConnectionManager(cm).build(); } public static String postParameters(String url, String parameterStr) throws ConnectTimeoutException, SocketTimeoutException, Exception{ 
    return post(url,parameterStr,"application/x-www-form-urlencoded",charset,connTimeout,readTimeout); } public static String postParameters(String url, String parameterStr,String charset, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception{ 
    return post(url,parameterStr,"application/x-www-form-urlencoded",charset,connTimeout,readTimeout); } public static String postParameters(String url, Map<String, String> params) throws ConnectTimeoutException, SocketTimeoutException, Exception { 
    return postForm(url, params, null, connTimeout, readTimeout); } public static String postParameters(String url, Map<String, String> params, Integer connTimeout,Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { 
    return postForm(url, params, null, connTimeout, readTimeout); } public static String get(String url) throws Exception { 
    return get(url, charset, null, null); } public static String get(String url, String charset) throws Exception { 
    return get(url, charset, connTimeout, readTimeout); } / * 发送一个 Post 请求, 使用指定的字符集编码. * * @param url * @param body RequestBody * @param mimeType 例如 application/xml "application/x-www-form-urlencoded" a=1&b=2&c=3 * @param charset 编码 * @param connTimeout 建立链接超时时间,毫秒. * @param readTimeout 响应超时时间,毫秒. * @return ResponseBody, 使用指定的字符集编码. * @throws ConnectTimeoutException 建立链接超时异常 * @throws SocketTimeoutException 响应超时 * @throws Exception */ public static String post(String url, String body, String mimeType,String charset, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { 
    HttpClient client = null; HttpPost post = new HttpPost(url); String result = ""; try { 
    if (StringUtils.isNotBlank(body)) { 
    HttpEntity entity = new StringEntity(body, ContentType.create(mimeType, charset)); post.setEntity(entity); } // 设置参数 Builder customReqConf = RequestConfig.custom(); if (connTimeout != null) { 
    customReqConf.setConnectTimeout(connTimeout); } if (readTimeout != null) { 
    customReqConf.setSocketTimeout(readTimeout); } post.setConfig(customReqConf.build()); HttpResponse res; if (url.startsWith("https")) { 
    // 执行 Https 请求. client = createSSLInsecureClient(); res = client.execute(post); } else { 
    // 执行 Http 请求. client = HttpClientUtils.client; res = client.execute(post); } result = IOUtils.toString(res.getEntity().getContent(), charset); } finally { 
    post.releaseConnection(); if (url.startsWith("https") && client != null&& client instanceof CloseableHttpClient) { 
    ((CloseableHttpClient) client).close(); } } return result; } / * 提交form表单 * * @param url * @param params * @param connTimeout * @param readTimeout * @return * @throws ConnectTimeoutException * @throws SocketTimeoutException * @throws Exception */ public static String postForm(String url, Map<String, String> params, Map<String, String> headers, Integer connTimeout,Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { 
    HttpClient client = null; HttpPost post = new HttpPost(url); try { 
    if (params != null && !params.isEmpty()) { 
    List<NameValuePair> formParams = new ArrayList<NameValuePair>(); Set<Entry<String, String>> entrySet = params.entrySet(); for (Entry<String, String> entry : entrySet) { 
    formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8); post.setEntity(entity); } if (headers != null && !headers.isEmpty()) { 
    for (Entry<String, String> entry : headers.entrySet()) { 
    post.addHeader(entry.getKey(), entry.getValue()); } } // 设置参数 Builder customReqConf = RequestConfig.custom(); if (connTimeout != null) { 
    customReqConf.setConnectTimeout(connTimeout); } if (readTimeout != null) { 
    customReqConf.setSocketTimeout(readTimeout); } post.setConfig(customReqConf.build()); HttpResponse res = null; if (url.startsWith("https")) { 
    // 执行 Https 请求. client = createSSLInsecureClient(); res = client.execute(post); } else { 
    // 执行 Http 请求. client = HttpClientUtils.client; res = client.execute(post); } return IOUtils.toString(res.getEntity().getContent(), "UTF-8"); } finally { 
    post.releaseConnection(); if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) { 
    ((CloseableHttpClient) client).close(); } } } / * 发送一个 GET 请求 * * @param url * @param charset * @param connTimeout 建立链接超时时间,毫秒. * @param readTimeout 响应超时时间,毫秒. * @return * @throws ConnectTimeoutException 建立链接超时 * @throws SocketTimeoutException 响应超时 * @throws Exception */ public static String get(String url, String charset, Integer connTimeout,Integer readTimeout) throws ConnectTimeoutException,SocketTimeoutException, Exception { 
    HttpClient client = null; HttpGet get = new HttpGet(url); String result = ""; try { 
    // 设置参数 Builder customReqConf = RequestConfig.custom(); if (connTimeout != null) { 
    customReqConf.setConnectTimeout(connTimeout); } if (readTimeout != null) { 
    customReqConf.setSocketTimeout(readTimeout); } get.setConfig(customReqConf.build()); HttpResponse res = null; if (url.startsWith("https")) { 
    // 执行 Https 请求. client = createSSLInsecureClient(); res = client.execute(get); } else { 
    // 执行 Http 请求. client = HttpClientUtils.client; res = client.execute(get); } result = IOUtils.toString(res.getEntity().getContent(), charset); } finally { 
    get.releaseConnection(); if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) { 
    ((CloseableHttpClient) client).close(); } } return result; } / * 从 response 里获取 charset * * @param ressponse * @return */ @SuppressWarnings("unused") private static String getCharsetFromResponse(HttpResponse ressponse) { 
    // Content-Type:text/html; charset=GBK if (ressponse.getEntity() != null && ressponse.getEntity().getContentType() != null && ressponse.getEntity().getContentType().getValue() != null) { 
    String contentType = ressponse.getEntity().getContentType().getValue(); if (contentType.contains("charset=")) { 
    return contentType.substring(contentType.indexOf("charset=") + 8); } } return null; } / * 创建 SSL连接 * @return * @throws GeneralSecurityException */ private static CloseableHttpClient createSSLInsecureClient() throws GeneralSecurityException { 
    try { 
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { 
    public boolean isTrusted(X509Certificate[] chain,String authType) throws CertificateException { 
    return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() { 
    @Override public boolean verify(String arg0, SSLSession arg1) { 
    return true; } @Override public void verify(String host, SSLSocket ssl) throws IOException { 
    } @Override public void verify(String host, X509Certificate cert) throws SSLException { 
    } @Override public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException { 
    } }); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (GeneralSecurityException e) { 
    throw e; } } } 

核心代码

@RestController @RequestMapping("/api/user/weixin") public class WeixinApiController { 
    @Autowired private UserService userService; @Autowired private RedisTemplate redisTemplate; @ApiOperation(value = "微信登录获取openid(小程序)") @GetMapping("/wxLogin/{code}") public Result loginWx(@PathVariable String code){ 
    //1 得到微信返回的code临时票据 //2 拿着code + 小程序id +小程序密钥 请求微信接口服务 // 使用HttpClient工具类 //小程序id String wxOpenAppId = ConstantPropertiesUtil.WX_OPEN_APP_ID; //小程序密钥 String wxOpenAppSecret = ConstantPropertiesUtil.WX_OPEN_APP_SECRET; //get请求 //拼接请求地址+参数 StringBuffer url = new StringBuffer() .append("https://api.weixin..com/sns/jscode2session") .append("?appid=%s") .append("&secret=%s") .append("&js_code=%s") .append("&grant_type=authorization_code"); String tokenUrl = String.format(url.toString(), wxOpenAppId, wxOpenAppSecret, code); //使用httpClient String result = null; try { 
    result = HttpClientUtils.get(tokenUrl); } catch (Exception e) { 
    throw new SsyxException(ResultCodeEnum.FETCH_ACCESSTOKEN_FAILD); } //3 请求微信接口服务,返回两个值 session_key 和 openid // openId是微信标识 JSONObject jsonObject = JSONObject.parseObject(result); String sessionKey = jsonObject.getString("session_key"); String openId = jsonObject.getString("openid"); //4 添加微信用户信息到数据库中去 // 操作user表 //判断是否是第一次微信登录 根据openId查询 User user = userService.getUserByOpenId(openId); if (user == null){ 
    user = new User(); user.setOpenId(openId); user.setNickName(openId); user.setPhotoUrl(""); user.setUserType(UserType.USER); user.setIsNew(0); userService.save(user); } //5 根据userId查询提货点和团长信息 // 提货点 user_delivery // 团长 leader LeaderAddressVo leaderAddressVo = userService.getLeaderAddressByUserId(user.getId()); //6 使用JWT工具根据userId和username生成token字符串 String token = JwtHelper.createToken(user.getId(), user.getNickName()); //7 获取当前登录用户信息,放到Redis中去,设置有效时间 UserLoginVo userLoginVo = userService.getUserLoginVo(user.getId()); redisTemplate.opsForValue() .set(RedisConst.USER_LOGIN_KEY_PREFIX + user.getId(), userLoginVo, RedisConst.USERKEY_TIMEOUT, TimeUnit.MINUTES); //8 封装到map中返回 Map<String, Object> map = new HashMap<>(); map.put("user",user); map.put("token",token); map.put("leaderAddressVo",leaderAddressVo); return Result.ok(map); } } 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/127314.html

(0)
上一篇 2025-09-12 12:15
下一篇 2025-07-24 14:20

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信