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);
|
||||
}
|
||||
Reference in new issue
Block a user