Java tutorial
/** * Project: guahao-portal-web-home * * File Created at 2012-11-27 * * Copyright 2012 Greenline.com Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Greenline Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Greenline.com. */ package com.greenline.guahao.web.module.home.controllers.json.validcode; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.greenline.common.util.DESUtil; import com.greenline.common.util.MD5Util; import com.greenline.common.util.RegexUtil; import com.greenline.framework.mursi.web.servlet.view.freemarker.MursiFreeMarkerViewResolver; import com.greenline.guahao.biz.enums.VCodeCachePrefixEnum; import com.greenline.guahao.biz.enums.VerifyTypeEnum; import com.greenline.guahao.biz.manager.cache.order.OrderValidLimitCacheManager; import com.greenline.guahao.biz.manager.cache.valid.CodeCacheManager; import com.greenline.guahao.biz.manager.user.UserManager; import com.greenline.guahao.biz.manager.user.UserVerifyManager; import com.greenline.guahao.biz.manager.user.dataobject.UserDO; import com.greenline.guahao.web.module.common.annotation.MethodRemark; import com.greenline.guahao.web.module.common.constants.EncodeKeyConstants; import com.greenline.guahao.web.module.common.constants.GlobalConstants; import com.greenline.guahao.web.module.common.constants.JsonPathConstants; import com.greenline.guahao.web.module.common.constants.ReservationMsgConstants; import com.greenline.guahao.web.module.common.cookie.UserCookieUtil; import com.greenline.guahao.web.module.common.enums.DomainIndexEnum; import com.greenline.guahao.web.module.common.json.BaseJsonObject; import com.greenline.guahao.web.module.common.json.OperationJsonObject; import com.greenline.message.biz.service.share.sms.SMSResultDTO; import com.greenline.message.biz.service.share.sms.SMSResultDTO.ResultEnum; /** * @Type JsonValidCodeController * @Desc * @author jianyun.zheng * @date 2012-11-27 * @Version V1.0 */ @Controller public class JsonValidCodeController { private static final Log log = LogFactory.getLog(JsonValidCodeController.class); @Resource private HttpServletRequest request; @Resource private MursiFreeMarkerViewResolver mursiFreeMarkerViewResolver; @Resource private UserVerifyManager userVerifyManager; @Resource private UserManager userManager; @Resource private CodeCacheManager codeCacheManager; @Resource private OrderValidLimitCacheManager orderValidLimitCacheManager; // ???? private static final Integer interval = 60000; /** * ???? * * @param mobile * @param type * @param signdata * @param strParam * @return */ @MethodRemark(value = "remark=??,method=ajax,mobile=??,type=,signdata=?") @RequestMapping(value = JsonPathConstants.J_SEND_CODE_PATH, method = RequestMethod.GET) public @ResponseBody BaseJsonObject sendMobileCode(@PathVariable String mobile, @PathVariable String type, @PathVariable String signdata, String strParam) { return this.sendMobileValidCode(mobile, signdata, type, strParam); } private BaseJsonObject sendMobileValidCode(String mobile, String signdata, String type, String strParam) { BaseJsonObject json = new BaseJsonObject(); boolean hasError = Boolean.FALSE; if (RegexUtil.isMobile(StringUtils.trim(mobile))) { // ?? VerifyTypeEnum verEnum = VerifyTypeEnum.valueOf(type); // ??? if (StringUtils.isBlank(strParam) && VerifyTypeEnum.RES_CODE_MOBILE.equals(verEnum)) { log.error("?????(strParam,verEnum):" + strParam + "," + verEnum); return new BaseJsonObject(Boolean.TRUE, "?????,????."); } // // ??? // if (VerifyTypeEnum.PWD_RESET_MOBILE.equals(verEnum)) { // CheckResult result = userManager.isExistLoginId(mobile); // if (!result.isSystemError() && !result.isExist()) { // return new BaseJsonObject(Boolean.TRUE, "??."); // } // } // ? String mobilevalidcodepwd = (String) mursiFreeMarkerViewResolver.getAttributesMap() .get("mobilevalidcodepwd"); String clientsigndata = MD5Util.getMD5Format(type + mobile + mobilevalidcodepwd); if (StringUtils.isBlank(signdata) || !signdata.equals(clientsigndata)) { hasError = Boolean.TRUE; log.error("?????(signdata,clientsigndata):" + signdata + "," + clientsigndata); return new BaseJsonObject(Boolean.TRUE, "?????,????."); } if (!hasError) { String key = mobile; // ?: ???,??id+?id if (StringUtils.isNotBlank(strParam) && VerifyTypeEnum.RES_CODE_MOBILE.equals(verEnum)) { key += strParam; } // ??code String code = codeCacheManager.getCode(verEnum, VCodeCachePrefixEnum.CODE_PRE.getValue() + key); // ??,? if (StringUtils.isBlank(code)) { // ?? code = codeCacheManager.setCode(verEnum, VCodeCachePrefixEnum.CODE_PRE.getValue() + key, 6, orderValidLimitCacheManager.getOrderValidCodeTimeout() * 1000); json = this.sendMobileVerifyCode(mobile, verEnum, code, key); } else { // ???code String intervalCode = codeCacheManager.getCode(verEnum, VCodeCachePrefixEnum.INT_PRE.getValue() + key); // ???? if (StringUtils.isBlank(intervalCode)) { json = this.sendMobileVerifyCode(mobile, verEnum, code, key); } else { // ???? ???? ?????? json.setHasError(true); json.setMessage("?????,????."); } } } } else { json.setHasError(Boolean.TRUE); json.setMessage("??."); } return json; } /** * ???? * * @param model * @param loginId * @return OperationJsonObject */ @MethodRemark(value = "remark=????,method=ajax") @RequestMapping(value = "/json/white/forgetpwd/checkvalidcode", method = RequestMethod.GET) public @ResponseBody OperationJsonObject checkMobileCode(ModelMap model, @RequestParam(value = "mobile", required = true) String mobile, @RequestParam(value = "validcode", required = true) String validcode) { OperationJsonObject json = new OperationJsonObject(); boolean flag = codeCacheManager.verifyMobileCode(VerifyTypeEnum.PWD_RESET_MOBILE, mobile, validcode); if (flag) { // json.setHasError(false); } else { // this.setClearPwdResetCache(mobile); json.setHasError(true); json.setMessage("??????"); } return json; } /** * ???? * * @param mobile */ private void setClearPwdResetCache(String mobile) { if (StringUtils.isNotBlank(mobile)) { codeCacheManager.delCode(VerifyTypeEnum.PWD_RESET_MOBILE, VCodeCachePrefixEnum.CODE_PRE.getValue() + mobile); } } /** * ? key=??+id+?id * * @param code * @param mobile * @return BaseJsonObject */ @MethodRemark(value = "remark=json??,method=ajax,type=??,code=??,mobile=??,strParam=????") @RequestMapping(value = JsonPathConstants.J_CHECK_CODE_PATH, method = RequestMethod.GET) public @ResponseBody BaseJsonObject checkRandCode(@PathVariable String type, @PathVariable String code, @PathVariable String mobile, String validCode, String strParam) { BaseJsonObject json = new BaseJsonObject(); // ?id String userId = UserCookieUtil.getUserId(request) + StringUtils.EMPTY;// "" if (StringUtils.isBlank(userId)) { json.setHasError(true); json.setMessage(""); return json; } UserDO userDO = userManager.findUserByUserId(Long.valueOf(userId)); String encodeUserId = DESUtil.DESEncode(userId, EncodeKeyConstants.USER_ENCODE_KEY); // ???? VerifyTypeEnum verEnum = VerifyTypeEnum.valueOf(type); // ???;key=??+id+?id String key = mobile; String validKey = ""; if (StringUtils.isNotBlank(strParam) && VerifyTypeEnum.RES_CODE_MOBILE.equals(verEnum)) { validKey = encodeUserId + strParam; } key += validKey; if (!DomainIndexEnum.ZFB.equals(request.getAttribute(GlobalConstants.DOMAIN_ENUM)) && userDO.getReg_type() == 3) { if (codeCacheManager.verifyMobileCode(VerifyTypeEnum.RES_CODE_MOBILE, key, code)) { json.setHasError(false); } else { json.setHasError(true); json.setMessage(ReservationMsgConstants.ERR_MSG_MOBILE_CODE_ERROR); } validKey = userId + strParam; // ?? if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(validKey)) { if (!orderValidLimitCacheManager.verifyValidCode(validKey, validCode)) { json.setHasError(true); json.setMessage("????"); return json; } } else { json.setHasError(true); json.setMessage(ReservationMsgConstants.ERR_MSG_CODE_ERROR); return json; } } else { json.setHasError(false); } return json; } /** * ???? * * @param mobile * @param verEnum * @param code * @return */ private BaseJsonObject sendMobileVerifyCode(String mobile, final VerifyTypeEnum verEnum, final String code, final String key) { BaseJsonObject json = new BaseJsonObject(); // ???:false? true? if (codeCacheManager.isIsvalidcode()) { SMSResultDTO smsresultdto = userVerifyManager.sendMobileVerifyCode(mobile, verEnum, code); if (smsresultdto != null) { if (ResultEnum.SUCCESS.equals(smsresultdto.getResult())) { json.setHasError(false); } else if (ResultEnum.OUTLIMIT.equals(smsresultdto.getResult())) { json.setHasError(true); json.setMessage( "??????"); } } } else { json.setHasError(false);// ??? json.setMessage("???"); } // ???? codeCacheManager.setCode(verEnum, VCodeCachePrefixEnum.INT_PRE.getValue() + key, 6, interval); return json; } @MethodRemark(value = "remark=???,method=ajax,md5Mobile=???,userId=ID,type=,signdata=?") @RequestMapping(value = JsonPathConstants.J_FIND_PWD_SEND_CODE_PATH, method = RequestMethod.GET) public @ResponseBody OperationJsonObject sendFindPwdMobileCode(@PathVariable String md5Mobile, @PathVariable String type, @PathVariable String userId, @PathVariable String signdata, HttpServletRequest request) { if (StringUtils.isBlank(userId)) { // userId? OperationJsonObject json = new OperationJsonObject(); json.setHasError(true); json.setMessage("??"); return json; } userId = DESUtil.DESDecode(userId, EncodeKeyConstants.USER_ENCODE_KEY); if (!StringUtils.isNumeric(userId)) { // userId? OperationJsonObject json = new OperationJsonObject(); json.setHasError(true); json.setMessage("??"); return json; } UserDO user = userManager.findUserByUserId(Long.valueOf(userId)); if (user == null) { OperationJsonObject json = new OperationJsonObject(); json.setHasError(true); json.setMessage("??"); return json; } if (md5Mobile.equals(MD5Util.getMD5Format(user.getMobile()))) { return this.sendMobileValidCode(request, user.getMobile(), signdata, type, null, md5Mobile, null); } else { OperationJsonObject json = new OperationJsonObject(); json.setHasError(true); json.setMessage("???????"); return json; } } private OperationJsonObject sendMobileValidCode(HttpServletRequest request, String mobile, String signdata, String type, String orderparam, String md5Mobile, String encodeMobile) { OperationJsonObject json = new OperationJsonObject(); if (RegexUtil.isMobile(mobile)) { json.setHasError(true); json.setMessage("?????,????."); // ? String mobilevalidcodepwd = (String) mursiFreeMarkerViewResolver.getAttributesMap() .get("mobilevalidcodepwd"); String clientsigndata = null; if (null != md5Mobile) { clientsigndata = MD5Util.getMD5Format(type + md5Mobile + mobilevalidcodepwd); } else if (null != encodeMobile) { clientsigndata = MD5Util.getMD5Format(type + encodeMobile + mobilevalidcodepwd); } else { clientsigndata = MD5Util.getMD5Format(type + mobile + mobilevalidcodepwd); } if (StringUtils.isBlank(signdata) || !signdata.equals(clientsigndata)) { return json; } String key = mobile; // ?: ???,??id?id if (StringUtils.isNotBlank(orderparam) && "RES_CODE_MOBILE".equals(type)) { key += orderparam; } SMSResultDTO smsresultdto = null; // ??code String code = codeCacheManager.getCode(VerifyTypeEnum.valueOf(type), VCodeCachePrefixEnum.CODE_PRE.getValue() + key); // ???:false? true? // boolean isvalidcode = codeCacheManager.isIsvalidcode(); boolean isvalidcode = true; // ?? if (StringUtils.isBlank(code)) { // ?? code = codeCacheManager.setCode(VerifyTypeEnum.valueOf(type), VCodeCachePrefixEnum.CODE_PRE.getValue() + key, 6, orderValidLimitCacheManager.getOrderValidCodeTimeout() * 1000); // ???? if (isvalidcode) { smsresultdto = userVerifyManager.sendMobileVerifyCode(mobile, VerifyTypeEnum.valueOf(type), code); } else { smsresultdto = new SMSResultDTO(); smsresultdto.setResult(ResultEnum.SUCCESS); smsresultdto.setMessage("???"); } // ?? returnjson(json, smsresultdto); // ???? codeCacheManager.setCode(VerifyTypeEnum.valueOf(type), VCodeCachePrefixEnum.INT_PRE.getValue() + key, 6, interval); } else { // ???code String intervalCode = codeCacheManager.getCode(VerifyTypeEnum.valueOf(type), VCodeCachePrefixEnum.INT_PRE.getValue() + key); // ???? if (StringUtils.isBlank(intervalCode)) { // ????? if (isvalidcode) { smsresultdto = userVerifyManager.sendMobileVerifyCode(mobile, VerifyTypeEnum.valueOf(type), code); } else { smsresultdto = new SMSResultDTO(); smsresultdto.setResult(ResultEnum.SUCCESS); smsresultdto.setMessage("???"); } // ?? returnjson(json, smsresultdto); // ???? codeCacheManager.setCode(VerifyTypeEnum.valueOf(type), VCodeCachePrefixEnum.INT_PRE.getValue() + key, 6, interval); } else { // ???? ???? ?????? json.setHasError(true); json.setMessage("?????,????."); } } } else { json.setHasError(true); json.setMessage("??."); } return json; } private OperationJsonObject returnjson(OperationJsonObject json, SMSResultDTO smsresultdto) { if (smsresultdto != null) { if (ResultEnum.SUCCESS.equals(smsresultdto.getResult())) { json.setHasError(false); json.setMessage("???."); } else if (ResultEnum.OUTLIMIT.equals(smsresultdto.getResult())) { json.setHasError(true); json.setMessage( "??????"); } } return json; } }