Unique random marker generation in rake

I am new to grails and in my web application I want to create a random token with a length of 15 characters along with the username. And tokens must be unique.

All characters from az and 0-9 can be used, but do not have special characters. I tried to create an arbitrary token using

def generator = {String alphabet, int n -> new Random().with { (1..n).collec alphabet[ nextInt( alphabet.length() ) ] }.join() }} generator( (('A'..'Z')+('0'..'9')).join(), 9 ) 

but how can I add the username infront to the token like "JayKay586464ASDHH445"

+7
random grails grails-plugin
source share
2 answers

How about this? The username contains the original username without the token, the token is the 15 character token, and uTok is the username with the token

 def generator = { String alphabet, int n -> new Random().with { (1..n).collect { alphabet[ nextInt( alphabet.length() ) ] }.join() } } def token = generator( (('A'..'Z')+('0'..'9')).join(), 15 ) def username = "JayKay" def uTok = "${username}${token}" println "==>${uTok}<==" 
+5
source share

String confirmCode= UUID.randomUUID().toString() use this code generator, then use "+" to concatenate the string

+8
source share

All Articles