栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot+jwt+springSecurity微信小程序授权登录问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot+jwt+springSecurity微信小程序授权登录问题

场景重现:1.微信小程序向后台发送请求 ——而后台web采用的springSecuriry没有token生成,就会拦截请求,,所以小编记录下这个问题

微信小程序授权登录问题 思路

参考网上一大堆资料 核心关键字: 自定义授权+鉴权 (说的通俗就是解决办法就是改造springSecurity的过滤器)

参考文章

https://www.jb51.net/article/204704.htm

总的来说的

通过自定义的WxAppletAuthenticationFilter替换默认的UsernamePasswordAuthenticationFilter,在UsernamePasswordAuthenticationFilter中可任意定制自己的登录方式。

springSecurity的原来的登录过滤器UsernamePasswordAuthenticationFilter

采用账户+密码的形式

说明我微信小程序这里很有可能不适用要升级,因为微信小程序采用openid的形式登录,而没有password

用户认证

需要结合JWT来实现用户认证,第一步登录成功后如何颁发token。

关键点

使用cn.hutool.http请求第三方数据

 
  cn.hutool
  hutool-all
  4.5.16
 

说明:请求第三方数据时,需要授权。

第三方(微信小程序)会给到appid和secret,请求携带appid和secret获取一个token和expires,又了token就又了操作第三方数据的权限。

每次操作第三方数据时就需要携带token。

package com.shbykj.springboot.wx.security.handler;

import cn.hutool.http.ContentType;
import com.alibaba.fastjson.JSON;
import com.shbykj.springboot.wx.enums.ConstantEnum;
import com.shbykj.springboot.wx.security.WxAppletAuthenticationToken;
import com.shbykj.springboot.wx.util.JwtTokenUtils;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

 @Autowired
 private JwtTokenUtils jwtTokenUtils;

 @Override
 public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
 // 使用jwt管理,所以封装用户信息生成jwt响应给前端
 String token = jwtTokenUtils.generateToken(((WxAppletAuthenticationToken)authentication).getOpenid());
 Map result = new HashMap<>();
 result.put(ConstantEnum.AUTHORIZATION.getValue(), token);
 httpServletResponse.setContentType(ContentType.JSON.toString());
 httpServletResponse.getWriter().write(JSON.toJSonString(result));
 }
}
总结

发现微信小程序和后台使用一个项目的话,会有 不能使用多个WebSecurityConfig这个错误,暂时只想到这里了

到此这篇关于springboot+jwt+springSecurity微信小程序授权登录问题的文章就介绍到这了,更多相关springboot+jwt+springSecurity微信小程序授权登录内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/129263.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号