Python 官方文档:入门教程 => 点击学习
目录前言配置XcxAuthenticationProviderXcxAuthenticationToken小程序授权登录前言 我们有时候在开发中,遇到这样的问题,就是我们需要小程序授
我们有时候在开发中,遇到这样的问题,就是我们需要小程序授权登录我们自己的后台,通过小程序的信息换取我们自己后台的token,实现账号密码、小程序授权登录的多种登录方式。
在 SecurityConfig文件中配置
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;
}
}
public class XcxAuthenticationToken extends AbstractAuthenticationToken { private static final long serialVersionUID = 420L;
private final Object principal;
public XcxAuthenticationToken(String openid) {
super((Collection)null);
this.principal = openid;
this.setAuthenticated(false);
}
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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: springboot+jwt+微信小程序授权登录获取token的方法实例
本文链接: https://lsjlt.com/news/141898.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0