在HTML表单内发送API密钥

时间:2019-06-13 14:39:04

标签: java api verification api-key html-form-post

因此,我有一个用于Web服务的API密钥。每次用户注册时,我都会给他发送电子邮件以验证他的帐户。看起来像这样:

public class EmailSender {

private static final String username = "somesecretemail@gmail.com";
private static final String password = "uthoughiwillshowyoumypassword?";

public static void sendVerificationCode(String receiverusername, String receiveremail, String code) throws Exception {
    Properties prop = new Properties();
    prop.put("mail.smtp.host", "smtp.gmail.com");
    prop.put("mail.smtp.port", "587");
    prop.put("mail.smtp.auth", "true");
    prop.put("mail.smtp.starttls.enable", "true"); //TLS

    Session session = Session.getInstance(prop,
            new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    MimeMessage message = new MimeMessage(session);
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveremail));
    message.setSubject("Verification Code");

    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText("<form action=\"https://pathtomywebserviceurl.com/verify/"+code+"\">\n" +
    "<input type=\"submit\" value=\"Verify\" />\n" +
     "</form>", "UTF-8", "html");

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    message.setContent(multipart);

    Transport.send(message);
}

当他单击按钮时,用户实际上以其令牌作为参数向我的Web服务发出请求。但是现在我也希望我的API密钥发送为例如。一个http标头,以便我可以从用户验证请求中提取API密钥,并检查API密钥是否等于实际的API密钥。现在,我只发送代码,而不发送API密钥。

1 个答案:

答案 0 :(得分:1)

使用隐藏的表单元素:

<input type="hidden" id="myApiKey" name="myApiKey" value="myApiKeyValue">