我想做个拦截器,当用户点击某个菜单时校验他的基本信息,当他没有完善信息时,在微信上发个链接,让他点击进入完善信息界面!
/**
* 用一个wxHandler处理对应的用户请求
*/
public static View handle(WxHandler wxHandler, HttpServletRequest req, String key) throws IOException {
if (wxHandler == null) {
log.info("WxHandler is NULL");
return HttpStatusView.HTTP_502;
}
String signature = req.getParameter("signature");
String timestamp = req.getParameter("timestamp");
String nonce = req.getParameter("nonce");
String msg_signature = req.getParameter("msg_signature");
String encrypt_type = req.getParameter("encrypt_type");
if (!wxHandler.check(signature, timestamp, nonce, key)) {
log.info("token is invalid");
return HttpStatusView.HTTP_502;
}
if ("GET".equalsIgnoreCase(req.getMethod())) {
String echostr = req.getParameter("echostr");
log.info("GET? return echostr=" + echostr);
return new ViewWrapper(new RawView(null), echostr);
}
String postData = Streams.readAndClose(new InputStreamReader(req.getInputStream(), Encoding.CHARSET_UTF8));
if ("aes".equals(encrypt_type)) {
WXBizMsgCrypt msgCrypt = wxHandler.getMsgCrypt();
try {
// 若抛出Illegal key size,请更新JDK的加密库为不限制长度
postData = msgCrypt.decryptMsg(msg_signature, timestamp, nonce, postData);
}
catch (AesException e) {
return new HttpStatusView(403);
}
}
WxInMsg in = Wxs.convert(postData);
in.setExtkey(key);
WxOutMsg out = wxHandler.handle(in);
if (out != null) {
Wxs.fix(in, out);
}
return new ViewWrapper(WxView.me, out);
}
参考了这个类,写了
public class UserFilter implements ActionFilter {
private String path;
private CloudBindService cloudBindService= Mvcs.ctx().getDefaultIoc().get(CloudBindService.class);
private WxHandler wxHandler= Mvcs.ctx().getDefaultIoc().get(WxHandler.class);
public UserFilter(String path) {
this.path = path;
}
/**
* return MyBeetlViewMarker.make(path);返回Beetl渲染頁面
*/
@Override
public View match(ActionContext context) {
String openid = Strings.sNull(context.getRequest().getSession().getAttribute("openid"));
NutMap nutMap = cloudBindService.searchById(openid);
if (nutMap.get("data")==null) {
WxInMsg in = new WxInMsg();
in.setContent("1");
in.setMsgType("text");
WxOutMsg out = wxHandler.handle(in);
if (out != null) {
Wxs.fix(in, out);
}
return new ViewWrapper(WxView.me, out);
}
JSONObject json = JSONObject.parseObject(nutMap.get("data").toString());
context.getRequest().getSession().setAttribute("unitid",json.getString("unitid"));
return null;
}
}
怎么返回的是xml格式的报文