Spring?Security如何為用戶(hù)示例添加角色詳解
前言
在這個(gè) Spring Security 教程中,我很樂(lè)意與您分享如何通過(guò)在 Java Web 應(yīng)用程序中為用戶(hù)添加角色來(lái)實(shí)現(xiàn)授權(quán)——從數(shù)據(jù)庫(kù)設(shè)計(jì)到實(shí)體類(lèi);從單元測(cè)試到在用戶(hù)注冊(cè)中添加默認(rèn)角色;以 Web 形式更新用戶(hù)的角色。
技術(shù):Spring Web MVC、Spring Data JPA、Hibernate 框架、Spring Security、Spring Boot Test、JUnit 5、AssertJ、Thymeleaf 和 MySQL 數(shù)據(jù)庫(kù)。
基本上,我們需要在數(shù)據(jù)庫(kù)中有 3 個(gè)表,如下所示:
一個(gè)用戶(hù)可以有一個(gè)或多個(gè)角色,一個(gè)角色可以分配給一個(gè)或多個(gè)用戶(hù),因此用戶(hù)和角色表之間的實(shí)體關(guān)系是多對(duì)多的。users_roles是實(shí)現(xiàn)這種關(guān)系的中間表。
1. 用戶(hù)和角色實(shí)體類(lèi)和存儲(chǔ)庫(kù)的代碼
將User 實(shí)體類(lèi)編碼如下:
package net.codejava; import java.util.HashSet; import java.util.Set; import javax.persistence.*; @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, unique = true, length = 45) private String email; @Column(nullable = false, length = 64) private String password; @Column(name = "first_name", nullable = false, length = 20) private String firstName; @Column(name = "last_name", nullable = false, length = 20) private String lastName; @ManyToMany(fetch = FetchType.EAGER) @JoinTable( name = "users_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id") ) private Set<Role> roles = new HashSet<>(); public void addRole(Role role) { this.roles.add(role); } // getters and setters are not shown for brevity }
并像這樣對(duì)Role 實(shí)體類(lèi)進(jìn)行編碼:
package net.codejava; import javax.persistence.*; @Entity @Table(name = "roles") public class Role { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(nullable = false, length = 45) private String name; public Role() { } public Role(String name) { this.name = name; } public Role(Integer id, String name) { this.id = id; this.name = name; } public Role(Integer id) { this.id = id; } @Override public String toString() { return this.name; } // getters and setters are not shown for brevity }
如您所見(jiàn),User類(lèi)有一組角色,但Role類(lèi)沒(méi)有任何對(duì) User 的引用。默認(rèn)情況下, @ManyToMany關(guān)系上沒(méi)有級(jí)聯(lián)操作——這意味著更新User對(duì)象不會(huì)更改關(guān)聯(lián)的Role對(duì)象。
2. 單元測(cè)試——創(chuàng)建角色
接下來(lái),讓我們編寫(xiě)以下測(cè)試類(lèi),用于將一些Role對(duì)象持久化到數(shù)據(jù)庫(kù)中:
package net.codejava; import static org.assertj.core.api.Assertions.assertThat; import java.util.List; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.test.annotation.Rollback; @DataJpaTest @AutoConfigureTestDatabase(replace = Replace.NONE) @Rollback(false) public class RoleRepositoryTests { @Autowired private RoleRepository repo; @Test public void testCreateRoles() { Role user = new Role("User"); Role admin = new Role("Admin"); Role customer = new Role("Customer"); repo.saveAll(List.of(user, admin, customer)); List<Role> listRoles = repo.findAll(); assertThat(listRoles.size()).isEqualTo(3); } }
運(yùn)行testCreateRoles()方法,我們最終將根據(jù) 3 個(gè)角色將 3 個(gè)新行插入到角色表中:用戶(hù)、管理員和客戶(hù)。
3. 單元測(cè)試——給用戶(hù)添加角色
要測(cè)試向用戶(hù)添加角色,請(qǐng)使用以下初始代碼創(chuàng)建UserRepositoryTests類(lèi):
package net.codejava; import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; import org.springframework.test.annotation.Rollback; @DataJpaTest @AutoConfigureTestDatabase(replace = Replace.NONE) @Rollback(false) public class UserRepositoryTests { @Autowired private TestEntityManager entityManager; @Autowired private UserRepository userRepo; @Autowired private RoleRepository roleRepo; // test methods go here... }
以下是第一個(gè)測(cè)試方法的代碼片段,它保留了一個(gè)沒(méi)有任何角色的用戶(hù)對(duì)象:
@Test public void testCreateUser() { User user = new User(); user.setEmail("ravikumar@gmail.com"); user.setPassword("ravi2020"); user.setFirstName("Ravi"); user.setLastName("Kumar"); User savedUser = userRepo.save(user); User existUser = entityManager.find(User.class, savedUser.getId()); assertThat(user.getEmail()).isEqualTo(existUser.getEmail()); }
以下測(cè)試方法創(chuàng)建一個(gè)具有管理員角色的新用戶(hù):
@Test public void testAddRoleToNewUser() { Role roleAdmin = roleRepo.findByName("Admin"); User user = new User(); user.setEmail("mikes.gates@gmail.com"); user.setPassword("mike2020"); user.setFirstName("Mike"); user.setLastName("Gates"); user.addRole(roleAdmin); User savedUser = userRepo.save(user); assertThat(savedUser.getRoles().size()).isEqualTo(1); }
以下測(cè)試將通過(guò)添加兩個(gè)角色 User 和 Customer 來(lái)更新現(xiàn)有用戶(hù):
@Test public void testAddRoleToExistingUser() { User user = userRepo.findById(1L).get(); Role roleUser = roleRepo.findByName("User"); Role roleCustomer = new Role(3); user.addRole(roleUser); user.addRole(roleCustomer); User savedUser = userRepo.save(user); assertThat(savedUser.getRoles().size()).isEqualTo(2); }
運(yùn)行這些測(cè)試方法,您將看到插入到users和users_roles表中的行。角色表不受影響。
4. 為注冊(cè)用戶(hù)設(shè)置默認(rèn)角色
用戶(hù)注冊(cè)中的一個(gè)常見(jiàn)場(chǎng)景是為新注冊(cè)的用戶(hù)設(shè)置默認(rèn)角色,例如用戶(hù)或客戶(hù)角色。以下是服務(wù)層的示例代碼片段:
package net.codejava; @Service public class UserService { @Autowired private UserRepository userRepo; @Autowired RoleRepository roleRepo; @Autowired PasswordEncoder passwordEncoder; public void registerDefaultUser(User user) { Role roleUser = roleRepo.findByName("User"); user.addRole(roleUser); userRepo.save(user); } }
以及控制器層的代碼:
package net.codejava; @Controller public class AppController { @Autowired private UserService service; @PostMapping("/register") public String processRegister(User user) { service.registerDefaultUser(user); return "register_success"; } }
如您所見(jiàn),這非常簡(jiǎn)單——感謝 Spring Data JPA 和 Hibernate 框架,極大地簡(jiǎn)化了數(shù)據(jù)訪問(wèn)層的編碼。
5. 在 Web 表單中為用戶(hù)分配角色
現(xiàn)在,我將向您展示如何使用 Web 用戶(hù)界面編寫(xiě)編輯用戶(hù)功能的代碼,我們可以在其中更改分配給用戶(hù)的角色。
首先,在UserService 類(lèi)中實(shí)現(xiàn)如下方法:
public List<User> listAll() { return userRepo.findAll(); }
在控制器類(lèi)中:
@GetMapping("/users") public String listUsers(Model model) { List<User> listUsers = service.listAll(); model.addAttribute("listUsers", listUsers); return "users"; }
此處理程序方法將顯示從數(shù)據(jù)庫(kù)中檢索到的用戶(hù)列表。并將以下相關(guān)代碼放入視圖頁(yè)面(HTML):
<table class="table table-striped table-bordered"> <thead class="thead-dark"> <tr> <th>User ID</th> <th>E-mail</th> <th>First Name</th> <th>Last Name</th> <th>Roles</th> <th></th> </tr> </thead> <tbody> <tr th:each="user: ${listUsers}"> <td th:text="${user.id}">User ID</td> <td th:text="${user.email}">E-mail</td> <td th:text="${user.firstName}">First Name</td> <td th:text="${user.lastName}">Last Name</td> <td th:text="${user.roles}">Roles</td> <td><a th:href="/@{'/users/edit/' + ${user.id}}">Edit</a></td> </tr> </tbody> </table>
它將在 URL http://localhost.../users 處顯示用戶(hù)列表,如下所示:
在此用戶(hù)列表頁(yè)面上,我們可以單擊編輯超鏈接來(lái)編輯用戶(hù)。所以像這樣編寫(xiě)處理程序方法:
@GetMapping("/users/edit/{id}") public String editUser(@PathVariable("id") Long id, Model model) { User user = service.get(id); List<Role> listRoles = service.listRoles(); model.addAttribute("user", user); model.addAttribute("listRoles", listRoles); return "user_form"; }
并在服務(wù)類(lèi)中實(shí)現(xiàn)以下兩個(gè)方法:
public User get(Long id) { return userRepo.findById(id).get(); } public List<Role> listRoles() { return roleRepo.findAll(); }
在視圖層,為編輯用戶(hù)表單編寫(xiě)如下代碼:
<form th:action="@{/users/save}" th:object="${user}" method="post" style="max-width: 600px; margin: 0 auto;"> <input type="hidden" th:field="*{id}" /> <div class="m-3"> <div class="form-group row"> <label class="col-4 col-form-label">E-mail: </label> <div class="col-8"> <input type="email" th:field="*{email}" class="form-control" required /> </div> </div> <div class="form-group row"> <label class="col-4 col-form-label">Password: </label> <div class="col-8"> <input type="password" th:field="*{password}" class="form-control" required minlength="6" maxlength="10"/> </div> </div> <div class="form-group row"> <label class="col-4 col-form-label">First Name: </label> <div class="col-8"> <input type="text" th:field="*{firstName}" class="form-control" required minlength="2" maxlength="20"/> </div> </div> <div class="form-group row"> <label class="col-4 col-form-label">Last Name: </label> <div class="col-8"> <input type="text" th:field="*{lastName}" class="form-control" required minlength="2" maxlength="20" /> </div> </div> <div class="form-group row"> <label class="col-4 col-form-label">Roles: </label> <div class="col-8"> <th:block th:each="role: ${listRoles}"> <input type="checkbox" th:field="*{roles}" th:text="${role.name}" th:value="${role.id}" class="m-2" /> </th:block> </div> </div> <div> <button type="submit" class="btn btn-primary">Update</button> </div> </div> </form>
此頁(yè)面中最重要的是顯示角色列表并檢查分配給當(dāng)前用戶(hù)的角色的代碼:
<th:block th:each="role: ${listRoles}"> <input type="checkbox" th:field="*{roles}" th:text="${role.name}" th:value="${role.id}" class="m-2" /> </th:block>
然后編輯用戶(hù)表單將如下所示:
這里很酷的是,Thymeleaf 會(huì)根據(jù)分配給用戶(hù)的角色自動(dòng)顯示選擇的角色。此外,您可以在此處簡(jiǎn)單地選中/取消選中角色來(lái)更新用戶(hù)的角色。
并編寫(xiě)處理表單提交的處理程序方法,如下所示:
@PostMapping("/users/save") public String saveUser(User user) { service.save(user); return "redirect:/users"; }
以及服務(wù)層的相關(guān)代碼:
public void save(User user) { userRepo.save(user); }
這是一些關(guān)于在 Spring Boot Web 應(yīng)用程序中向用戶(hù)添加角色的代碼示例。我希望您發(fā)現(xiàn)這個(gè)書(shū)面教程對(duì)您有所幫助。
總結(jié)
到此這篇關(guān)于Spring Security如何為用戶(hù)示例添加角色的文章就介紹到這了,更多相關(guān)Spring Security用戶(hù)示例添加角色內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于ReentrantLock的實(shí)現(xiàn)原理解讀
這篇文章主要介紹了關(guān)于ReentrantLock的實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06導(dǎo)入maven項(xiàng)目各個(gè)注解均報(bào)錯(cuò)的解決方案
這篇文章主要介紹了導(dǎo)入maven項(xiàng)目各個(gè)注解均報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12小程序與后端Java接口交互實(shí)現(xiàn)HelloWorld入門(mén)
本文主要介紹了小程序與后端Java接口交互實(shí)現(xiàn)HelloWorld入門(mén) ,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07Java計(jì)算數(shù)學(xué)表達(dá)式代碼詳解
這篇文章主要介紹了Java計(jì)算數(shù)學(xué)表達(dá)式代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12Java date format時(shí)間格式化操作示例
這篇文章主要介紹了Java date format時(shí)間格式化操作,結(jié)合具體實(shí)例形式分析了java針對(duì)日期時(shí)間進(jìn)行格式化操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03SpringCloud 服務(wù)網(wǎng)關(guān)路由規(guī)則的坑及解決
這篇文章主要介紹了SpringCloud 服務(wù)網(wǎng)關(guān)路由規(guī)則的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07