1.application.xml配置
weChat:
appid: ******************
secret: ********************************
code2Session: https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={js_code}&grant_type=authorization_code
2.微信信息实体类
@Component
@Data
@ConfigurationProperties(prefix = "wechat")
public class WechatConfig {
private String appid;
private String secret;
private String code2Session;
private String code;
}
3.登录方法
(1)controller层
@Autowired
private WechatConfig wechatConfig;
@PostMapping(value = "/code2Session")
@ApiOperation(value = "小程序 登录凭证校验", notes = "通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程")
public AjaxResult reloadPwd(@RequestBody @ApiParam("登录时获取的 code") WechatConfig weconfig) {
try {
Map<String, String> params = new HashMap<>();
params.put("appid", wechatConfig.getAppid());
params.put("secret", wechatConfig.getSecret());
params.put("js_code", weconfig.getCode());
log.error("微信请求code2Session-->"+ params);
String result = RestTemplateUtil.getForm(wechatConfig.getCode2Session(), String.class, params);
log.error("微信返回code2Session-->"+ result);
if (!WechatUtils.isSuccess(result)) {
return AjaxResult.error();
}
com.alibaba.fastjson.JSONObject resultObj = JSON.parseObject(result);
//可添加业务逻辑处理
return AjaxResult.success(resultObj);
} catch (Exception e) {
log.error("小程序 登录凭证校验异常:{}", e);
return AjaxResult.error();
}
}
(2)RestTemplateUtil工具类
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.client.AsyncRestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
/**
* created By DoLaLi on 2021/12/6
*/
public class RestTemplateUtil {
/**
* 发送表单参数的post请求
*
* @param url 请求url
* @param param 参数
* @param respType 返回类型
* @return T
*/
public static <T> T postForm(String url, Map<String, List<Object>> param, Class<T> respType) {
return getRestInstance().postForEntity(url, getHttpEntity(param, false), respType).getBody();
}
public static <T> T postFormEx(String url, Map<String, Object> param, Class<T> respType) {
return getRestInstance().postForEntity(url, param, respType).getBody();
}
/**
* @Description: 发送post json请求
* @Param: [url, param, respType]
* @return: T
* @Author: John.Liu
* @Date: 2021-03-12
*/
public static <T> T postJson(String url, Map<String, List<Object>> param, Class<T> respType) {
return getRestInstance().postForEntity(url, getHttpEntity(param, true), respType).getBody();
}
/**
* @Description: 发送post json请求
* @Param: [url, param, respType]
* @return: T
* @Author: John.Liu
* @Date: 2021-03-12
*/
public static <T> T postJsonStr(String url, Map<String, Object> param, Class<T> respType) {
return getRestInstance().postForEntity(url, getHttpEntity(param, true), respType).getBody();
}
/**
* 发送表单参数的异步post请求
*
* @param url 请求url
* @param callback 回调接口
* @param respType 返回类型
*/
public static <T> void asyncPostForm(String url, Map<String, List<Object>> param,
Class<T> respType, ListenableFutureCallback<ResponseEntity<T>> callback) {
getAsyncRestInstance().postForEntity(url, getHttpEntity(param, false), respType).addCallback(callback);
}
/**
* 发送表单有参数get请求
*
* @param url 请求url
* @param param 参数对象
* @param respType 返回类型
* @return T
*/
public static <T> T getForm(String url, Class<T> respType, Map<String,String> param) {
return getRestInstance().getForEntity(url, respType, param).getBody();
}
/**
* @Description: 发送表单无参数的get请求
* @Param: [url, param, respType]
* @return: T
* @Author: tonyzhang
* @Date: 2019-01-18 17:23
*/
public static <T> T getForm(String url, Class<T> respType) {
return getRestInstance().getForObject(url, respType);
}
/**
* 获取HttpEntity实例对象
*
* @param param 参数对象
* @param isJson true 发送json请求,false发送表单请求
* @return HttpEntity
*/
private static <P> HttpEntity<P> getHttpEntity(P param, boolean isJson) {
HttpHeaders headers = new HttpHeaders();
if (isJson) {
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
} else {
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
}
return new HttpEntity<>(param, headers);
}
/*-----------------生产单例对象,方便自定义如何构造对象------------------*/
private static RestTemplate restInit() {
//设置连接超时和读取超时时间
SimpleClientHttpRequestFactory factory=new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(5000);
factory.setReadTimeout(5000);
RestTemplate restTemplate = new RestTemplate(factory);
FormHttpMessageConverter fastConverter = new FormHttpMessageConverter();
WxMappingJackson2 wmc=new WxMappingJackson2();
restTemplate.getMessageConverters().add(fastConverter);
restTemplate.getMessageConverters().add(wmc);
return restTemplate;
}
private static AsyncRestTemplate asyncRestInit() {
return new AsyncRestTemplate();
}
private static RestTemplate getRestInstance() {
return RestSingle.INSTANCE;
}
private static AsyncRestTemplate getAsyncRestInstance() {
return AsyncRestSingle.INSTANCE;
}
private static class RestSingle {
private static final RestTemplate INSTANCE = restInit();
}
private static class AsyncRestSingle {
private static final AsyncRestTemplate INSTANCE = asyncRestInit();
}
}
(3)WxMappingJackson2工具类
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.util.ArrayList;
import java.util.List;
/**
* created By DoLaLi on 2021/12/6
* 封装转换器, 添加更多类型的支持
*/
public class WxMappingJackson2 extends MappingJackson2HttpMessageConverter {
public WxMappingJackson2(){
List<MediaType> mediaTypes=new ArrayList<>();
//添加text/html类型的支持
mediaTypes.add(MediaType.TEXT_HTML);
//添加text/plain类型的支持.微信接口会用到
mediaTypes.add(MediaType.TEXT_PLAIN);
setSupportedMediaTypes(mediaTypes);
}
}
(4)WechatUtils工具类
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
* created By DoLaLi on 2021/12/6
*/
public class WechatUtils {
public static boolean isSuccess(String result){
JSONObject jsonObj = JSON.parseObject(result);
if(jsonObj.containsKey("errcode") && jsonObj.getIntValue("errcode") != 0){
return false;
}
return true;
}
}
结果
微信返回code2Session-->{"session_key":"R7R2EuVVtSzR5M7iQ9Jxqw==","openid":"ohenl3ZDgBlDN6AFaYl7D9OxRl_2","unionid":"orKka6IeooKvpYwd62T3EL71e3Kc"}