Найти

Вы здесь:

GitHub проекта: https://github.com/cherepakhin/springboot-thymeleaf-security-demo
Демонстрация SpringBoot WEB приложения с авторизацией и аутентификаци Spring Security.
Использованы следующие технологии:

  • Spring Boot
  • Spring MVC
  • Thymeleaf
  • Spring Security
  • Spring Data JPA
  • MySQL

Запуск

Java 11

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
./mvnw spring-boot:run

Открыть в браузере: http://localhost:8080/
Вход в программу по email

  • User: Адрес электронной почты защищен от спам-ботов. Для просмотра адреса в браузере должен быть включен Javascript. Password: admin
  • User: Адрес электронной почты защищен от спам-ботов. Для просмотра адреса в браузере должен быть включен Javascript. Password: user

Открыть домашнюю страницу пользователя: http://127.0.0.1:8080/home
Она будет разная для разных пользователей.

Screens

__Not__ logged: ![doc/not_logged.png](doc/not_logged.png)

Interface for Admin

View for admin: ![home_admin](doc/home_admin.png)

  • Added menu "To Admin"
  • you see message __"You will see this only if you are ADMIN"__): ![doc/home_admin.png](doc/home_admin.png)

Interface for User

Result to admin page for user: For __user__ login: ![doc/home_user.png](doc/home_user.png) Not access to __Admin page__ for user: ![doc/from_user_to_admin_page.png](doc/from_user_to_admin_page.png)) [userhome.html](src/main/resources/templates/userhome.html) (line 20): ````html

You will see this only if you are ADMIN

Admin Home

```` __WebSecurityConfig.java defines the rules for accessing pages.__

Mobile screens

Alt text Alt text Alt text

Link action form to java code

Form (html):

link: th:object="${content}" th:action="@{/new_message}" method="post"

New Message

<div class="container" style="padding-left: 0px; margin-left: 0px;">
    <form class="form-horizontal" th:object="${content}" th:action="@{/new_message}"
          name="new_form"
          id="edit_form"
          method="post" role="form"> // <--- POST!!! /new_message
        <div class="form-group" style="margin-left: 0px;">
            <label for="message_content">Сообщение (max 100 символов):</label>
            <input class="form-control" id="content" maxlength="100" type="text" th:name="content"/>
        </div>
        <button type="submit" class="btn btn-primary" value="Save">Сохранить</button>
        <a class="btn btn-default" th:href="@{/home}">Отмена</a>
    </form>
</div>

Handler in Java code (@PostMapping("/new_message")):

  • Method POST: @PostMapping(value = "/new_message")
  • Value: th:object="${content}"

Обработка в Java-коде контроллера:

@Controller
public class HomeController {
    @PostMapping(value = "/new_message")
    public String newMessage(@RequestParam(defaultValue = "----", value = "content") String newContent) {
        ...
    }
}

Покрытие тестами (test coverage)

./mvnw clean test
./mvnw jacoco:report

Open target/site/jacoco/index.html](target/site/jacoco/index.html

Links

Разное

Подключение к MySQL из shell:

mysql -u vasi -h v -p

database: thymeleaf_security_demo

TODO

  • tests
  • paging messages