⼿把⼿教你使⽤java对接-签名验证1、进⼊登录界⾯,先⽤⾃⼰的去注册服务号。
我们在测试的时候可以通过申请测试号进⾏开发,开发完成后再切换到正式环境。
登录成功后是这个页⾯,但是接⼝配置信息应该是没有的,因为我已经配置了。
接⼝来校验签名,签名校验规则⽂档中也说了。
3、编写签名验证接⼝
1)编写WxCheckSignatureController。
ller;
baties.service.WxCheckSignatureService;
代码转换import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Description:
* @Author: lst
* @Date 2020-08-18
*/
@RestController
@Api(value = "WxCheckSignatureController", tags = "验证消息的确来⾃服务器")
public class WxCheckSignatureController {
@Autowired
private WxCheckSignatureService wxCheckSignatureService;
/**
* @Description 验证消息的确来⾃服务器,签名验证
* @author lst
* @date 2020-8-18 12:03
* @param signature 加密签名
* @param timestamp 时间戳
* @param nonce 随机数
* @param echostr 随机字符串
* @return java.lang.String
*/
@GetMapping(value = "/check-token", produces = "application/json; charset=utf-8")
@ApiOperation(value = "验证消息的确来⾃服务器,签名验证", notes = "验证消息的确来⾃服务器,签名验证", code = 200, produces = "application/json" @ApiImplicitParams({
@ApiImplicitParam(paramType = "query", dataType = "string", name = "signature",required = true ,value = "加密签名,signature结合了开发者填写的toke @ApiImplicitParam(paramType = "query", dataType = "string", name = "timestamp",required = true ,value = "时间戳"),
@ApiImplicitParam(paramType = "query", dataType = "string", name = "nonce",required = true ,value = "随机数"),
@ApiImplicitParam(paramType = "query", dataType = "string", name = "echostr",required = true ,value = "随机字符串")
})
public String checkToken(@RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce, @RequestParam("echostr") String echostr) {
return wxCheckSignatureService.checkSignature(signature, timestamp, nonce,echostr);
}
}
2)、编写实现类WxCheckSignatureServiceImpl、WxCheckSignatureService、ShaUtil
baties.service.impl;
baties.service.WxCheckSignatureService;
baties.utils.ShaUtil;
slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* @Description:
* @Author: lst
* @Date 2020-08-18
*/
@Slf4j
@Service
public class WxCheckSignatureServiceImpl implements WxCheckSignatureService {
@Value("${wx.token}")
public String token;
/
**
* @Description 进⾏签名认证
* @author lst
* @date 2020-8-20 10:49
* @param signature 加密签名
* @param timestamp 时间戳
* @param nonce 随机数
* @param echostr 随机字符串
* @return java.lang.String
*/
@Override
public String checkSignature(String signature, String timestamp, String nonce, String echostr) { // 1.将token、timestamp、nonce三个参数进⾏字典序排序
log.info("signature:{},token:{},timestamp:{},nonce:{}",signature,token,timestamp,nonce);
String tmpStr = SHA1(token, timestamp, nonce);
//TODO 进⾏对⽐
log.info("随机字符串echostr:{}",echostr);
log.info("tmpStr:{}",tmpStr);
if (tmpStr.UpperCase())) {
return echostr;
}
return null;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论