springboot+jwt+微信小程序授权登录获取token的方法实例

网友投稿 260 2022-08-22


springboot+jwt+微信小程序授权登录获取token的方法实例

目录前言配置XcxAuthenticationProviderXcxAuthenticationToken小程序授权登录

前言

我们有时候在开发中,遇到这样的问题,就是我们需要小程序授权登录我们自己的后台,通过小程序的信息换取我们自己后台的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 principajPnDfuhl;

/**

* 没登录之前,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 IllegalArgumentExcejPnDfuhption {

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 map) {

HashMap 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.lambdaQuery()

.eq(XcxUser::getOpenId, session.getOpenid()), false);

if (ObjectUtil.isNull(user)) {

//过滤掉表情

user = XcxUser.builder()

http:// .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 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的功能了,希望可以帮助到大家


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot webflux 过滤器(使用RouterFunction实现)
下一篇:Java中的反射机制示例详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~