首页 > 编程知识 正文

微信扫描登录生成二维码失败,微信登录二维码怎么生成

时间:2023-05-05 20:04:49 阅读:248290 作者:1313

添加配置

application.properties添加相关配置信息

# 微信开放平台 appidwx.open.app_id=你的appid# 微信开放平台 appsecretwx.open.app_secret=你的appsecret# 微信开放平台 重定向urlwx.open.redirect_url=http://你的服务器名称/api/ucenter/wx/callback 创建常量类

创建util包,创建ConstantPropertiesUtil.java常量类

@Componentpublic class ConstantPropertiesUtil implements InitializingBean {@Value("${wx.open.app_id}")private String appId;@Value("${wx.open.app_secret}")private String appSecret;@Value("${wx.open.redirect_url}")private String redirectUrl;public static String WX_OPEN_APP_ID;public static String WX_OPEN_APP_SECRET;public static String WX_OPEN_REDIRECT_URL;@Overridepublic void afterPropertiesSet() throws Exception {WX_OPEN_APP_ID = appId;WX_OPEN_APP_SECRET = appSecret;WX_OPEN_REDIRECT_URL = redirectUrl;}} 创建controller

microservice-ucenter微服务中创建api包

api包中创建WxApiController

@CrossOrigin@Controller//注意这里没有配置 @RestController@RequestMapping("/api/ucenter/wx")public class WxApiController {@GetMapping("login")public String genQrConnect(HttpSession session) {// 微信开放平台授权baseUrlString baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +"?appid=%s" +"&redirect_uri=%s" +"&response_type=code" +"&scope=snsapi_login" +"&state=%s" +"#wechat_redirect";// 回调地址String redirectUrl = ConstantPropertiesUtil.WX_OPEN_REDIRECT_URL; //获取业务服务器重定向地址try {redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8"); //url编码} catch (UnsupportedEncodingException e) {throw new Exception(20001, e.getMessage());}// 防止csrf攻击(跨站请求伪造攻击)//String state = UUID.randomUUID().toString().replaceAll("-", "");//一般情况下会使用一个随机数String state = "imhelen";//为了让大家能够使用我搭建的外网的微信回调跳转服务器,这里填写你在ngrok的前置域名System.out.println("state = " + state);// 采用redis等进行缓存state 使用sessionId为key 30分钟后过期,可配置//键:"wechar-open-state-" + httpServletRequest.getSession().getId()//值:satte//过期时间:30分钟//生成qrcodeUrlString qrcodeUrl = String.format(baseUrl,ConstantPropertiesUtil.WX_OPEN_APP_ID,redirectUrl,state);return "redirect:" + qrcodeUrl;}}

 

win7电脑文件夹如何加密如何使用CSS设置div半透明的效果

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。