springboot+jwt+微信小程序授权登录获取token的方法实例
作者:泉城老铁 时间:2022-07-11 17:53:57
前言
我们有时候在开发中,遇到这样的问题,就是我们需要小程序授权登录我们自己的后台,通过小程序的信息换取我们自己后台的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的功能了,希望可以帮助到大家
来源:https://juejin.cn/post/7072602870792585230
标签:springboot,小程序,授权登录,获取token
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
maven多个仓库查询的优先级顺序案例讲解
2023-08-16 02:33:23
Java实现多线程断点下载
2022-04-27 00:29:16
![](https://img.aspxhome.com/file/2023/9/76319_0s.png)
深入理解Java高级特性——注解
2021-05-23 20:28:54
![](https://img.aspxhome.com/file/2023/2/62702_0s.png)
Java创建多线程的两种方式对比
2023-09-14 21:02:49
![](https://img.aspxhome.com/file/2023/2/58562_0s.png)
Spring中的aware接口详情
2023-11-29 10:48:29
![](https://img.aspxhome.com/file/2023/9/60689_0s.webp)
SpringBoot实现单文件与多文件上传功能
2023-03-22 23:44:11
![](https://img.aspxhome.com/file/2023/9/76229_0s.png)
c#在excel中添加超链接示例分享
2023-06-03 14:10:45
C#控制台程序中使用官方依赖注入的实现
2021-07-12 15:35:09
![](https://img.aspxhome.com/file/2023/2/83342_0s.png)
Java压缩解压zip技术_动力节点Java学院整理
2022-08-29 22:50:42
![](https://img.aspxhome.com/file/2023/8/84208_0s.png)
实例讲解Java 自旋锁
2021-09-17 09:38:50
![](https://img.aspxhome.com/file/2023/4/70914_0s.png)
Java文件操作之IO流 File类的使用详解
2023-07-26 00:49:41
java设计模式--三种工厂模式详解
2023-11-29 09:54:06
![](https://img.aspxhome.com/file/2023/7/60337_0s.png)
浅谈C#六大设计原则
2023-05-02 16:29:58
C# Winform实现石头剪刀布游戏
2022-03-17 17:05:14
SpringMVC数据输出相关知识总结
2022-04-06 10:35:06
![](https://img.aspxhome.com/file/2023/1/62711_0s.png)
简单了解redis常见客户端及Sharding机制原理
2022-03-28 16:49:09
C#绘制时钟的方法
2021-08-25 15:18:12
![](https://img.aspxhome.com/file/2023/0/91240_0s.jpg)
C#编译器对局部变量的优化指南
2023-06-23 14:54:12
Java实现简单QQ聊天工具
2023-11-25 05:54:52
![](https://img.aspxhome.com/file/2023/0/60130_0s.jpg)
Java双冒号(::)运算符使用详解
2022-03-31 11:18:17
![](https://img.aspxhome.com/file/2023/2/65612_0s.png)