简体   繁体   English

JavaMail - 用于用户注册的电子邮件身份验证

[英]JavaMail - Email Authentication for user registration

I'm working on a spring based web app and when a user enters his email and password for a registration i want to a validate them and send him a email .I have used java mail api and set up gmail smtp correctly. 我正在开发一个基于Spring的网络应用程序,当用户输入他的电子邮件和密码进行注册时, 我想验证它们并向他发送电子邮件 。我使用了java mail api并正确设置了gmail smtp

Here is my register controller class. 这是我的注册控制器类。

    @RequestMapping(value = "/globalshop/register", method = RequestMethod.GET)
    public final ModelAndView registerModel(
            HttpServletRequest request, HttpServletResponse response)
    {
        final String user_email = request.getParameter("user_email");
        final String user_password = request.getParameter("user_password"); 

        Properties email_properties = new Properties();

        email_properties.put("mail.smtp.auth", "true");
        email_properties.put("mail.smtp.starttls.enable", "true");
        email_properties.put("mail.smtp.host", "smtp.gmail.com");       
        email_properties.put("mail.smtp.port", "587");

        final String siteEmail = "**************@gmail.com";
        final String siteEmailPassword = "*************";

        Session session= Session.getInstance(email_properties, new Authenticator() {
            protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                return new javax.mail.PasswordAuthentication(siteEmail, siteEmailPassword);

            }
        });


        try {
            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress(siteEmail));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(user_email));
            message.setSubject("Testing Subject");
            message.setText("Dear User,"+ "\n\n You have been successfully registerd with the gift corner!");

            Transport.send(message);
            System.out.println("Done");

        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        registerSucessView = new ModelAndView("test_register_succes");      
        registerSucessView.addObject("welcome", "welcome user");


        return registerSucessView;
    }

}

user_email and user_password are the incoming request parameters. user_emailuser_password是传入的请求参数。 how can I validate them ? 我该如何验证它们 If the validation is successful user_email can be set to message.setRecipients(...) 如果验证成功,则user_email可以设置为message.setRecipients(...)

Spring does provide a mechanism to validate your models, as shown here . 春天确实提供了一种机制来验证你的模型,如图所示这里 Personally I would recommend that you follow those guidelines so that your application makes full use of the features offered by Spring. 我个人建议您遵循这些准则,以便您的应用程序充分利用Spring提供的功能。

That being said, the most basic forms of validation for user names is to make sure that the user name is not already in your database and in some cases, certain special characters might not be allowed. 话虽这么说,用户名的最基本验证形式是确保用户名不在您的数据库中,在某些情况下,可能不允许某些特殊字符。

For passwords, on the other hand, there are usually some length restrictions, such as at least 5 characters. 另一方面,对于密码,通常存在一些长度限制,例如至少5个字符。 In certain situations, you can also enforce the use of letters of different casing, numbers and other special symbols. 在某些情况下,您还可以强制使用不同套管,数字和其他特殊符号的字母。 In these cases, regular expressions can be helpful. 在这些情况下,正则表达式可能会有所帮助。 ( Note: For this case, it is not recommended to build a single regex that will attempt to match everything). 注意:对于这种情况,不建议构建一个试图匹配所有内容的单一正则表达式)。

On a completely different note, do not forget to store your passwords as hash values, and then, when the user logs on, you compare the value of the hash of the password which the user has entered with the hash that you have already in your DB. 在一个完全不同的说明中,不要忘记将密码存储为哈希值,然后,当用户登录时,您将用户输入的密码的哈希值与您已经输入的哈希值进行比较。 D B。

Knowing the user's email address doesn't help you if you don't know what server the user is using. 如果您不知道用户使用的是哪台服务器,则了解用户的电子邮件地址对您没有帮助。 There are ways to figure that out in many cases, but you may not have access to that server even if you could figure it out. 在许多情况下,有很多方法可以解决这个问题,但即使你能弄明白,也可能无法访问该服务器。 The only reason to ask for the password is because you want to log in to the user's email server as if you were that user. 要求输入密码的唯一原因是因为您想要登录用户的电子邮件服务器,就像您是该用户一样。 Hope the users trust you! 希望用户信任你!

If you really just want to know that the user's email address is valid so that you can send email to that user, the only real way to know is to send a message to the address and ask the user to do something (eg, visit a URL on your server) if the message is received. 如果您真的只是想知道用户的电子邮件地址是有效的,以便您可以向该用户发送电子邮件,唯一真正的方法是向该地址发送消息并要求用户做某事(例如,访问如果收到消息,则说明服务器上的URL。

You'll find additional information in the JavaMail FAQ . 您可以在JavaMail FAQ中找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM