preface

Sometimes in the development, we encounter such a problem that we need to log in to our own background through the mini-program authorization, exchange the token of our own background through the information of the mini-program, and realize a variety of login methods such as account password and mini-program authorization.

configuration

Configure in the SecurityConfig file

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); // After the authentication is successful, An authenticated authenticationResult should be new again. Return XcxAuthenticationToken authenticationResult = new XcxAuthenticationToken(userDetails, userDetails.getAuthorities()); authenticationResult.setDetails(authenticationToken.getDetails()); return authenticationResult; } @Override public boolean supports(Class<? > authentication) {/ / figure out whether the authentication SmsCodeAuthenticationToken subclasses of return or child interface XcxAuthenticationToken.class.isAssignableFrom(authentication); } public UserDetailsService getUserDetailsService() { return userDetailsService; } public void setUserDetailsService(UserDetailsService userDetailsService) { this.userDetailsService = userDetailsService; }}Copy the code

XcxAuthenticationToken

public class XcxAuthenticationToken extends AbstractAuthenticationToken { private static final long serialVersionUID = 420L; private final Object principal; @param OpenId */ public XcxAuthenticationToken(String OpenID) {super((Collection)null); this.principal = openid; this.setAuthenticated(false); } /** * Principal We use user information * @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(); }}Copy the code

Applets authorize login

RestController @requestMapping ("/ XCX ") @allargsconstructor @api (value =" Tags = "Authentication module ") public class XcxAuthController {private JwtService JwtService; private JwtUserDetail jwtUserDetail; private XcxUserService userService; private AuthenticationManager authenticationManager; @requestMapping (value = "/login", method = requestmethod. POST) @apiOperation (value = "login", 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); }}Copy the code

Here is the basic completion of the small program authorization login to obtain the token function, I hope to help you