springboot+jwt+springSecurity微信小程序授權(quán)登錄問題
場景重現(xiàn):1.微信小程序向后臺(tái)發(fā)送請求 ——而后臺(tái)web采用的springSecuriry沒有token生成,就會(huì)攔截請求,,所以小編記錄下這個(gè)問題
微信小程序授權(quán)登錄問題
思路
參考網(wǎng)上一大堆資料 核心關(guān)鍵字: 自定義授權(quán)+鑒權(quán) (說的通俗就是解決辦法就是改造springSecurity的過濾器)
參考文章
http://www.dbjr.com.cn/article/204704.htm
總的來說的
通過自定義的WxAppletAuthenticationFilter替換默認(rèn)的
UsernamePasswordAuthenticationFilter
,在UsernamePasswordAuthenticationFilter中可任意定制自己的登錄方式。
springSecurity的原來的登錄過濾器UsernamePasswordAuthenticationFilter
采用賬戶+密碼的形式
說明我微信小程序這里很有可能不適用要升級(jí),因?yàn)槲⑿判〕绦虿捎胦penid的形式登錄,而沒有password
用戶認(rèn)證
需要結(jié)合JWT來實(shí)現(xiàn)用戶認(rèn)證,第一步登錄成功后如何頒發(fā)token。
關(guān)鍵點(diǎn)
使用cn.hutool.http請求第三方數(shù)據(jù)
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.5.16</version> </dependency>
說明:請求第三方數(shù)據(jù)時(shí),需要授權(quán)。
第三方(微信小程序)會(huì)給到appid和secret,請求攜帶appid和secret獲取一個(gè)token和expires,又了token就又了操作第三方數(shù)據(jù)的權(quán)限。
每次操作第三方數(shù)據(jù)時(shí)就需要攜帶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; /** * 用戶認(rèn)證通過的處理handler */ public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler { @Autowired private JwtTokenUtils jwtTokenUtils; @Override public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException { // 使用jwt管理,所以封裝用戶信息生成jwt響應(yīng)給前端 String token = jwtTokenUtils.generateToken(((WxAppletAuthenticationToken)authentication).getOpenid()); Map<String, Object> result = new HashMap<>(); result.put(ConstantEnum.AUTHORIZATION.getValue(), token); httpServletResponse.setContentType(ContentType.JSON.toString()); httpServletResponse.getWriter().write(JSON.toJSONString(result)); } }
總結(jié)
發(fā)現(xiàn)微信小程序和后臺(tái)使用一個(gè)項(xiàng)目的話,會(huì)有 不能使用多個(gè)WebSecurityConfig這個(gè)錯(cuò)誤,暫時(shí)只想到這里了
到此這篇關(guān)于springboot+jwt+springSecurity微信小程序授權(quán)登錄問題的文章就介紹到這了,更多相關(guān)springboot+jwt+springSecurity微信小程序授權(quán)登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot整合SpringSecurity實(shí)現(xiàn)登錄認(rèn)證和鑒權(quán)全過程
- SpringBoot整合SpringSecurity和JWT和Redis實(shí)現(xiàn)統(tǒng)一鑒權(quán)認(rèn)證
- SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)動(dòng)態(tài)權(quán)限問題
- SpringBoot集成SpringSecurity和JWT做登陸鑒權(quán)的實(shí)現(xiàn)
- SpringSecurity動(dòng)態(tài)加載用戶角色權(quán)限實(shí)現(xiàn)登錄及鑒權(quán)功能
- SpringSecurity實(shí)現(xiàn)權(quán)限認(rèn)證與授權(quán)的使用示例
- SpringSecurity進(jìn)行認(rèn)證與授權(quán)的示例代碼
- springSecurity用戶認(rèn)證和授權(quán)的實(shí)現(xiàn)
- 深入淺析springsecurity入門登錄授權(quán)
- mall整合SpringSecurity及JWT實(shí)現(xiàn)認(rèn)證授權(quán)實(shí)戰(zhàn)
- SpringSecurity頁面授權(quán)與登錄驗(yàn)證實(shí)現(xiàn)(內(nèi)存取值與數(shù)據(jù)庫取值)
- SpringSecurity 鑒權(quán)與授權(quán)的具體使用
相關(guān)文章
Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫
本篇文章主要介紹了Java用 Rhino/Nashorn 代替第三方 JSON 轉(zhuǎn)換庫,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05SpringBoot 統(tǒng)一請求返回的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot 統(tǒng)一請求返回的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07mybatis group by substr函數(shù)傳參報(bào)錯(cuò)的解決
這篇文章主要介紹了mybatis group by substr函數(shù)傳參報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01springboot默認(rèn)日志框架選擇源碼解析(推薦)
這篇文章主要介紹了springboot默認(rèn)日志框架選擇源碼解析(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03