java-weixin-tools接入微信

第一步:添加服务器配置

第二步:验证来自微信服务器的消息

@RequestMapping("/weixin")
@Controller  
public class WeixinController {
    @Autowired
    private WeixinService wxService;
    
    /**
     * 微信接入
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @return
     */
    @RequestMapping("/check")
    @ResponseBody
    public String checkSignature(@RequestParam String signature, @RequestParam String timestamp,
            @RequestParam String nonce, @RequestParam String echostr) {
        return wxService.checkSignature(signature, timestamp, nonce, echostr);
    }
}

 

public interface WeixinService {
    public String checkSignature(String signature,String timestamp,String nonce,String echostr);
}

 

@Service
public class WeixinServiceImpl implements WeixinService {

    /**
     * 微信接入
     */
    @Override
    public String checkSignature(String signature, String timestamp,
            String nonce, String echostr) {
        if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
            return echostr;
        }
        ;
        return null;
    }
}

 

public class CheckUtil {
    public static final String  tooken = "xiang"; //开发者自行定义Tooken
    public static boolean checkSignature(String signature,String timestamp,String nonce){
    //1.定义数组存放tooken,timestamp,nonce
    String[] arr = {tooken,timestamp,nonce};

    //2.对数组进行排序
    Arrays.sort(arr);

    //3.生成字符串
    StringBuffer sb = new StringBuffer();
    for(String s : arr){
    sb.append(s);
    }

    //4.sha1加密,网上均有现成代码
    String temp = getSha1(sb.toString());

    //5.将加密后的字符串,与微信传来的加密签名比较,返回结果
    return temp.equals(signature);

    }


    public static String getSha1(String str){
            if(str==null||str.length()==0){
                return null;
            }
            char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
                    'a','b','c','d','e','f'};
            try {
                MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
                mdTemp.update(str.getBytes("UTF-8"));
                byte[] md = mdTemp.digest();
                int j = md.length;
                char buf[] = new char[j*2];
                int k = 0;
                for (int i = 0; i < j; i++) {
                    byte byte0 = md[i];
                    buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                    buf[k++] = hexDigits[byte0 & 0xf];      
                }
                return new String(buf);
            } catch (Exception e) {
                return null;
            }
        }
}

 大家觉得不错的话 可以支持一下

转载于:https://www.cnblogs.com/xiangxiang521/p/9378422.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值