samedi 15 juin 2019

Spring: password encryption

here's my view:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Регистрация</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300|Oswald|Roboto|Roboto+Condensed" rel="stylesheet">
    <link href="../static/css/form.css" rel="stylesheet" th:href="@{/css/form.css}">
</head>
<body>
<div id="wrapper">
    <div id="register_main_wrapper">
        <form action="#" th:action="@{/register}" th:object="${user}" method="post">
            <div id="register_form_wrapper">
                <div id="register_text_wrapper">
                    <h1 id="register_text">Создайте учётную запись</h1>
                </div>
                <div id="email_desc_reg_wrapper" class="desc_text">
                    <a>ЭЛЕКТРОННАЯ ПОЧТА</a>
                </div>
                <div id="email_input_reg_wrapper">
                    <input th:field="*{userEmail}"  type="email" size="35" class="input_fields">
                </div>
                <div id="nickname_desc_reg_wrapper" class="desc_text">
                    <a>ИМЯ ПОЛЬЗОВАТЕЛЯ</a>
                </div>
                <div id="nickname_input_reg_wrapper">
                    <input th:field="*{userNick}" type="text" size="35" class="input_fields">
                </div>
                <div id="password_desc_reg_wrapper" class="desc_text">
                    <a>ПАРОЛЬ</a>
                </div>
                <div id="password_input_reg_wrapper">
                    <input th:field="*{userPass}" type="password" size="35" class="input_fields">
                </div>
                <div id="register_button_wrapper">
                    <button class="form_btn" id="register_button" type="submit" value="Submit">Регистрация</button>
                </div>
                <div id="login_switch_wrapper">
                    <a href="login" id="login_switch">Вход</a>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
</html>

when i press submit, all attributes(pass,nick,email) goes to my controller, there is a part of it

@RequestMapping(value="/register", method=RequestMethod.GET)
public ModelAndView displayRegistration(ModelAndView modelAndView, User user)
{
    modelAndView.addObject("user", user);
    modelAndView.setViewName("register");
    return modelAndView;
}
@RequestMapping(value="/register", method=RequestMethod.POST)
    public ModelAndView registerUser(ModelAndView modelAndView, User user)
    {

        User existingUser = userRepository.findByUserEmailIgnoreCase(user.getUserEmail());
        if(existingUser != null)
        {
            modelAndView.addObject("message","This email already exists!");
            modelAndView.setViewName("error");
        }
        else
        {
            userRepository.save(user);

i need to add password encryption to my app, so, as far as i know, i need to extract the password attribute from the model object, encrypt it and put it back. how can i extract attributes and return them to the model object?




Aucun commentaire:

Enregistrer un commentaire