springboot 实现微信h5授权登录

项目依赖

        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>4.1.0</version>
        </dependency>
                <!--  httpclient-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>

application.yml

### 微信网页(h5)配置参数
wxh5:
 appid: wx57f8a596522dfb99 #公众号APPID (这使用的是测试账户公众号)
 appSecret: 426c9109b51dd1f5183ec506a7d78dbd #公众号AppSecret (这使用的是测试账户公众号)
 serverurl: http://jdqaiv.natappfree.cc  #服务器回调地址(最好是https协议)

响应数据格式

package com.admin.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.stereotype.Component;

/**
 * @author kuaiting
 */
@Accessors(chain = true)
@Data
@Component
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData<T> {

    /**
     * 是 否 成 功
     */

    private boolean success;

    /**
     * 错 误 状 态
     */

    private Integer code;

    /**
     * 操作消 息
     */

    private String msg;

    /**
     * 返 回 数 据
     */

    private T data;


    /**
     * 成 功 操 作 , 携 带 数 据
     */
    public static <T> ResponseData<T> success(T data) {
        return success(Constants.SUCCESS, "操作成功", data);
    }

    /**
     * 成 功 操 作, 携 带 消 息
     */
    public static <T> ResponseData<T> success(String message) {
        return success(message, null);
    }

    /**
     * 成 功 操 作, 携 带 消 息 和 携 带 数 据
     */
    public static <T> ResponseData<T> success(String message, T data) {
        return success(Constants.SUCCESS, message, data);
    }

    /**
     * 成 功 操 作, 携 带 自 定 义 状 态 码 和 消 息
     */
    public static <T> ResponseData<T> success(Integer code, String message) {
        return success(code, message, null);
    }

    /**
     * 成 功 操 作, 携 带 自 定义 状 态 码, 消 息 和 数 据
     */
    public static <T> ResponseData<T> success(Integer code, String message, T data) {
        ResponseData<T> result = new ResponseData<T>();
        result.setCode(code);
        result.setMsg(message);
        result.setSuccess(true);
        result.setData(data);
        return result;
    }

    /**
     * 失 败 操 作, 默 认 数 据
     *
     * @param userAccountExpired
     */
    public static <T> ResponseData<T> failure(ResultCode userAccountExpired) {
        return failure(userAccountExpired.USER_ACCOUNT_EXPIRED);
    }

    /**
     * 失 败 操 作, 携 带 自 定 义 消 息
     */
    public static <T> ResponseData<T> failure(String message) {
        return failure(message, null);
    }

    /**
     * 失 败 操 作, 携 带 自 定 义 消 息 和 数 据
     */
    public static <T> ResponseData<T> failure(String message, T data) {
        return failure(Constants.FAIL, message, data);
    }

    /**
     * 失 败 操 作, 携 带 自 定 义 状 态 码 和 自 定 义 消 息
     */
    public static <T> ResponseData<T> failure(Integer code, String message) {
        return failure(code, message, null);
    }

    /**
     * 失 败 操 作, 携 带 自 定 义 状 态 码 , 消 息 和 数 据
     */
    public static <T> ResponseData<T> failure(Integer code, String message, T data) {
        ResponseData<T> result = new ResponseData<T>();
        result.setCode(code);
        result.setMsg(message);
        result.setSuccess(false);
        result.setData(data);
        return result;
    }



}

1. 获取Code的H5链接

Controller

@GetMapping("/wxh5codeurl")
	public ResponseData<String> getWeixinH5Code() throws IOException, ParseException {
		return loginService.getWeixinH5Code();
	}

Service

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
	@Value("${wxh5.appid}")
	private String appid;
	@Value("${wxh5.appSecret}")
	private String appSecret;
	@Value("${wxh5.serverurl}")
	private String serverurl;

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private UserServiceImpl userService;
	@Override
	public ResponseData<String> getWeixinH5Code() throws IOException, ParseException {

		String scope = "snsapi_userinfo";
		String redirecturl = URLEncoder.encode(serverurl + "/login/callbackH5code", "UTF-8");
		String url = WeixinUtil.getWeixinH5Code(appid, appSecret, redirecturl, scope);
		return ResponseData.success("操作成功", url);
	}
	}

WeixinUtil

	public static String getWeixinH5Code(String appid, String appSecret, String redirectUrl, String scope) throws IOException, ParseException {
		if (scope == null) {
			scope = "snsapi_userinfo";
		}

		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";

		return url
				.replace("APPID", appid)
				.replace("SECRET", appSecret)
				.replace("REDIRECT_URI", redirectUrl)
				.replace("SCOPE", scope).replace("STATE", getRandomString(20)).trim();
	}

getRandomString方法


	/**
	 * 获取随机字符串
	 */

	public static String getRandomString(int length) {
		String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(62);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}

2. 通过微信h5链接获取的code去获取 AccessToken

Controller

	/**
	 * 通过微信h5链接获取的code去获取 AccessToken
	 * @param code
	 * @return ResponseData<String>
	 */
	@GetMapping("/callbackH5code")
	public ResponseData<Map<String, Object>> authorCallback(@RequestParam("code") String code) {
		return loginService.getWxH5AccessToken(code);
	}

Service

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
	@Value("${wxh5.appid}")
	private String appid;
	@Value("${wxh5.appSecret}")
	private String appSecret;
	@Value("${wxh5.serverurl}")
	private String serverurl;

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private UserServiceImpl userService;
	@Override
	public ResponseData<Map<String, Object>> getWxH5AccessToken(String code) {
		Map<String, Object> map = WeixinUtil.getWxH5AccessToken(appid, appSecret, code);
		return ResponseData.success(map);
	}
	}

