Spring?Security用戶定義?
基于內(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Data JPA中的Specification動(dòng)態(tài)查詢詳解
Specification是一個(gè)設(shè)計(jì)模式,用于企業(yè)級(jí)應(yīng)用開發(fā)中,其主要目的是將業(yè)務(wù)規(guī)則從業(yè)務(wù)邏輯中分離出來,在數(shù)據(jù)查詢方面,Specification可以定義復(fù)雜的查詢,使其更易于重用和測試,這篇文章主要介紹了Spring Data JPA中的Specification動(dòng)態(tài)查詢詳解,需要的朋友可以參考下2023-07-07
java中對(duì)象為null時(shí)的打印輸出方式
這篇文章主要介紹了java中對(duì)象為null時(shí)的打印輸出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
SpringBoot使用Mybatis-Generator配置過程詳解
這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
關(guān)于SpringBoot中的請(qǐng)求映射及使用
這篇文章主要介紹了關(guān)于SpringBoot中的請(qǐng)求映射及使用,Spring Boot 中的授權(quán)機(jī)制,包括基于角色的授權(quán)和基于資源的授權(quán),同時(shí),我們也將給出相應(yīng)的代碼示例,幫助讀者更好地理解和應(yīng)用這些授權(quán)機(jī)制,需要的朋友可以參考下2023-07-07

