1.登录微信公众号平台
2.设置
3.授权url配置,
签名model类:
public class JsConfigVo {
private String appId;
private String timestamp;
private String nonceStr;
private String signature;
private String url;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getNonceStr() {
return nonceStr;
}
public void setNonceStr(String nonceStr) {
this.nonceStr = nonceStr;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
授权:
@Controller
@RequestMapping("/api/js")
public class WxSdkController {
@Autowired
private final static Logger LOG = LoggerFactory.getLogger(WxSdkController.class);
/**
* 微信JS-SDK
* @param request
* @return
*/
@Autowired
private TaskExecutor task;
@Autowired
private WeixinAccountService weixinAccountService;
@RequestMapping(value = "/sdk",method ={RequestMethod.GET,RequestMethod.POST})
@ResponseBody
public Result wxsdk(String signUrl){
signUrl=URLDecoder.decode(signUrl);
final Result result = Result.newInstance();
ResultObj obj=new ResultObj();
try {
WeixinAccount weixinAccount = weixinAccountService.findOne(WeixinPublicPlatform.DJ17.getDescription());
String appId = weixinAccount.getAppId();
String appSecret = weixinAccount.getAppSecret();
ApiConfig config = new ApiConfig(appId, appSecret,true);
String ticket = config.getJsApiTicket();
String jsapi_ticket = ticket;
String noncestr = WxJsConfigUtils.getNoncestr();
String timestamp = WxJsConfigUtils.getTimestamp();
String str = "jsapi_ticket="+jsapi_ticket+"&noncestr="+noncestr+"×tamp="+timestamp+"&url="+signUrl;
System.out.println(str);
String signature =WxJsConfigUtils.newSHA1(str);
System.out.println(signature);
JsConfigVo vo = new JsConfigVo();
vo.setAppId(appId);
vo.setNonceStr(noncestr);
vo.setTimestamp(timestamp);
vo.setSignature(signature);
vo.setUrl(signUrl);
obj.put("jsSdk", vo);
result.setObj(obj);
result.setCode(ResultCode.SUCCESS);
result.setSuccess(true);
result.setMsg("获取配置成功");
} catch (Exception e) {
LOG.error(e.getMessage(), e);
result.setSuccess(false);
result.setCode(ResultCode.SERVICE_EXCEPTION);
result.setMsg(Constants.EXCEPTION_MSG);
}
return result;
}
}