前端对此接口进行请求
package com.chuang.bootplus.controller.wx;import com.alibaba.fastjson.JSONObject;
import com.chuang.bootplus.entity.UserInfo;
import com.chuang.bootplus.service.UserInfoService;
import com.chuang.bootplus.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@RestController
@CrossOrigin
@RequestMapping("/wx")
public class WxLogin2 {@AutowiredUserInfoService userInfoService;//小程序端登录接收code@PostMapping("/login")@CrossOriginpublic Map<String, String> login(@RequestParam("code") String code,@RequestParam("CName") String CName,@RequestParam("Cimgurl") String url) {// 配置请求参数Map<String, String> param = new HashMap<>();param.put("appid", "");param.put("secret", "");param.put("js_code", code);param.put("grant_type","authorization_code" );// 发送请求String wxResult = HttpClientUtil.doGet("https://api.weixin.qq.com/sns/jscode2session", param);JSONObject jsonObject = JSONObject.parseObject(wxResult);// 获取参数返回的System.out.println(wxResult);String session_key = jsonObject.get("session_key").toString();System.out.println(session_key);String open_id = jsonObject.get("openid").toString();System.out.println(open_id);// 根据返回的实体类,判断用户是否是新用户,是的话,将用户信息存到数据库UserInfo userInfo = new UserInfo();UserInfo byId = userInfoService.getById(open_id);if (byId != null) {Map<String, String> result = new HashMap<>();result.put("Cname", byId.getNickname());result.put("CImgUrl", byId.getAvatar());
// result.put("CNum",byId);result.put("session_key", session_key);result.put("open_id", open_id);return result;} else {userInfo.setNickname(CName);userInfo.setAvatar(url);userInfo.setSession_key(session_key);userInfo.setOpenid(open_id);// 添加到数据库Boolean flag = userInfoService.save(userInfo);}// 封装返回小程序Map<String, String> result = new HashMap<>();result.put("session_key", session_key);result.put("open_id", open_id);return result;}}
后端返回
session_key
openid
需要用到的util类
package com.chuang.bootplus.utils;import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;import java.io.IOException;
import java.net.URI;
import java.util.Map;public class HttpClientUtil {public static String doGet(String url, Map<String, String> param) {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();String resultString = "";CloseableHttpResponse response = null;try {// 创建uriURIBuilder builder = new URIBuilder(url);if (param != null) {for (String key : param.keySet()) {builder.addParameter(key, param.get(key));}}URI uri = builder.build();// 创建http GET请求HttpGet httpGet = new HttpGet(uri);// 执行请求response = httpclient.execute(httpGet);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == 200) {resultString = EntityUtils.toString(response.getEntity(), "UTF-8");}} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpclient.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}public static String doGet(String url) {return doGet(url, null);}
}
全部的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.shengxian</groupId><artifactId>shop</artifactId><version>0.0.1-SNAPSHOT</version><name>shop</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties>
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-thymeleaf</artifactId>-->
<!-- </dependency>-->
<!-- <!– 模板引擎 –>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.velocity</groupId>-->
<!-- <artifactId>velocity-engine-core</artifactId>-->
<!-- <version>2.0</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.projectlombok</groupId>-->
<!-- <artifactId>lombok</artifactId>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- <!– mybatis-plus –>-->
<!-- <!– mybatis-plus 是自己开发,并非官方的! –>-->
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus-boot-starter</artifactId>-->
<!-- <version>3.0.5</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.swagger</groupId>-->
<!-- <artifactId>swagger-annotations</artifactId>-->
<!-- <version>1.5.22</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>commons-httpclient</groupId>-->
<!-- <artifactId>commons-httpclient</artifactId>-->
<!-- <version>3.0.1</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.apache.commons</groupId>-->
<!-- <artifactId>commons-io</artifactId>-->
<!-- <version>1.3.2</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.apache.commons</groupId>-->
<!-- <artifactId>commons-lang3</artifactId>-->
<!-- <version>3.4</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.apache.httpcomponents</groupId>-->
<!-- <artifactId>httpclient</artifactId>-->
<!-- <version>4.3.2</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>fastjson</artifactId>-->
<!-- <version>1.2.38</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.junit.vintage</groupId>-->
<!-- <artifactId>junit-vintage-engine</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.aspectj</groupId>-->
<!-- <artifactId>aspectjweaver</artifactId>-->
<!-- <version>1.8.13</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.apache.commons</groupId>-->
<!-- <artifactId>commons-pool2</artifactId>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.apache.shiro</groupId>-->
<!-- <artifactId>shiro-spring</artifactId>-->
<!-- <version>1.4.1</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>commons-httpclient</groupId>-->
<!-- <artifactId>commons-httpclient</artifactId>-->
<!-- <version>3.0.1</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-devtools</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.commons</groupId>-->
<!-- <artifactId>commons-io</artifactId>-->
<!-- <version>1.3.2</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.apache.commons</groupId>-->
<!-- <artifactId>commons-lang3</artifactId>-->
<!-- <version>3.4</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.apache.httpcomponents</groupId>-->
<!-- <artifactId>httpclient</artifactId>-->
<!-- <version>4.3.2</version>-->
<!-- </dependency>-->
<!-- <!–httpClint–>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.httpcomponents</groupId>-->
<!-- <artifactId>httpclient</artifactId>-->
<!-- <version>4.5.5</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>org.apache.httpcomponents</groupId>-->
<!-- <artifactId>httpmime</artifactId>-->
<!-- <version>4.5</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>fastjson</artifactId>-->
<!-- <version>1.2.38</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>com.auth0</groupId>-->
<!-- <artifactId>java-jwt</artifactId>-->
<!-- <version>3.4.1</version>-->
<!-- </dependency>--><!-- <dependency>-->
<!-- <groupId>com.github.binarywang</groupId>-->
<!-- <artifactId>weixin-java-miniapp</artifactId>-->
<!-- <version>3.3.0</version>-->
<!-- </dependency>--><!-- <!–springSecurity–>-->
<!-- <!–<dependency>–>-->
<!-- <!– <groupId>org.springframework.boot</groupId>–>-->
<!-- <!– <artifactId>spring-boot-starter-security</artifactId>–>-->
<!-- <!– <version>2.3.7.RELEASE</version>–>-->
<!-- <!–</dependency>–>--><!-- <!–<dependency>–>-->
<!-- <!– <groupId>com.fasterxml.jackson.core</groupId>–>-->
<!-- <!– <artifactId>jackson-databind</artifactId>–>-->
<!-- <!– <version>2.11.3</version>–>-->
<!-- <!–</dependency>–>--><!-- <!–<!–thymeleaf和security整合–>–>-->
<!-- <!–<dependency>–>-->
<!-- <!– <groupId>org.thymeleaf.extras</groupId>–>-->
<!-- <!– <artifactId>thymeleaf-extras-springsecurity5</artifactId>–>-->
<!-- <!– <version>3.0.4.RELEASE</version>–>-->
<!-- <!–</dependency>–>--><!-- </dependencies>--><dependencies><dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId><version>0.4.8</version></dependency><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-jdbc</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.1.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
<!-- <!– 模板引擎 –>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.velocity</groupId>-->
<!-- <artifactId>velocity-engine-core</artifactId>-->
<!-- <version>2.0</version>-->
<!-- </dependency>--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- mybatis-plus --><!-- mybatis-plus 是自己开发,并非官方的! --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version></dependency><dependency><groupId>io.swagger</groupId><artifactId>swagger-annotations</artifactId><version>1.5.22</version></dependency><!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.junit.vintage</groupId>-->
<!-- <artifactId>junit-vintage-engine</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>--><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><!--springSecurity--><!--<dependency>--><!-- <groupId>org.springframework.boot</groupId>--><!-- <artifactId>spring-boot-starter-security</artifactId>--><!-- <version>2.3.7.RELEASE</version>--><!--</dependency>--><!--<dependency>--><!-- <groupId>com.fasterxml.jackson.core</groupId>--><!-- <artifactId>jackson-databind</artifactId>--><!-- <version>2.11.3</version>--><!--</dependency>--><!--<!–thymeleaf和security整合–>--><!--<dependency>--><!-- <groupId>org.thymeleaf.extras</groupId>--><!-- <artifactId>thymeleaf-extras-springsecurity5</artifactId>--><!-- <version>3.0.4.RELEASE</version>--><!--</dependency>--><!-- <dependency>-->
<!-- <groupId>org.apache.shiro</groupId>-->
<!-- <artifactId>shiro-spring</artifactId>-->
<!-- <version>1.4.1</version>-->
<!-- </dependency>--><dependency><groupId>commons-httpclient</groupId><artifactId>commons-httpclient</artifactId><version>3.0.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-io</artifactId><version>1.3.2</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.2</version></dependency><!--httpClint--><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.5</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.5</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.38</version></dependency><dependency><groupId>com.auth0</groupId><artifactId>java-jwt</artifactId><version>3.4.1</version></dependency><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-miniapp</artifactId><version>3.3.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>
</project>