[feat]:用户微信登录模块,登录注册微信用户数据

This commit is contained in:
李世琳
2024-12-17 11:31:32 +08:00
parent 7f0b291311
commit 62a9f8e6ff
3 changed files with 35 additions and 29 deletions
@@ -5,23 +5,21 @@ import com.central.oauth.service.IValidateCodeService;
import com.central.oauth.service.impl.UserDetailServiceFactory;
import com.central.oauth.support.base.BaseAuthenticationProvider;
import com.central.oauth2.common.token.TencentAuthenticationToken;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.OAuth2Token;
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.util.MultiValueMap;
import java.util.HashMap;
import java.util.Map;
@@ -92,33 +90,29 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
}
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("appid", "wx0c3cc6f1d297644d");
params.add("secret", "9d344e6c86b42105b6e4a018f242e233");
params.add("js_code", code);
params.add("grant_type", "authorization_code");
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
headers.setContentType(org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED);
Map<String, String> params = new HashMap<>();
params.put("appid", "appId");
params.put("secret", "appSecret");
params.put("js_code", code);
params.put("grant_type", "authorization_code");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
HttpEntity<Map<String, String>> entity = new HttpEntity<>(params, headers);
ResponseEntity<Map> response = restTemplate.exchange(
"WX_LOGIN_URL",
HttpMethod.POST,
entity,
Map.class
);
ResponseEntity<String> responseEntity = restTemplate.exchange("https://api.weixin.qq.com/sns/jscode2session", HttpMethod.POST, requestEntity, String.class);
if (response.getStatusCode().is2xxSuccessful()) {
Map<String, Object> body = response.getBody();
if (body != null && body.containsKey("openid")) {
return body;
} else {
throw new CustomOAuth2AuthenticationException("Failed to obtain OpenID token");
if (responseEntity.getStatusCode().is2xxSuccessful()) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.readValue(responseEntity.getBody(), Map.class);
} catch (Exception e) {
throw new CustomOAuth2AuthenticationException("Failed obtain wxOpenid:"+ e.getMessage());
}
} else {
throw new CustomOAuth2AuthenticationException("Request failed: " + response.getStatusCode());
}else {
throw new CustomOAuth2AuthenticationException("Failed obtain wxOpenid");
}
}
}