- Добавил возможность назначения ролей пользователям.
- Добавил контроллер для управления пользователями и запрет доступа к нему пользователям без роли ADMIN. https://www.youtube.com/watch?v=6dteOGWy4uk&list=PLU2ftbIeotGpAYRP9Iv2KLIwK36-o_qYk&index=9 Использовал аннотацию @EnableMethodSecurity вместо deprecated @EnableGlobalMethodSecurity
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
package net.sytes.kashey.sweater.config;
|
||||
|
||||
import net.sytes.kashey.sweater.service.CustomUserDetailsService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
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.configurers.LogoutConfigurer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@EnableMethodSecurity
|
||||
public class WebSecurityConfig {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package net.sytes.kashey.sweater.controller;
|
||||
|
||||
import net.sytes.kashey.sweater.domain.Role;
|
||||
import net.sytes.kashey.sweater.domain.User;
|
||||
import net.sytes.kashey.sweater.repository.UserRepository;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/users")
|
||||
@PreAuthorize("hasAuthority('ADMIN')")
|
||||
public class UserController {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public UserController(UserRepository userRepository) {
|
||||
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String getAllUsers(Model model) {
|
||||
|
||||
model.addAttribute("users", userRepository.findAll());
|
||||
return "userList";
|
||||
}
|
||||
|
||||
@GetMapping("/{user}")
|
||||
public String editUser(@PathVariable User user, Model model) {
|
||||
|
||||
model.addAttribute("user", user);
|
||||
model.addAttribute("roles", Role.values());
|
||||
return "userEdit";
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public String updateUser(@RequestParam String username,
|
||||
@RequestParam Map<String, String> form,
|
||||
@RequestParam("userId") User user) {
|
||||
|
||||
user.setUsername(username);
|
||||
user.getRoles().clear();
|
||||
Set<String> roles = Arrays.stream(Role.values()).map(Enum::name).collect(Collectors.toSet());
|
||||
for (String role : form.keySet()) {
|
||||
if (roles.contains(role)) {
|
||||
user.getRoles().add(Role.valueOf(role));
|
||||
}
|
||||
}
|
||||
userRepository.save(user);
|
||||
return "redirect:/users";
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
<#import "parts/common.ftl" as com>
|
||||
<@com.page>
|
||||
<div>Hello there</div>
|
||||
<a href="/main">Main page</a>
|
||||
<div>Hello there</div>
|
||||
<a href="/main">Main page</a>
|
||||
<br>
|
||||
<a href="/users">Users page</a>
|
||||
</@com.page>
|
||||
@@ -3,6 +3,7 @@
|
||||
<@com.page>
|
||||
<div>
|
||||
<@l.logout />
|
||||
<span> <a href="/users">Users page</a> </span>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
@@ -33,4 +34,5 @@
|
||||
<#else>
|
||||
No messages found
|
||||
</#list>
|
||||
<br>
|
||||
</@com.page>
|
||||
14
src/main/resources/templates/userEdit.ftl
Normal file
14
src/main/resources/templates/userEdit.ftl
Normal file
@@ -0,0 +1,14 @@
|
||||
<#import "parts/common.ftl" as com>
|
||||
<@com.page>
|
||||
<form action="/users" method="post">
|
||||
<input type="text" name="username" value="${user.username}">
|
||||
<#list roles as role>
|
||||
<div>
|
||||
<label><input type="checkbox" name="${role}" ${user.roles?seq_contains(role)?string("checked", "")}>${role}</label>
|
||||
</div>
|
||||
</#list>
|
||||
<input type="hidden" value="${user.id}" name="userId">
|
||||
<input type="hidden" value="${_csrf.token}" name="_csrf">
|
||||
<button type="submit">Save</button>
|
||||
</form>
|
||||
</@com.page>
|
||||
23
src/main/resources/templates/userList.ftl
Normal file
23
src/main/resources/templates/userList.ftl
Normal file
@@ -0,0 +1,23 @@
|
||||
<#import "parts/common.ftl" as com>
|
||||
<@com.page>
|
||||
<div>Users</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Role</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<#list users as user>
|
||||
<tr>
|
||||
<td>${user.username}</td>
|
||||
<td><#list user.roles as role>${role}<#sep>, </#list></td>
|
||||
<td><a href="/users/${user.id}">edit</a></td>
|
||||
</tr>
|
||||
</#list>
|
||||
</tbody>
|
||||
</table>
|
||||
<a href="/main">Main page</a>
|
||||
</@com.page>
|
||||
Reference in New Issue
Block a user