dev_xds
姜玉琦 2024-04-17 02:18:03 +08:00
parent 7033395690
commit 817086dd2a
14 changed files with 207 additions and 18 deletions

View File

@ -113,7 +113,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
.antMatchers("/login","/wechat/**", "/common/**", "/register", "/captchaImage","/bgscreen/**","/publics/**","/system/dict/data/**").permitAll()
// 静态资源,可匿名访问
.antMatchers(HttpMethod.GET, "/", "/*.txt", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/**/img/**", "/profile/**").permitAll()
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/**/img/**", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
// 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated()

View File

@ -18,6 +18,19 @@
<dependencies>
<!-- spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 表示依赖不会传递 -->
</dependency>
<!-- SpringBoot集成thymeleaf模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 核心模块-->
<dependency>
<groupId>com.ruoyi</groupId>

View File

@ -0,0 +1,118 @@
package com.ruoyi.web;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserOpenid;
import com.ruoyi.system.service.ISysUserOpenidService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
@Slf4j
@Controller
@RequestMapping("/wechat/wxAuth")
@CrossOrigin(origins = "*",maxAge = 3600)
public class IndexController {
@Autowired
private RedisCache redisCache;
@Autowired
private WxMpService wxMpService;
@Autowired
private ISysUserOpenidService sysUserOpenidService;
/**********************************************************页面跳转*************************************************/
/**
* @author JiangYuQi
* @param:
* @return
*
*/
@Anonymous
@GetMapping("/model")
public String model(Model model,String state) throws Exception {
model.addAttribute("state",state);
return "/wxAuth/index";
}
/**
* @author tao
* @param:
* @return
*
*/
@Anonymous
@GetMapping("/authorize")
public String authorize(String userOpenId, String state, HttpSession HttpSession) throws Exception {
HttpSession.setAttribute("userOpenId", userOpenId);
log.info("【微信网页授权】进来了,参数...{}", userOpenId);
//1. 配置
//2. 调用方法
String url = RuoYiConfig.getProjectUrl() + "wechat/wxAuth/userInfo";
/*
*
* URLEncoder.decode(returnUrl,"UTF-8"
* https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
*/
String resultUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, state);
log.info("【微信网页授权】获取code,result={}", resultUrl);
return "redirect:" + resultUrl;
}
/**
* @author tao
* @param:
* @return
* url
*/
@Anonymous
@GetMapping("/userInfo")
public String userInfo(@RequestParam("code") String code,
@RequestParam("state") String state, HttpSession HttpSession, Model model) throws Exception {
model.addAttribute("state", "NO");
/*urlauthorization code
codeaccess tokenopenid*/
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
try {
//获取access token
wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
log.info("【AccessToken】{}", wxMpOAuth2AccessToken.getAccessToken());
// 拿到openid
String openId = wxMpOAuth2AccessToken.getOpenId();
String userOpenId = HttpSession.getAttribute("userOpenId").toString();
log.info("小程序用户...{}的openid...{}", userOpenId, openId);
if(StringUtils.isNotEmpty(openId) && StringUtils.isNotEmpty(userOpenId)){
SysUserOpenid sysUserOpenid = sysUserOpenidService.selectSysUserOpenidByOpenId(userOpenId);
if(sysUserOpenid!=null){
sysUserOpenid.setMsgOpenId(openId);
sysUserOpenidService.updateSysUserOpenid(sysUserOpenid);
//删除缓存
redisCache.deleteObject(CacheConstants.WX_MPMESSAGE_OPENID +sysUserOpenid.getLoginName());
model.addAttribute("state", "OK");
}
}
} catch (WxErrorException e) {
log.error("【微信网页授权】{}", e);
}
return "wxAuth/index";
}
}

View File

@ -14,6 +14,8 @@ ruoyi:
addressEnabled: false
# 验证码类型 math 数字计算 char 字符验证
captchaType: math
# 服务回调地址
projectUrl: https://szgcwx.jhncidg.com/
# 开发环境配置
server:
# 服务器的HTTP端口默认为8080

View File

@ -14,6 +14,8 @@ ruoyi:
addressEnabled: false
# 验证码类型 math 数字计算 char 字符验证
captchaType: math
# 服务回调地址
projectUrl: https://szgcwx.jhncidg.com/
# 开发环境配置
server:
# 服务器的HTTP端口默认为8080

View File

@ -15,10 +15,6 @@ user:
# Spring配置
spring:
# 微信资源验证访问
web:
resources:
static-locations: classpath:/wxstatic/
# 资源信息
messages:
# 国际化资源文件路径

View File

@ -1,10 +0,0 @@
# 代码生成
gen:
# 作者
author: ruoyi
# 默认生成包路径 system 需改成自己的模块名称 如 system monitor tool
packageName: com.ruoyi.system
# 自动去除表前缀默认是false
autoRemovePre: false
# 表前缀(生成类名不会包含表前缀,多个用逗号分隔)
tablePrefix: sys_

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,64 @@
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8">
<link rel="stylesheet" href="../../css/element-ui.css">
<title>公众号消息绑定</title>
</head>
<style>
.info-btn-blue-margin {
padding-top: 250px;
}
</style>
<body th:if="${state == 'OK'}">
<div class="el-body el-col el-col-24 el-col-sm-12 el-col-lg-6"><div class="el-result"><div class="el-result__icon"><svg viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg" class="icon-success"><path d="M24,4 C35.045695,4 44,12.954305 44,24 C44,35.045695 35.045695,44 24,44 C12.954305,44 4,35.045695 4,24 C4,12.954305 12.954305,4 24,4 Z M34.5548098,16.4485711 C33.9612228,15.8504763 32.9988282,15.8504763 32.4052412,16.4485711 L32.4052412,16.4485711 L21.413757,27.5805811 L21.413757,27.5805811 L21.4034642,27.590855 C21.0097542,27.9781674 20.3766105,27.9729811 19.9892981,27.5792711 L19.9892981,27.5792711 L15.5947588,23.1121428 C15.0011718,22.514048 14.0387772,22.514048 13.4451902,23.1121428 C12.8516033,23.7102376 12.8516033,24.6799409 13.4451902,25.2780357 L13.4451902,25.2780357 L19.6260786,31.5514289 C20.2196656,32.1495237 21.1820602,32.1495237 21.7756472,31.5514289 L21.7756472,31.5514289 L34.5548098,18.614464 C35.1483967,18.0163692 35.1483967,17.0466659 34.5548098,16.4485711 Z"></path></svg></div><div class="el-result__title"><p>公众号消息授权成功</p></div></div></div>
</body>
<body th:if="${state == 'NO'}">
<div class="el-body el-col el-col-24 el-col-sm-12 el-col-lg-6"><div class="el-result"><div class="el-result__icon"><svg viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg" class="icon-error"><path d="M24,4 C35.045695,4 44,12.954305 44,24 C44,35.045695 35.045695,44 24,44 C12.954305,44 4,35.045695 4,24 C4,12.954305 12.954305,4 24,4 Z M32.57818,15.42182 C32.0157534,14.8593933 31.1038797,14.8593933 30.541453,15.42182 L30.541453,15.42182 L24.0006789,21.9625941 L17.458547,15.42182 C16.8961203,14.8593933 15.9842466,14.8593933 15.42182,15.42182 C14.8593933,15.9842466 14.8593933,16.8961203 15.42182,17.458547 L15.42182,17.458547 L21.9639519,23.9993211 L15.42182,30.541453 C14.8593933,31.1038797 14.8593933,32.0157534 15.42182,32.57818 C15.9842466,33.1406067 16.8961203,33.1406067 17.458547,32.57818 L17.458547,32.57818 L24.0006789,26.0360481 L30.541453,32.57818 C31.1038797,33.1406067 32.0157534,33.1406067 32.57818,32.57818 C33.1406067,32.0157534 33.1406067,31.1038797 32.57818,30.541453 L32.57818,30.541453 L26.0374059,23.9993211 L32.57818,17.458547 C33.1406067,16.8961203 33.1406067,15.9842466 32.57818,15.42182 Z"></path></svg></div><div class="el-result__title"><p>公众号消息授权失败</p></div><div class="el-result__subtitle"><p>请关注公众号并进行网页授权</p></div></div></div>
</body>
<button type="button" class="el-button el-button--primary is-round" onclick="colseView()">
<span>返回小程序</span>
</button>
<script type="text/javascript" src="../../js/jquery-3.0.0.min.js"></script>
<script type="text/javascript" src="../../js/jweixin-1.3.2.js"></script>
<script>
function colseView(){
wx.miniProgram.navigateTo({url: '/pages/xiangmugaikuang/index?showUser=true'});
}
</script>
<style type="text/css">
html {
width:100%;
height:100%;
text-align: center;
background: #191d28
url("https://szgcwx.jhncidg.com/staticFiles/img/CORE_40247DD946964A15AA0D4000E1031E19.png")
no-repeat bottom/100%;
}
.el-body{
padding-top: 40%;
padding-bottom: 30%;
}
.el-result__title p {
color: #ffffff !important;
}
.el-result__subtitle p {
color: #f2777a !important;
font-weight: 800;
}
.el-result__extra {
width: 100%;
}
.el-button {
width: 90%;
}
.el-result .icon-info{
fill: #409eff !important;
}
</style>
</html>

View File

@ -1 +0,0 @@
29aef62cd1316f1635063c5b86a3ffee

View File

@ -64,7 +64,7 @@ public class WxAuthController extends BaseController {
* URLEncoder.decode(returnUrl,"UTF-8"
* https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
*/
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_USER_INFO, returnUrl);
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, returnUrl);
log.info("【微信网页授权】获取code,result={}", redirectUrl);
response.sendRedirect(redirectUrl);
}