Spring?Security用戶定義?
更新時間:2022年02月18日 14:10:34 作者:周杰倫本人
這篇文章主要介紹了Spring?Security用戶定義,大家都知道?Spring?Security的用戶定義有很多方式,其實主要有兩種,基于內(nèi)存的和基于數(shù)據(jù)庫的,下面我給大家簡單介紹一下這兩種方式,需要的朋友可以參考下
基于內(nèi)存的和基于數(shù)據(jù)庫的,下面我給大家簡單介紹一下這兩種方式。
一、基于內(nèi)存
Spring Security中的配置:
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { ? ? InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); ? ? manager.createUser(User.withUsername("admin").password("{noop}123").roles("admin").build()); ? ? manager.createUser(User.withUsername("sang").password("{noop}123").roles("user").build()); ? ? auth.userDetailsService(manager); }
二、基于mybatis
MyUserDetailsService
@Service public class MyUserDetailsService implements UserDetailsService { ? ? @Autowired ? ? UserMapper userMapper; ? ? @Override ? ? public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { ? ? ? ? User user = userMapper.loadUserByUsername(username); ? ? ? ? if (user == null) { ? ? ? ? ? ? throw new UsernameNotFoundException("用戶不存在"); ? ? ? ? } ? ? ? ? user.setRoles(userMapper.getRolesByUid(user.getId())); ? ? ? ? return user; ? ? } }
User類:
public class User implements UserDetails { ? ? private Integer id; ? ? private String username; ? ? private String password; ? ? private Boolean enabled; ? ? private Boolean accountNonExpired; ? ? private Boolean accountNonLocked; ? ? private Boolean credentialsNonExpired; ? ? private List<Role> roles = new ArrayList<>(); ? ? @Override ? ? public String toString() { ? ? ? ? return "User{" + ? ? ? ? ? ? ? ? "id=" + id + ? ? ? ? ? ? ? ? ", username='" + username + '\'' + ? ? ? ? ? ? ? ? ", password='" + password + '\'' + ? ? ? ? ? ? ? ? ", enabled=" + enabled + ? ? ? ? ? ? ? ? ", accountNonExpired=" + accountNonExpired + ? ? ? ? ? ? ? ? ", accountNonLocked=" + accountNonLocked + ? ? ? ? ? ? ? ? ", credentialsNonExpired=" + credentialsNonExpired + ? ? ? ? ? ? ? ? ", roles=" + roles + ? ? ? ? ? ? ? ? '}'; ? ? } ? ? @Override ? ? public Collection<? extends GrantedAuthority> getAuthorities() { ? ? ? ? List<SimpleGrantedAuthority> authorities = new ArrayList<>(); ? ? ? ? for (Role role : roles) { ? ? ? ? ? ? authorities.add(new SimpleGrantedAuthority(role.getName())); ? ? ? ? } ? ? ? ? return authorities; ? ? } ? ? @Override ? ? public String getPassword() { ? ? ? ? return password; ? ? } ? ? @Override ? ? public String getUsername() { ? ? ? ? return username; ? ? } ? ? @Override ? ? public boolean isAccountNonExpired() { ? ? ? ? return accountNonExpired; ? ? } ? ? @Override ? ? public boolean isAccountNonLocked() { ? ? ? ? return accountNonLocked; ? ? } ? ? @Override ? ? public boolean isCredentialsNonExpired() { ? ? ? ? return credentialsNonExpired; ? ? } ? ? @Override ? ? public boolean isEnabled() { ? ? ? ? return enabled; ? ? } ? ? public void setId(Integer id) { ? ? ? ? this.id = id; ? ? } ? ? public void setUsername(String username) { ? ? ? ? this.username = username; ? ? } ? ? public void setPassword(String password) { ? ? ? ? this.password = password; ? ? } ? ? public void setEnabled(Boolean enabled) { ? ? ? ? this.enabled = enabled; ? ? } ? ? public void setAccountNonExpired(Boolean accountNonExpired) { ? ? ? ? this.accountNonExpired = accountNonExpired; ? ? } ? ? public void setAccountNonLocked(Boolean accountNonLocked) { ? ? ? ? this.accountNonLocked = accountNonLocked; ? ? } ? ? public void setCredentialsNonExpired(Boolean credentialsNonExpired) { ? ? ? ? this.credentialsNonExpired = credentialsNonExpired; ? ? } ? ? public Integer getId() { ? ? ? ? return id; ? ? } ? ? public List<Role> getRoles() { ? ? ? ? return roles; ? ? } ? ? public void setRoles(List<Role> roles) { ? ? ? ? this.roles = roles; ? ? } }
Spring Security中的配置:
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { ? ? auth.userDetailsService(myUserDetailsService); }
到此這篇關(guān)于Spring Security用戶定義 的文章就介紹到這了,更多相關(guān)Spring Security用戶定義 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Data JPA中的Specification動態(tài)查詢詳解
Specification是一個設(shè)計模式,用于企業(yè)級應(yīng)用開發(fā)中,其主要目的是將業(yè)務(wù)規(guī)則從業(yè)務(wù)邏輯中分離出來,在數(shù)據(jù)查詢方面,Specification可以定義復(fù)雜的查詢,使其更易于重用和測試,這篇文章主要介紹了Spring Data JPA中的Specification動態(tài)查詢詳解,需要的朋友可以參考下2023-07-07SpringBoot使用Mybatis-Generator配置過程詳解
這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02