WeixinUtil


	public static Map<String, Object> getWxH5AccessToken(String appid, String appSecret, String code) {
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
		String newurl = url
				.replace("APPID", appid)
				.replace("SECRET", appSecret)
				.replace("CODE", code).trim();
		JSONObject jsonObject = JSONObject.parseObject(HttpClient.get(newurl));

		Map<String, Object> map = new HashMap<>();
		map.put("access_token", jsonObject.get("access_token"));
		map.put("expires_in", jsonObject.get("expires_in"));
		map.put("refresh_token", jsonObject.get("refresh_token"));
		map.put("openid", jsonObject.get("openid"));
		map.put("scope", jsonObject.get("scope"));
		return map;
	}

HttpClient

package com.admin.utils.util;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author kuaiting
 * @date 2021/12/18
 * @apiNote
 */
public class HttpClient {
    private String url;
    private Map<String, String> param;
    private int statusCode;
    private String content;
    private String xmlParam;
    private boolean isHttps;


    public boolean isHttps() {
        return isHttps;
    }

    public void setHttps(boolean isHttps) {
        this.isHttps = isHttps;
    }

    public String getXmlParam() {
        return xmlParam;
    }

    public void setXmlParam(String xmlParam) {
        this.xmlParam = xmlParam;
    }

    public HttpClient(String url, Map<String, String> param) {
        this.url = url;
        this.param = param;
    }

    public HttpClient(String url) {
        this.url = url;
    }

    public void setParameter(Map<String, String> map) {
        param = map;
    }

    public void addParameter(String key, String value) {
        if (param == null){
            param = new HashMap<String, String>();

        }
        param.put(key, value);
    }

    public void post() throws ClientProtocolException, IOException {
        HttpPost http = new HttpPost(url);
        setEntity(http);
        execute(http);
    }

    public void put() throws ClientProtocolException, IOException {
        HttpPut http = new HttpPut(url);
        setEntity(http);
        execute(http);
    }

    public void get() throws ClientProtocolException, IOException {
        if (param != null) {
            StringBuilder url = new StringBuilder(this.url);
            boolean isFirst = true;
            for (String key : param.keySet()) {
                if (isFirst) {
                    url.append("?");
                } else {
                    url.append("&");
                }
                url.append(key).append("=").append(param.get(key));
            }
            this.url = url.toString();
        }
        HttpGet http = new HttpGet(url);
        execute(http);
    }

    /**
     * set http post,put param
     */
    private void setEntity(HttpEntityEnclosingRequestBase http) {
        if (param != null) {
            List<NameValuePair> nvps = new LinkedList<NameValuePair>();
            for (String key : param.keySet()) {
                nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数
            }
            http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
        }
        if (xmlParam != null) {
            http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
        }
    }

    private void execute(HttpUriRequest http) throws ClientProtocolException,
            IOException {
        CloseableHttpClient httpClient = null;
        try {
            if (isHttps) {
                SSLContext sslContext = new SSLContextBuilder()
                        .loadTrustMaterial(null, new TrustStrategy() {
                            // 信任所有
                            @Override
                            public boolean isTrusted(X509Certificate[] chain,
                                                     String authType)
                                    throws CertificateException {
                                return true;
                            }
                        }).build();
                SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                        sslContext);
                httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
                        .build();
            } else {
                httpClient = HttpClients.createDefault();
            }
            try (CloseableHttpResponse response = httpClient.execute(http)) {
                if (response != null) {
                    if (response.getStatusLine() != null) {
                        statusCode = response.getStatusLine().getStatusCode();
                    }
                    HttpEntity entity = response.getEntity();
                    // 响应内容
                    content = EntityUtils.toString(entity, Consts.UTF_8);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
        }
    }


    public static String sendJson(String url, String json) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }



    public static String get(String url)  {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpGet httpGet = new HttpGet(url);
            // 执行http请求
            response = httpClient.execute(httpGet);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return resultString;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public String getContent() throws ParseException, IOException {
        return content;
    }

}

3. 获取用户信息

	@GetMapping("/getH5UserInfo")
	public ResponseData<User> getH5UserInfo(@RequestParam("access_token") String access_token, @RequestParam("openid") String openid) throws Exception {
		return loginService.getH5UserInfo(access_token, openid);
	}

Service

	@Override
	public ResponseData<User> getH5UserInfo(String accesstoken, String openid) throws Exception {
		JSONObject jsonObject = WeixinUtil.getH5UserInfo(accesstoken, openid);
		System.out.println(jsonObject);
		if (StringUtils.isEmpty(jsonObject)) {
			return ResponseData.failure("");
		}

		QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
		userQueryWrapper.eq("h5_open_id", jsonObject.get("openid"));
		User user = userService.getOne(userQueryWrapper, true);

		if (StrUtil.isBlankIfStr(user)) {
			if (StrUtil.isEmptyIfStr(user.getAvatar())) {
				user.setAvatar(jsonObject.get("headimgurl").toString());
			}
			if (StrUtil.isEmptyIfStr(user.getUserName())) {
				user.setAvatar(jsonObject.get("nickname").toString());
			}
			if (StrUtil.isEmptyIfStr(user.getSex())) {
				user.setAvatar(jsonObject.get("sex").toString());
			}
		}

		return ResponseData.success(user);

WeixinUtil

	public static JSONObject getH5UserInfo(String accesstoken, String openid) throws Exception {
		String url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
		String newurl = url
				.replace("ACCESS_TOKEN", accesstoken)
				.replace("OPENID", openid).trim();
		return JSONObject.parseObject(HttpClient.get(newurl));


	}

拿到用户信息你就可以做您想做的事情了。
注意:这里使用的是微信公众号实现的

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

缘不易

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值