微信开放平台服务集成

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> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.3</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId> <artifactId>spring-boot-starter-mail</artifactId>
@ -123,6 +128,7 @@
<dependency> <dependency>
<groupId>commons-codec</groupId> <groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId> <artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.jsonwebtoken</groupId> <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> * @author <a href="https://github.com/007gzs">007</a>
*/ */
@RestController @RestController
@RequestMapping("/notify") @RequestMapping("/wx/notify")
public class WxOpenNotifyController { public class WxOpenNotifyController {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired @Autowired
@ -52,7 +52,7 @@ public class WxOpenNotifyController {
return "success"; return "success";
} }
@RequestMapping("{appId}/callback") @RequestMapping("/{appId}/callback")
public Object callback(@RequestBody(required = false) String requestBody, public Object callback(@RequestBody(required = false) String requestBody,
@PathVariable("appId") String appId, @PathVariable("appId") String appId,
@RequestParam("signature") String signature, @RequestParam("signature") String signature,

View File

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