@GET @Produces(MediaType.APPLICATION_JSON) @Path("resetpassword/webpage") public Response getPasswordResetWebPage(@QueryParam("mail") String mail) throws MessagingException, IOException { if ((mail == null || mail.trim().equals(""))) { return Response.ok(AuthenticationConstants.EMAILPROVIDEDISNOTCORRECT).build(); } final EntityManager em = emf.createEntityManager(); Query queryE = em.createNamedQuery("User.findByEmail"); queryE.setParameter("email", mail); List<User> userList = queryE.getResultList(); if (userList.isEmpty() || userList.size() > 1) { return Response.ok(AuthenticationConstants.EMAILPROVIDEDISNOTCORRECT).build(); } ServletContext sc = servletConfig.getServletContext(); Properties tzMediaProperties = PropertiesUtils.getProperties(sc); String subject = tzMediaProperties.getProperty("password.retrival.mail.subject"); String tail = tzMediaProperties.getProperty("password.retrival.mail.tail"); final User userEntity = userList.get(0); String tokeyGenerated = TokenGenerator.nextToken(); // Put the stake that holds the user password reset. TokenHolder.userPasswordTokenMap.put(userEntity.getUserId(), tokeyGenerated); String body = "<p>亲爱的" + userEntity.getEmail() + ",</p><p>" + "重新设置嘟嘟囔囔密码请点击下面的链接:</p>" // 链接 + "<p><a href=" + uriInfo.getBaseUri().toString().replace("resources", "#") + "resetuserpassword>" + uriInfo.getBaseUri().toString().replace("resources", "#") + "resetuserpassword</a>" + "</p>" // mail last text + tail; SendCloudMail.send(mail, subject, body); // TZMediaMail.send(mail, subject, body, null, sc); return Response.ok(AuthenticationConstants.EMAILSUCCESSFULLYSEND).build(); }
@GET @Path("registration/mail") @Produces(MediaType.APPLICATION_JSON) public Response sendVerifyMessageToEmail(@QueryParam("mail") String mail) throws MessagingException, IOException { ServletContext sc = servletConfig.getServletContext(); Properties tzMediaProperties = PropertiesUtils.getProperties(sc); String verificationCode = String.valueOf(VerificationCodeGenerator.randInt(100000, 999999)); String subject = tzMediaProperties.getProperty("validation.with.mail.subject"); String tail = tzMediaProperties.getProperty("password.retrival.mail.tail"); if (mail != null) { final EntityManager em = emf.createEntityManager(); Query queryE = em.createNamedQuery("User.findByEmail"); queryE.setParameter("email", mail); List<User> userList = queryE.getResultList(); // User already registered if (userList.size() > 0) { return Response.ok( new PhoneVerifyResponse( AuthenticationConstants.EMAILPROVIDEDISNOTCORRECT, mail, null)) .build(); } String body = "<p>亲爱的 " + mail + ",</p>" + "<p>您的嘟嘟囔囔邮箱注册验证码是:" + verificationCode + "</p>" + tail; // TZMediaMail.send(mail, subject, body, null, sc); SendCloudMail.send(mail, subject, body); TokenHolder.verificationCodeMap.put(mail, verificationCode); return Response.ok( new PhoneVerifyResponse( AuthenticationConstants.EMAILSUCCESSFULLYSEND, null, verificationCode)) .build(); } else { return Response.ok( new PhoneVerifyResponse( AuthenticationConstants.EMAILPROVIDEDISNOTCORRECT, mail, null)) .build(); } }
/** * The response entity from message service is * {"statusCode":"000000","templateSMS":{"dateCreated":"20140827105250", * "smsMessageSid":"20140827105250065847"}} * * @param phoneNumber * @return * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException */ @GET @Path("registration/sms") @Produces(MediaType.APPLICATION_JSON) public Response sendVerifyMessageToMobile(@QueryParam("phoneNumber") String phoneNumber) throws NoSuchAlgorithmException, UnsupportedEncodingException { if (phoneNumber == null) { return Response.ok( new PhoneVerifyResponse( AuthenticationConstants.MESSAGESENDFAILURE, "Parameter phoneNumber provided is null", null)) .build(); } Properties tzProperties = PropertiesUtils.getProperties(servletConfig.getServletContext()); SslConfigurator sslConfig = SslConfigurator.newInstance() .trustStoreFile(tzProperties.getProperty("ssl.trust.store.file")) .trustStorePassword(tzProperties.getProperty("ssl.trust.store.pass")) .trustStoreType("JKS") .trustManagerFactoryAlgorithm("PKIX") .keyStoreFile(tzProperties.getProperty("ssl.key.store.file")) .keyPassword(tzProperties.getProperty("ssl.key.store.pass")) .keyStoreType("JKS") .keyManagerFactoryAlgorithm("SunX509") .keyStoreProvider("SUN") .securityProtocol("SSL"); SSLContext sslContext = sslConfig.createSSLContext(); Client client = ClientBuilder.newBuilder().sslContext(sslContext).build(); WebTarget target = client.target(tzProperties.getProperty("mobile.verify.service")); String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); String accountSid = tzProperties.getProperty("mobile.account.sid"); String encodeString = accountSid + tzProperties.getProperty("mobile.auth.token") + timeStamp; String authenticationString = accountSid + ":" + timeStamp; String sig = new EncryptUtil().md5Digest(encodeString); JsonObjectBuilder messageBuilder = Json.createObjectBuilder(); JsonArrayBuilder datasBuilder = Json.createArrayBuilder(); String verificationCode = String.valueOf(VerificationCodeGenerator.randInt(100000, 999999)); datasBuilder.add(verificationCode).add(tzProperties.getProperty("mobile.code.active.time")); messageBuilder .add("to", phoneNumber) .add("appId", tzProperties.getProperty("mobile.appid")) .add("templateId", tzProperties.getProperty("mobile.templateid")) .add("datas", datasBuilder); Response providerResponse = target .queryParam("sig", sig) .request(MediaType.APPLICATION_JSON) .header(HttpHeaders.AUTHORIZATION, Base64.encodeAsString(authenticationString)) .post(Entity.entity(messageBuilder.build(), MediaType.APPLICATION_JSON)); JsonObject jsonObject = providerResponse.readEntity(JsonObject.class); if (jsonObject.getString("statusCode").equals("000000")) { TokenHolder.verificationCodeMap.put(phoneNumber, verificationCode); return Response.ok( new PhoneVerifyResponse( AuthenticationConstants.MESSAGESENDSUCCESS, jsonObject.toString(), verificationCode)) .build(); } else { return Response.ok( new PhoneVerifyResponse( AuthenticationConstants.MESSAGESENDFAILURE, jsonObject.toString(), null)) .build(); } }