springboot+jwt+微信小程序授權(quán)登錄獲取token的方法實(shí)例
前言
我們有時(shí)候在開(kāi)發(fā)中,遇到這樣的問(wèn)題,就是我們需要小程序授權(quán)登錄我們自己的后臺(tái),通過(guò)小程序的信息換取我們自己后臺(tái)的token,實(shí)現(xiàn)賬號(hào)密碼、小程序授權(quán)登錄的多種登錄方式。
配置
在 SecurityConfig文件中配置
XcxAuthenticationProvider
public class XcxAuthenticationProvider implements AuthenticationProvider { private UserDetailsService userDetailsService; @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { XcxAuthenticationToken authenticationToken = (XcxAuthenticationToken) authentication; String openId = (String) authenticationToken.getPrincipal(); XcxUserService service= SpringContextUtil.getContext().getBean(XcxUserService.class); UserDetails userDetails = service.loadUserByOpenId(openId); // 此時(shí)鑒權(quán)成功后,應(yīng)當(dāng)重新 new 一個(gè)擁有鑒權(quán)的 authenticationResult 返回 XcxAuthenticationToken authenticationResult = new XcxAuthenticationToken(userDetails, userDetails.getAuthorities()); authenticationResult.setDetails(authenticationToken.getDetails()); return authenticationResult; } @Override public boolean supports(Class<?> authentication) { // 判斷 authentication 是不是 SmsCodeAuthenticationToken 的子類或子接口 return XcxAuthenticationToken.class.isAssignableFrom(authentication); } public UserDetailsService getUserDetailsService() { return userDetailsService; } public void setUserDetailsService(UserDetailsService userDetailsService) { this.userDetailsService = userDetailsService; } }
XcxAuthenticationToken
public class XcxAuthenticationToken extends AbstractAuthenticationToken { private static final long serialVersionUID = 420L; private final Object principal; /** * 沒(méi)登錄之前,principal我們使用手機(jī)號(hào) * @param openid */ public XcxAuthenticationToken(String openid) { super((Collection)null); this.principal = openid; this.setAuthenticated(false); } /** * 登錄認(rèn)證之后,principal我們使用用戶信息 * @param principal * @param authorities */ public XcxAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) { super(authorities); this.principal = principal; super.setAuthenticated(true); } public Object getCredentials() { return null; } public Object getPrincipal() { return this.principal; } public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { if (isAuthenticated) { throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead"); } else { super.setAuthenticated(false); } } public void eraseCredentials() { super.eraseCredentials(); } }
小程序授權(quán)登錄
@RestController @RequestMapping("/xcx") @AllArgsConstructor @Api(value = "認(rèn)證模塊", tags = "認(rèn)證模塊") public class XcxAuthController { private JwtService jwtService; private JwtUserDetail jwtUserDetail; private XcxUserService userService; private AuthenticationManager authenticationManager; @RequestMapping(value = "/login", method = RequestMethod.POST) @ApiOperation(value = "登錄", notes = "登錄") public Result login(@RequestBody Map<String, Object> map) { HashMap<String, Object> hashMap = new HashMap<>(); String code = String.valueOf(map.get("code")); try { WxMaService wxMaService = WxMaConfiguration.getWxMaService(); WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code); XcxUser user = userService.getOne(Wrappers.<XcxUser>lambdaQuery() .eq(XcxUser::getOpenId, session.getOpenid()), false); if (ObjectUtil.isNull(user)) { //過(guò)濾掉表情 user = XcxUser.builder() .openId(session.getOpenid()) // .nickname(wxMpUser.getNickName()) // .avatar(wxMpUser.getAvatarUrl()) .build(); userService.save(user); } else { userService.updateById(user); } UserDetails userDetails = jwtUserDetail.loadUserByOpenId(session.getOpenid()); authenticationManager.authenticate(new XcxAuthenticationToken(session.getOpenid())); Map<String, Object> parse = JSON.parseObject(JSON.toJSONString(userDetails), Map.class); String token = jwtService.createToken(parse); hashMap.put("token", token); hashMap.put("user", userDetails); } catch (Exception e) { e.printStackTrace(); } return Result.success(hashMap); } }
這里就基本完成了小程序的授權(quán)登錄獲取token的功能了,希望可以幫助到大家
到此這篇關(guān)于springboot+jwt+微信小程序授權(quán)登錄獲取token的方法實(shí)例的文章就介紹到這了,更多相關(guān)springboot 小程序授權(quán)登錄獲取token內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot項(xiàng)目中實(shí)現(xiàn)微信小程序登錄案例(最新推薦)
- SpringBoot整合Mybatis-Plus實(shí)現(xiàn)微信注冊(cè)登錄的示例代碼
- 微信小程序使用uni-app和springboot實(shí)現(xiàn)一鍵登錄功能(JWT鑒權(quán))
- springboot實(shí)現(xiàn)微信掃碼登錄的項(xiàng)目實(shí)踐
- 詳解SpringBoot如何實(shí)現(xiàn)整合微信登錄
- 一篇文章帶你入門Springboot整合微信登錄與微信支付(附源碼)
- springboot 微信授權(quán)網(wǎng)頁(yè)登錄操作流程
- SpringBoot實(shí)現(xiàn)微信掃碼登錄的示例代碼
相關(guān)文章
帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹(shù)
這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之2-3-4樹(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01EasyExcel工具讀取Excel空數(shù)據(jù)行問(wèn)題的解決辦法
EasyExcel是阿里巴巴開(kāi)源的一個(gè)excel處理框架,以使用簡(jiǎn)單,節(jié)省內(nèi)存著稱,下面這篇文章主要給大家介紹了關(guān)于EasyExcel工具讀取Excel空數(shù)據(jù)行問(wèn)題的解決辦法,需要的朋友可以參考下2022-08-08Intellij Idea 多模塊Maven工程中模塊之間無(wú)法相互引用問(wèn)題
這篇文章主要介紹了Intellij Idea 多模塊Maven工程中模塊之間無(wú)法相互引用問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01springboot后端如何實(shí)現(xiàn)攜帶token登陸
這篇文章主要介紹了springboot后端如何實(shí)現(xiàn)攜帶token登陸,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11全面解析SpringBoot自動(dòng)配置的實(shí)現(xiàn)原理
這篇文章主要介紹了全面解析SpringBoot自動(dòng)配置的實(shí)現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下2017-05-05Java 日期格式加上指定月數(shù)(一個(gè)期限)得到一個(gè)新日期的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java 日期格式加上指定月數(shù)(一個(gè)期限)得到一個(gè)新日期的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-05-05