springboot+jwt+springSecurity微信小程序授權(quán)登錄問(wèn)題
場(chǎng)景重現(xiàn):1.微信小程序向后臺(tái)發(fā)送請(qǐng)求 ——而后臺(tái)web采用的springSecuriry沒有token生成,就會(huì)攔截請(qǐng)求,,所以小編記錄下這個(gè)問(wèn)題
微信小程序授權(quán)登錄問(wèn)題
思路
參考網(wǎng)上一大堆資料 核心關(guān)鍵字: 自定義授權(quán)+鑒權(quán) (說(shuō)的通俗就是解決辦法就是改造springSecurity的過(guò)濾器)
參考文章
http://www.dbjr.com.cn/article/204704.htm
總的來(lái)說(shuō)的
通過(guò)自定義的WxAppletAuthenticationFilter替換默認(rèn)的
UsernamePasswordAuthenticationFilter
,在UsernamePasswordAuthenticationFilter中可任意定制自己的登錄方式。
springSecurity的原來(lái)的登錄過(guò)濾器UsernamePasswordAuthenticationFilter
采用賬戶+密碼的形式
說(shuō)明我微信小程序這里很有可能不適用要升級(jí),因?yàn)槲⑿判〕绦虿捎胦penid的形式登錄,而沒有password
用戶認(rèn)證
需要結(jié)合JWT來(lái)實(shí)現(xiàn)用戶認(rèn)證,第一步登錄成功后如何頒發(fā)token。
關(guān)鍵點(diǎn)
使用cn.hutool.http請(qǐng)求第三方數(shù)據(jù)
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.5.16</version> </dependency>
說(shuō)明:請(qǐng)求第三方數(shù)據(jù)時(shí),需要授權(quán)。
第三方(微信小程序)會(huì)給到appid和secret,請(qǐng)求攜帶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)證通過(guò)的處理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)登錄問(wèn)題的文章就介紹到這了,更多相關(guān)springboot+jwt+springSecurity微信小程序授權(quán)登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java如何實(shí)現(xiàn)驗(yàn)證碼驗(yàn)證功能
這篇文章主要教大家如何實(shí)現(xiàn)Java驗(yàn)證碼驗(yàn)證功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02SpringBoot項(xiàng)目部署到服務(wù)器上的方法(Jar包)
這篇文章主要介紹了SpringBoot項(xiàng)目部署到服務(wù)器上的方法(Jar包),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01System.getProperty(user.dir)定位問(wèn)題解析
System.getProperty(user.dir) 獲取的是啟動(dòng)項(xiàng)目的容器位置,用IDEA是項(xiàng)目的根目錄,部署在tomcat上是tomcat的啟動(dòng)路徑,即tomcat/bin的位置,這篇文章主要介紹了System.getProperty(user.dir)定位問(wèn)題,需要的朋友可以參考下2023-05-05Spring Security+Spring Data Jpa如何進(jìn)行安全管理
這篇文章主要介紹了Spring Security+Spring Data Jpa如何進(jìn)行安全管理,幫助大家更好的理解和學(xué)習(xí)Spring Security框架,感興趣的朋友可以了解下2020-09-09Java對(duì)象轉(zhuǎn)Json,關(guān)于@JSONField對(duì)象字段重命名和順序問(wèn)題
這篇文章主要介紹了Java對(duì)象轉(zhuǎn)Json,關(guān)于@JSONField對(duì)象字段重命名和順序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08一個(gè)MIDP俄羅斯方塊游戲的設(shè)計(jì)和實(shí)現(xiàn)
一個(gè)MIDP俄羅斯方塊游戲的設(shè)計(jì)和實(shí)現(xiàn)...2006-12-12