[feat]:用户微信登录模块,登录注册微信用户数据
This commit is contained in:
+11
@@ -110,6 +110,17 @@ public class SysUserController {
|
|||||||
return appUserService.findByOpenId(openId);
|
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}")
|
@GetMapping("/users/{id}")
|
||||||
public SysUser findUserById(@PathVariable Long id) {
|
public SysUser findUserById(@PathVariable Long id) {
|
||||||
return appUserService.getById(id);
|
return appUserService.getById(id);
|
||||||
|
|||||||
+2
-1
@@ -76,7 +76,8 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
|||||||
@Override
|
@Override
|
||||||
public SysUser findByWxOpenid(String wxOpenid) throws Exception {
|
public SysUser findByWxOpenid(String wxOpenid) throws Exception {
|
||||||
SysUser sysUser = this.selectByOpenId(wxOpenid);
|
SysUser sysUser = this.selectByOpenId(wxOpenid);
|
||||||
if (sysUser.getId() == null) {
|
if (ObjectUtils.isEmpty(sysUser)) {
|
||||||
|
sysUser = new SysUser();
|
||||||
sysUser.setOpenId(wxOpenid);
|
sysUser.setOpenId(wxOpenid);
|
||||||
sysUser.setUsername(wxOpenid);
|
sysUser.setUsername(wxOpenid);
|
||||||
sysUser.setType(UserType.APP.name());
|
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.service.impl.UserDetailServiceFactory;
|
||||||
import com.central.oauth.support.base.BaseAuthenticationProvider;
|
import com.central.oauth.support.base.BaseAuthenticationProvider;
|
||||||
import com.central.oauth2.common.token.TencentAuthenticationToken;
|
import com.central.oauth2.common.token.TencentAuthenticationToken;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.*;
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.core.AuthenticationException;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
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.AuthorizationGrantType;
|
||||||
import org.springframework.security.oauth2.core.OAuth2Token;
|
import org.springframework.security.oauth2.core.OAuth2Token;
|
||||||
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
||||||
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator;
|
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -92,33 +90,29 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
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();
|
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<>();
|
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
|
||||||
params.put("appid", "appId");
|
|
||||||
params.put("secret", "appSecret");
|
|
||||||
params.put("js_code", code);
|
|
||||||
params.put("grant_type", "authorization_code");
|
|
||||||
|
|
||||||
HttpEntity<Map<String, String>> entity = new HttpEntity<>(params, headers);
|
ResponseEntity<String> responseEntity = restTemplate.exchange("https://api.weixin.qq.com/sns/jscode2session", HttpMethod.POST, requestEntity, String.class);
|
||||||
ResponseEntity<Map> response = restTemplate.exchange(
|
|
||||||
"WX_LOGIN_URL",
|
|
||||||
HttpMethod.POST,
|
|
||||||
entity,
|
|
||||||
Map.class
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
Map<String, Object> body = response.getBody();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
if (body != null && body.containsKey("openid")) {
|
try {
|
||||||
return body;
|
return objectMapper.readValue(responseEntity.getBody(), Map.class);
|
||||||
} else {
|
} catch (Exception e) {
|
||||||
throw new CustomOAuth2AuthenticationException("Failed to obtain OpenID token");
|
throw new CustomOAuth2AuthenticationException("Failed obtain wxOpenid:"+ e.getMessage());
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
throw new CustomOAuth2AuthenticationException("Failed obtain wxOpenid");
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
throw new CustomOAuth2AuthenticationException("Request failed: " + response.getStatusCode());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user