前言
我们有时候在开发中,遇到这样的问题,就是我们需要小程序授权登录我们自己的后台,通过小程序的信息换取我们自己后台的token,实现账号密码、小程序授权登录的多种登录方式。
配置
在 SecurityConfig文件中配置
XcxAuthenticationProvider
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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(); } } |
小程序授权登录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
@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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://juejin.cn/post/7072602870792585230