服务器配置
微信公众号接口必须以http://或https://开头,分别支持80端口和443端口。
1、https开头的,要域名(443端口),ip+443端口可以吗(待确认)。
2、验证服务器,后台写代码验证,类似以下代码:(如果服务器配置处写的url为https://xxx.xxx.com/test)
新建一个servlet类(get方法用来给微信验证服务器,post用来业务逻辑),或者Controller:
/**
* Servlet implementation class test
*/
@WebServlet("/test")
public class test extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public test() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String signature=request.getParameter("signature");
String timestamp=request.getParameter("timestamp");
String nonce=request.getParameter("nonce");
String echostr=request.getParameter("echostr");
String token="somelog";//这里填基本配置中的token
String jiami="";
try {
jiami=SHA1.getSHA1(token, timestamp, nonce,"");//这里是对三个参数进行加密
} catch (AesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("加密"+jiami);
System.out.println("本身"+signature);
PrintWriter out=response.getWriter();
if(jiami.equals(signature))
out.print(echostr);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}