Spring?Security用戶(hù)定義?
基于內(nèi)存的和基于數(shù)據(jù)庫(kù)的,下面我給大家簡(jiǎn)單介紹一下這兩種方式。
一、基于內(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("用戶(hù)不存在"); ? ? ? ? } ? ? ? ? user.setRoles(userMapper.getRolesByUid(user.getId())); ? ? ? ? return user; ? ? } }
User類(lèi):
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用戶(hù)定義 的文章就介紹到這了,更多相關(guān)Spring Security用戶(hù)定義 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章

java中對(duì)象為null時(shí)的打印輸出方式

SpringBoot bean查詢(xún)加載順序流程詳解

SpringBoot使用Mybatis-Generator配置過(guò)程詳解

關(guān)于SpringBoot中的請(qǐng)求映射及使用

mybatis中關(guān)于in的使用方法及說(shuō)明