And the ministry of communications development in the name of muhammad al khorezmi tashkent university of information technologies


Download 175.21 Kb.
bet8/8
Sana09.04.2023
Hajmi175.21 Kb.
#1343718
1   2   3   4   5   6   7   8
Bog'liq
Mustaqil Ish-1

Customized Messages
Passay library enables us to customize messages returned by validation rules. Firstly, we should define the messages and assign them to error codes.
We can put them into a simple file. Let's see how easy it is:

TOO_LONG=Password must not have more characters than %2$s.
TOO_SHORT=Password must not contain less characters than %2$s.

Once we have messages, we have to load that file. Finally, we can pass it into PasswordValidator object.


Here is a sample code:

URL resource = this.getClass().getClassLoader().getResource("messages.properties");
Properties props = new Properties();
props.load(new FileInputStream(resource.getPath()));


MessageResolver resolver = new PropertiesMessageResolver(props);

As we can see, we've loaded the message.properties file and passed it into Properties object. Then, we can use the Properties object to create PropertiesMessageResolver.


Let's take a look at the example how to use the message resolver:

PasswordValidator validator = new PasswordValidator(
resolver,
new LengthRule(8, 16),
new WhitespaceRule()
);


RuleResult tooShort = validator.validate(new PasswordData("XXXX"));
RuleResult tooLong = validator.validate(new PasswordData("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"));


Assert.assertEquals(
"Password must not contain less characters than 16.",
validator.getMessages(tooShort).get(0));
Assert.assertEquals(
"Password must not have more characters than 16.",
validator.getMessages(tooLong).get(0));

The example clearly shows that we can translate all error codes with the validator equipped with a message resolver.




Conclusion
In this tutorial, we've learned how to use Passay library. We have analyzed several examples of how the library can be easily used for password validation. Provided rules cover most of the common ways of assuring that a password is safe.
But we should remember that Passay library itself doesn't make our password secure. Firstly, we should learn what are general rules and then use the library to implement them.
All examples, as always, can be found over on GitHub.


Used publications and web-sites



  1. https://www.baeldung.com/

  2. https://www.tutorialspoint.com/

  3. https://www.coderlessons.com/

  4. https://www.dzone.com/

  5. https://www.dev.to/

Download 175.21 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling