springboot+jwt+微信小程序授权登录获取token的方法实例
作者:泉城老铁
本文主要介绍了springboot+jwt+微信小程序授权登录获取token的方法实例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
前言
我们有时候在开发中,遇到这样的问题,就是我们需要小程序授权登录我们自己的后台,通过小程序的信息换取我们自己后台的token,实现账号密码、小程序授权登录的多种登录方式。
配置
在 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); // 此时鉴权成功后,应当重新 new 一个拥有鉴权的 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; /** * 没登录之前,principal我们使用手机号 * @param openid */ public XcxAuthenticationToken(String openid) { super((Collection)null); this.principal = openid; this.setAuthenticated(false); } /** * 登录认证之后,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(); } }
小程序授权登录
@RestController @RequestMapping("/xcx") @AllArgsConstructor @Api(value = "认证模块", tags = "认证模块") 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)) { //过滤掉表情 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); } }
这里就基本完成了小程序的授权登录获取token的功能了,希望可以帮助到大家
到此这篇关于springboot+jwt+微信小程序授权登录获取token的方法实例的文章就介绍到这了,更多相关springboot 小程序授权登录获取token内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!