當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringSecurity集中式整合之使用数据库数据实现认证
生活随笔
收集整理的這篇文章主要介紹了
SpringSecurity集中式整合之使用数据库数据实现认证
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
創建用戶pojo對象
這里直接實現SpringSecurity的用戶對象接口,并添加角色集合私有屬性。注意接口屬性都要標記不參與json的處理
@Data public class SysRole implements GrantedAuthority {private Integer id;private String roleName;private String roleDesc; }創建角色pojo對象
這里直接使用SpringSecurity的角色規范,我們實現UserDetails的類型
@Data public class SysUser implements UserDetails {private Integer id;private String username;private String password;private Integer status;private List<SysRole> roles;@JsonIgnore@Overridepublic Collection<? extends GrantedAuthority> getAuthorities() {return roles;}@Overridepublic String getPassword() {return password;}@Overridepublic String getUsername() {return username;}@JsonIgnore@Overridepublic boolean isAccountNonExpired() {return true;}@JsonIgnore@Overridepublic boolean isAccountNonLocked() {return true;}@JsonIgnore@Overridepublic boolean isCredentialsNonExpired() {return true;}@JsonIgnore@Overridepublic boolean isEnabled() {return true;} }提供角色mapper接口
public interface RoleMapper extends Mapper<SysRole> {@Select("SELECT r.id, r.role_name roleName, r.role_desc roleDesc " +"FROM sys_role r, sys_user_role ur " +"WHERE r.id=ur.rid AND ur.uid=#{uid}")public List<SysRole> findByUid(Integer uid); }提供用戶mapper接口
這里就用到了Mybatis的一對多進行操作
public interface UserMapper extends Mapper<SysUser> {@Select("select * from sys_user where username = #{username}")@Results({@Result(id = true, property = "id", column = "id"),@Result(property = "roles", column = "id", javaType = List.class,many = @Many(select = "com.itheima.mapper.RoleMapper.findByUid"))})public SysUser findByName(String username); }提供認證service接口
@Service @Transactional public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {return userMapper.findByUsername(s);} }總結
以上是生活随笔為你收集整理的SpringSecurity集中式整合之使用数据库数据实现认证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 集中式整合之编写springsecuri
- 下一篇: SpringSecurity集中式整合之