Spring Security测试代码
⒈实体Bean
1 package cn.coreqi.blog.entities; 2 3 import org.springframework.security.core.GrantedAuthority; 4 import javax.persistence.*; 5 6 /** 7 * 表示权限的实体类 8 * @author fanqi 9 */ 10 @Entity 11 public class Authority implements GrantedAuthority { 12 @Id 13 @GeneratedValue(strategy = GenerationType.IDENTITY) //自增长 14 private Long id; 15 16 @Column(nullable = false) //值不能为空 17 private String name; 18 19 public Long getId() { 20 return id; 21 } 22 23 public void setId(Long id) { 24 this.id = id; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 @Override 36 public String getAuthority() { 37 return this.name; 38 } 39 }
1 package cn.coreqi.blog.entities; 2 3 import org.springframework.security.core.GrantedAuthority; 4 import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 import org.springframework.security.core.userdetails.UserDetails; 6 import javax.persistence.*; 7 import javax.validation.constraints.Email; 8 import javax.validation.constraints.NotEmpty; 9 import javax.validation.constraints.Size; 10 import java.io.Serializable; 11 import java.util.ArrayList; 12 import java.util.Collection; 13 import java.util.List; 14 15 /** 16 * 用户实体类 17 * @author fanqi 18 */ 19 @Entity 20 public class User implements Serializable,UserDetails { 21 @Id 22 @GeneratedValue(strategy = GenerationType.IDENTITY) //自增主键 23 private Long id; //主键ID 24 25 @NotEmpty(message = "姓名不能为空!") 26 @Size(min = 2,max = 20) 27 @Column(nullable = false,length = 20) 28 private String name; 29 30 @NotEmpty(message = "邮箱不能为空!") 31 @Size(max = 50) 32 @Email(message = "邮箱格式不正确!") 33 @Column(nullable = false,length = 50,unique = true) 34 private String email; 35 36 @NotEmpty(message = "用户账号不能为空!") 37 @Size(min = 6,max = 18) 38 @Column(nullable = false,length = 18,unique = true) 39 private String username; //用户账号 40 41 @NotEmpty(message = "用户密码不能为空!") 42 @Size(max = 20) 43 @Column(length = 20) 44 private String password; //用户密码 45 46 @Column(length = 200) 47 private String avatar; //头像图片地址 48 49 @ManyToMany(cascade = CascadeType.DETACH,fetch = FetchType.EAGER) 50 @JoinTable(name = "user_authority", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), 51 inverseJoinColumns = @JoinColumn(name = "authority_id", referencedColumnName = "id")) 52 private List<Authority> authorities; 53 54 public User() { 55 } 56 57 public Long getId() { 58 return id; 59 } 60 61 public void setId(Long id) { 62 this.id = id; 63 } 64 65 public String getName() { 66 return name; 67 } 68 69 public void setName(String name) { 70 this.name = name; 71 } 72 73 public String getEmail() { 74 return email; 75 } 76 77 public void setEmail(String email) { 78 this.email = email; 79 } 80 81 public void setUsername(String username) { 82 this.username = username; 83 } 84 85 public void setPassword(String password) { 86 this.password = password; 87 } 88 89 public String getAvatar() { 90 return avatar; 91 } 92 93 public void setAvatar(String avatar) { 94 this.avatar = avatar; 95 } 96 97 public void setAuthorities(List<Authority> authorities) { 98 this.authorities = authorities; 99 } 100 101 @Override 102 public String toString() { 103 return "User{" + 104 "id=" + id + 105 ", name='" + name + '\'' + 106 ", email='" + email + '\'' + 107 ", username='" + username + '\'' + 108 ", password='" + password + '\'' + 109 ", avatar='" + avatar + '\'' + 110 ", authorities=" + authorities + 111 '}'; 112 } 113 114 @Override 115 public Collection<? extends GrantedAuthority> getAuthorities() { 116 // return authorities; 117 // 需将 List<Authority> 转成 List<SimpleGrantedAuthority>,否则前端拿不到角色列表名称 118 List<SimpleGrantedAuthority> simpleAuthorities = new ArrayList<>(); 119 for(GrantedAuthority authority : authorities){ 120 simpleAuthorities.add(new SimpleGrantedAuthority(authority.getAuthority())); 121 } 122 return simpleAuthorities; 123 } 124 125 @Override 126 public String getPassword() { 127 return password; 128 } 129 130 @Override 131 public String getUsername() { 132 return username; 133 } 134 135 @Override 136 public boolean isAccountNonExpired() { //账号是否未过期 137 return true; 138 } 139 140 @Override 141 public boolean isAccountNonLocked() { //账号是否未锁 142 return true; 143 } 144 145 @Override 146 public boolean isCredentialsNonExpired() { //验证信息是否未过期 147 return true; 148 } 149 150 @Override 151 public boolean isEnabled() { //账号是否启动 152 return true; 153 } 154 }
⒉仓库
1 package cn.coreqi.blog.repository; 2 3 import cn.coreqi.blog.entities.Authority; 4 import org.springframework.data.jpa.repository.JpaRepository; 5 6 public interface AuthorityRepository extends JpaRepository<Authority,Long> { 7 }
⒊服务层
1 package cn.coreqi.service; 2 3 import cn.coreqi.blog.entities.Authority; 4 import org.springframework.stereotype.Service; 5 6 public interface AuthorityService { 7 /** 8 * 根据ID查询权限 9 * @param id 10 * @return 11 */ 12 Authority getAuthorityById(Long id); 13 }
1 package cn.coreqi.service.impl; 2 3 import cn.coreqi.blog.entities.Authority; 4 import cn.coreqi.blog.repository.AuthorityRepository; 5 import cn.coreqi.service.AuthorityService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 @Service 10 public class AuthorityServiceImpl implements AuthorityService { 11 12 @Autowired 13 private AuthorityRepository repository; 14 15 @Override 16 public Authority getAuthorityById(Long id) { 17 return repository.findById(id).get(); 18 } 19 }
⒋控制层(待补充