开发者

Can anyone guide on generating UUID and then sending confirm link in Spring MVC

开发者 https://www.devze.com 2023-02-16 07:01 出处:网络
I have function in controller like 开发者_如何学PythonSession session = sessionFactory.getCurrentSession();

I have function in controller like

开发者_如何学PythonSession session = sessionFactory.getCurrentSession();

      session.save(person);

Now i want to generate UUID and send the link in email

http://www.abc.com/confirm?uusi=1234

Can anyone give me some code of how actually generating UUID and what to do fater click

If anyone can give me some online link showing this example. This is the common one should on internet


Knowing this is an old thread, it's still being returned relevant by google. There are 3 UUID generators in Spring itself - all three sharing one interface: org.springframework.util.IdGenerator (introduced in Spring 4.0). Implementations are:

  • SimpleIdGenerator since 4.1.5
  • JdkIdGenerator since 4.1.5
  • AlternativeJdkIdGenerator since 4.0


You can generate UUID and for maximum uniqueness add current time milliseconds. Then, hash it with any algorithm.

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class UUIDTest {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        long currentTimeMillis = System.currentTimeMillis();
        UUID randomUUID = UUID.randomUUID();

        String uuid = randomUUID.toString() + "-" + currentTimeMillis ;
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(uuid.getBytes());
        byte[] mb = md.digest();
        String out = "";

        for (int i = 0; i < mb.length; i++) {
            byte temp = mb[i];
            String s = Integer.toHexString(new Byte(temp));
            while (s.length() < 2) {
                s = "0" + s;
            }
            s = s.substring(s.length() - 2);
            out += s;
        }

        System.out.println(out);
    }

}


java.util.UUID.randomUUID()

then just send that in an email. - or did you need something more complex?

0

精彩评论

暂无评论...
验证码 换一张
取 消