微信开放平台服务集成

This commit is contained in:
ronger 2020-03-19 17:47:10 +08:00
parent e28e219428
commit a18d3847d8
4 changed files with 95 additions and 20 deletions

View File

@ -24,6 +24,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
@ -123,6 +128,7 @@
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>

View File

@ -0,0 +1,76 @@
package com.rymcu.vertical.wx.open.controller;
import com.rymcu.vertical.wx.open.handler.WxOpenServiceHandler;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author <a href="https://github.com/007gzs">007</a>
*/
@Controller
@RequestMapping("/wx/open/auth")
public class WxOpenApiController {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Resource
private WxOpenServiceHandler wxOpenServiceHandler;
@GetMapping("/goto_auth_url_show")
@ResponseBody
public String gotoPreAuthUrlShow(){
return "<a href='goto_auth_url'>go</a>";
}
@GetMapping("/goto_auth_url")
public void gotoPreAuthUrl(HttpServletRequest request, HttpServletResponse response){
String host = request.getHeader("host");
String url = "http://"+host+"/vertical-console/wx/open/auth/jump";
try {
url = wxOpenServiceHandler.getWxOpenComponentService().getPreAuthUrl(url);
// 添加来源解决302跳转来源丢失的问题
response.addHeader("Referer", "http://"+host);
response.sendRedirect(url);
} catch (WxErrorException | IOException e) {
logger.error("gotoPreAuthUrl", e);
throw new RuntimeException(e);
}
}
@GetMapping("/jump")
@ResponseBody
public WxOpenQueryAuthResult jump(@RequestParam("auth_code") String authorizationCode){
try {
WxOpenQueryAuthResult queryAuthResult = wxOpenServiceHandler.getWxOpenComponentService().getQueryAuth(authorizationCode);
logger.info("getQueryAuth", queryAuthResult);
return queryAuthResult;
} catch (WxErrorException e) {
logger.error("gotoPreAuthUrl", e);
throw new RuntimeException(e);
}
}
@GetMapping("/get_auth_info")
@ResponseBody
public WxOpenAuthorizerInfoResult getAuthInfo(@RequestParam String appId){
try {
return wxOpenServiceHandler.getWxOpenComponentService().getAuthorizerInfo(appId);
} catch (WxErrorException e) {
logger.error("getAuthInfo", e);
throw new RuntimeException(e);
}
}
}

View File

@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.*;
* @author <a href="https://github.com/007gzs">007</a>
*/
@RestController
@RequestMapping("/notify")
@RequestMapping("/wx/notify")
public class WxOpenNotifyController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
@ -52,7 +52,7 @@ public class WxOpenNotifyController {
return "success";
}
@RequestMapping("{appId}/callback")
@RequestMapping("/{appId}/callback")
public Object callback(@RequestBody(required = false) String requestBody,
@PathVariable("appId") String appId,
@RequestParam("signature") String signature,

View File

@ -1,17 +1,15 @@
package com.rymcu.vertical.wx.open.handler;
import com.rymcu.vertical.core.service.props.DynProps4FilesService;
import com.rymcu.vertical.config.RedisProperties;
import com.rymcu.vertical.wx.open.config.WxOpenProperties;
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenMessageRouter;
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
@ -20,20 +18,17 @@ import javax.annotation.Resource;
* @author ronger
*/
@Service
@EnableConfigurationProperties({WxOpenProperties.class, RedisProperties.class})
public class WxOpenServiceHandler extends WxOpenServiceImpl {
private Logger logger = LoggerFactory.getLogger(getClass());
@Resource
private WxOpenProperties wxOpenProperties;
@Resource
private WxOpenMessageRouter wxOpenMessageRouter;
@Resource
private DynProps4FilesService dynProps4Files;
@Resource
private Environment env;
private RedisProperties redisProperties;
private static JedisPool pool;
private WxOpenMessageRouter wxOpenMessageRouter;
@PostConstruct
public void init() {
@ -54,14 +49,12 @@ public class WxOpenServiceHandler extends WxOpenServiceImpl {
if (pool == null) {
synchronized (WxOpenServiceHandler.class) {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(dynProps4Files.getInt("REDIS_MAX_IDLE", JedisPoolConfig.DEFAULT_MAX_IDLE));
config.setMaxTotal(dynProps4Files.getInt("REDIS_MAX_TOTAL", JedisPoolConfig.DEFAULT_MAX_TOTAL));
config.setMaxWaitMillis(dynProps4Files.getLong("REDIS_MAX_WAIT", JedisPoolConfig.DEFAULT_MAX_WAIT_MILLIS));
config.setTestOnBorrow(true);
pool = new JedisPool(config, env.getProperty("spring.redis.host"),
dynProps4Files.getInt("REDIS_PORT", 6379), dynProps4Files.getInt(
"REDIS_MAX_WAIT", 1000), dynProps4Files.getProperty("REDIS_PASSWORD", env.getProperty("spring.redis.password")));
pool = new JedisPool(redisProperties, redisProperties.getHost(),
redisProperties.getPort(), redisProperties.getConnectionTimeout(),
redisProperties.getSoTimeout(), redisProperties.getPassword(),
redisProperties.getDatabase(), redisProperties.getClientName(),
redisProperties.isSsl(), redisProperties.getSslSocketFactory(),
redisProperties.getSslParameters(), redisProperties.getHostnameVerifier());
}
}
}