com.jfinal.weixin.sdk.api.ApiConfig Java Examples

The following examples show how to use com.jfinal.weixin.sdk.api.ApiConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WechatApiConfigInterceptor.java    From jboot with Apache License 2.0 6 votes vote down vote up
@Override
public void intercept(Invocation inv) {
    try {
        JbootWechatController controller = (JbootWechatController) inv.getController();
        ApiConfig config = controller.getApiConfig();

        if (config == null) {
            inv.getController().renderText("error : cannot get apiconfig,please config jboot.properties");
            return;
        }

        ApiConfigKit.setThreadLocalAppId(config.getAppId());
        inv.invoke();
    } finally {
        ApiConfigKit.removeThreadLocalAppId();
    }
}
 
Example #2
Source File: WeixinMsgController.java    From jfinal-weixin with Apache License 2.0 6 votes vote down vote up
/**
 * 如果要支持多公众账号,只需要在此返回各个公众号对应的  ApiConfig 对象即可
 * 可以通过在请求 url 中挂参数来动态从数据库中获取 ApiConfig 属性值
 */
public ApiConfig getApiConfig() {
	ApiConfig ac = new ApiConfig();
	
	// 配置微信 API 相关常量
	ac.setToken(PropKit.get("token"));
	ac.setAppId(PropKit.get("appId"));
	ac.setAppSecret(PropKit.get("appSecret"));
	
	/**
	 *  是否对消息进行加密,对应于微信平台的消息加解密方式:
	 *  1:true进行加密且必须配置 encodingAesKey
	 *  2:false采用明文模式,同时也支持混合模式
	 */
	ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
	ac.setEncodingAesKey(PropKit.get("encodingAesKey", "setting it in config file"));
	return ac;
}
 
Example #3
Source File: JfinalConfigListener.java    From jboot-admin with Apache License 2.0 5 votes vote down vote up
@Override
public void onJFinalStarted() {
    JbootWechatConfig wechatConfig = Jboot.config(JbootWechatConfig.class);
    ApiConfig apiConfig = new ApiConfig();
    apiConfig.setAppId(wechatConfig.getAppId());
    apiConfig.setAppSecret(wechatConfig.getAppSecret());
    apiConfig.setToken(wechatConfig.getToken());
    ApiConfigKit.putApiConfig(apiConfig);
}
 
Example #4
Source File: JbootWechatConfig.java    From jboot with Apache License 2.0 5 votes vote down vote up
public ApiConfig getApiConfig() {

        if (!isConfigOk()) {
            return null;
        }

        ApiConfig config = new ApiConfig();
        config.setAppId(appId);
        config.setAppSecret(appSecret);
        config.setToken(token);
        return config;
    }
 
Example #5
Source File: OptionInitializer.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 设置微信的相关配置
 */
private void initWechatOption() {

    String appId = JPressOptions.get(JPressConsts.OPTION_WECHAT_APPID);
    String appSecret = JPressOptions.get(JPressConsts.OPTION_WECHAT_APPSECRET);
    String token = JPressOptions.get(JPressConsts.OPTION_WECHAT_TOKEN);

    if (StrUtil.areNotEmpty(appId, appSecret, token)) {
        // 配置微信 API 相关参数
        ApiConfig ac = new ApiConfig();
        ac.setAppId(appId);
        ac.setAppSecret(appSecret);
        ac.setToken(token);
        ac.setEncryptMessage(false); //采用明文模式,同时也支持混合模式

        //重新设置后,需要清空之前的配置。
        try {
            Field mapField =  ApiConfigKit.class.getDeclaredField("CFG_MAP");
            mapField.setAccessible(true);
            Map map = (Map) mapField.get(null);
            map.clear();
        } catch (Exception e) {}

        ApiConfigKit.putApiConfig(ac);
    }

    WechatInterceptor.init();

}
 
Example #6
Source File: MsgEncryptKit.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
public static String encrypt(String msg, String timestamp, String nonce) {
	try {
		ApiConfig ac = ApiConfigKit.getApiConfig();
		WXBizMsgCrypt pc = new WXBizMsgCrypt(ac.getToken(), ac.getEncodingAesKey(), ac.getAppId());
		return pc.encryptMsg(msg, timestamp, nonce);
	}
	catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #7
Source File: MsgEncryptKit.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
public static String decrypt(String encryptedMsg, String timestamp, String nonce, String msgSignature) {
	try {
		ApiConfig ac = ApiConfigKit.getApiConfig();
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		StringReader sr = new StringReader(encryptedMsg);
		InputSource is = new InputSource(sr);
		Document document = db.parse(is);
		
		Element root = document.getDocumentElement();
		NodeList nodelist1 = root.getElementsByTagName("Encrypt");
		// NodeList nodelist2 = root.getElementsByTagName("MsgSignature");
		
		String encrypt = nodelist1.item(0).getTextContent();
		// String msgSignature = nodelist2.item(0).getTextContent();
		
		String fromXML = String.format(format, encrypt);
		
		String encodingAesKey = ac.getEncodingAesKey();
		if (encodingAesKey == null)
			throw new IllegalStateException("encodingAesKey can not be null, config encodingAesKey first.");
		
		WXBizMsgCrypt pc = new WXBizMsgCrypt(ac.getToken(), encodingAesKey, ac.getAppId());
		return pc.decryptMsg(msgSignature, timestamp, nonce, fromXML);	// 此处 timestamp 如果与加密前的不同则报签名不正确的异常
	}
	catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #8
Source File: JbootWechatController.java    From jboot with Apache License 2.0 4 votes vote down vote up
public ApiConfig getApiConfig() {
    return ApiConfigKit.getApiConfig();
}
 
Example #9
Source File: MsgController.java    From jfinal-weixin with Apache License 2.0 votes vote down vote up
public abstract ApiConfig getApiConfig(); 
Example #10
Source File: ApiController.java    From jfinal-weixin with Apache License 2.0 votes vote down vote up
public abstract ApiConfig getApiConfig();