怎么在springboot中使用jwt与springSecurity实现一个微信小程序授权登录功能

  介绍

怎么在springboot中使用jwt与springSecurity实现一个微信小程序授权登录功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

通过自定义的WxAppletAuthenticationFilter替换默认的<代码> UsernamePasswordAuthenticationFilter>

springSecurity的原来的登录过滤器<代码> UsernamePasswordAuthenticationFilter

怎么在springboot中使用jwt与springSecurity实现一个微信小程序授权登录功能

采用账户+密码的形式

怎么在springboot中使用jwt与springSecurity实现一个微信小程序授权登录功能

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

用户认证

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

关键点

<强>使用cn.hutool。http请求第三方数据

, & lt; dependency>   & lt;才能groupId> cn.hutool</groupId>   & lt;才能artifactId> hutool-all</artifactId>   & lt;才能version> 4.5.16</version>   ,& lt;/dependency>

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

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

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

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 身份验证),throws  IOException, ServletException  {//大敌;使用jwt管理,所以封装用户信息生成jwt响应给前端   ,String  token =, jwtTokenUtils.generateToken (((WxAppletAuthenticationToken)身份验证).getOpenid ());   ,Map<字符串,Object>, result =, new  HashMap<在();   ,result.put (ConstantEnum.AUTHORIZATION.getValue(),,令牌);   ,httpServletResponse.setContentType (ContentType.JSON.toString ());   ,httpServletResponse.getWriter () .write (JSON.toJSONString(结果));   ,}   }

看完上述内容,你们掌握怎么在springboot中使用jwt与springSecurity实现一个微信小程序授权登录功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

怎么在springboot中使用jwt与springSecurity实现一个微信小程序授权登录功能