[feat]:用户微信登录模块,登录注册微信用户数据
This commit is contained in:
+11
@@ -110,6 +110,17 @@ public class SysUserController {
|
||||
return appUserService.findByOpenId(openId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据WxOpenId查询用户信息
|
||||
*
|
||||
* @param openId openId
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/wxOpenId", params = "openId")
|
||||
@Operation(summary = "根据WxOpenId查询用户")
|
||||
public SysUser findByWxOpenId(String openId) throws Exception {
|
||||
return appUserService.findByWxOpenid(openId);
|
||||
}
|
||||
|
||||
@GetMapping("/users/{id}")
|
||||
public SysUser findUserById(@PathVariable Long id) {
|
||||
return appUserService.getById(id);
|
||||
|
||||
+2
-1
@@ -76,7 +76,8 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
||||
@Override
|
||||
public SysUser findByWxOpenid(String wxOpenid) throws Exception {
|
||||
SysUser sysUser = this.selectByOpenId(wxOpenid);
|
||||
if (sysUser.getId() == null) {
|
||||
if (ObjectUtils.isEmpty(sysUser)) {
|
||||
sysUser = new SysUser();
|
||||
sysUser.setOpenId(wxOpenid);
|
||||
sysUser.setUsername(wxOpenid);
|
||||
sysUser.setType(UserType.APP.name());
|
||||
|
||||
+22
-28
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user