Added auth system, file upload system, file upload tests
This commit is contained in:
1 parent
2ba79f8058
commit
cb9790fdd6
13 files changed
+332
-6
No files matched your search
@@ -1,4 +1,16 @@
|
||||
package site.easystartup.easystartupcore.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"site.easystartup.easystartupcore"})
|
||||
public class AppConfig {
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package site.easystartup.easystartupcore.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class MvcConfig implements WebMvcConfigurer {
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/login").setViewName("login");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package site.easystartup.easystartupcore.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
private final DataSource dataSource;
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.authorizeRequests()
|
||||
.antMatchers("/", "/home", "/registration").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.formLogin()
|
||||
.loginPage("/login")
|
||||
.permitAll()
|
||||
.and()
|
||||
.logout()
|
||||
.permitAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.jdbcAuthentication()
|
||||
.dataSource(dataSource)
|
||||
.passwordEncoder(passwordEncoder)
|
||||
.usersByUsernameQuery("SELECT `username`, `password`, `active` FROM usr WHERE `username` = ?;")
|
||||
.authoritiesByUsernameQuery("SELECT u.username, ur.roles FROM usr u INNER JOIN user_role ur ON u.id = ur.user_id WHERE username = ?;");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package site.easystartup.easystartupcore.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import site.easystartup.easystartupcore.domain.Role;
|
||||
import site.easystartup.easystartupcore.domain.User;
|
||||
import site.easystartup.easystartupcore.repos.UserRepo;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@Controller
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class RegistrationController {
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
private final UserRepo userRepo;
|
||||
|
||||
@GetMapping("/registration")
|
||||
public String registration() {
|
||||
return "registration";
|
||||
}
|
||||
|
||||
@PostMapping("/registration")
|
||||
public String addUser(User user, Model model) {
|
||||
User userFromDb = userRepo.findByUsername(user.getUsername());
|
||||
|
||||
if(userFromDb != null) {
|
||||
model.addAttribute("message", "Пользователь уже существует!");
|
||||
return "registration";
|
||||
}
|
||||
|
||||
user.setActive(true);
|
||||
user.setRoles(Collections.singleton(Role.USER));
|
||||
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
||||
|
||||
userRepo.save(user);
|
||||
|
||||
return "redirect:/login";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package site.easystartup.easystartupcore.domain;
|
||||
|
||||
public enum Role {
|
||||
USER, PREMIUM, MODER, ADMIN;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package site.easystartup.easystartupcore.domain;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Set;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = "usr")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
private String username;
|
||||
private String password;
|
||||
private boolean active;
|
||||
|
||||
@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
|
||||
@CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"))
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Set<Role> roles;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package site.easystartup.easystartupcore.repos;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import site.easystartup.easystartupcore.domain.User;
|
||||
|
||||
public interface UserRepo extends JpaRepository<User, Long> {
|
||||
User findByUsername(String username);
|
||||
}
|
||||
@@ -1,14 +1,11 @@
|
||||
server.port=80
|
||||
|
||||
spring.main.banner-mode=off
|
||||
logging.level.org.springframework=ERROR
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=none
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
|
||||
spring.sql.init.mode=always
|
||||
|
||||
spring.sql.init.platform=postgres
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=root
|
||||
|
||||
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
@@ -0,0 +1,65 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
|
||||
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Вход</title>
|
||||
|
||||
<style>
|
||||
div.error {
|
||||
color: #ff5967;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
div.logout {
|
||||
color: #2c84ec;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
|
||||
</head>
|
||||
|
||||
<body class="text-center">
|
||||
|
||||
<main>
|
||||
|
||||
<form th:action="@{/login}" method="post">
|
||||
<h1>Пожалуйста, войдите</h1>
|
||||
|
||||
<div>
|
||||
<input type="text" class="form-control" minlength="3" size="25" id="username" name="username" required placeholder="UserName">
|
||||
<label for="username">Логин</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="password" name="password" minlength="3" id="password" required placeholder="Password123">
|
||||
<label for="password">Пароль</label>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label>
|
||||
<input type="checkbox" value="remember-me"> Запомнить меня
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<button type="submit">Войти</button>
|
||||
|
||||
<div th:if="${param.error}" class="error">
|
||||
Неправильный логин или пароль.
|
||||
</div>
|
||||
<div th:if="${param.logout}" class="logout">
|
||||
Вы вышли из аккаунта.
|
||||
</div>
|
||||
|
||||
<p>© 2022</p>
|
||||
</form>
|
||||
<a href="registration">Зарегистрироваться</a>
|
||||
</main>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,42 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
|
||||
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
|
||||
<head>
|
||||
<title>Регистрация</title>
|
||||
</head>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
|
||||
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
|
||||
</head>
|
||||
<body class="text-center">
|
||||
|
||||
<main class="form-signin w-100 m-auto">
|
||||
|
||||
<form th:action="@{/registration}" method="post">
|
||||
|
||||
<h1>Пожалуйста, зарегистрируйтесь</h1>
|
||||
|
||||
<div>
|
||||
<input minlength="3" size="25" name="username" id="username" required placeholder="UserName">
|
||||
<label for="username">Логин</label>
|
||||
</div>
|
||||
<div>
|
||||
<input type="password" name="password" minlength="3" id="password" required placeholder="Password123">
|
||||
<label for="password">Пароль</label>
|
||||
</div>
|
||||
|
||||
<button type="submit">Зарегистрироваться</button>
|
||||
<div>
|
||||
<p th:text="${message}"></p>
|
||||
</div>
|
||||
<p>© 2022</p>
|
||||
</form>
|
||||
</main>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user