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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring中@ComponentScan自動掃描并指定掃描規(guī)則
本文主要介紹了spring中@ComponentScan自動掃描并指定掃描規(guī)則,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
教你如何把Eclipse創(chuàng)建的Web項(xiàng)目(非Maven)導(dǎo)入Idea
這篇文章主要介紹了教你如何把Eclipse創(chuàng)建的Web項(xiàng)目(非Maven)導(dǎo)入Idea,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
WebUploader實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了WebUploader實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03
Spring實(shí)現(xiàn)在非controller中獲取request對象
這篇文章主要介紹了Spring實(shí)現(xiàn)在非controller中獲取request對象方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Java鏈表數(shù)據(jù)結(jié)構(gòu)及其簡單使用方法解析
這篇文章主要介紹了Java鏈表數(shù)據(jù)結(jié)構(gòu)及其簡單使用方法解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07
詳解SpringBoot配置文件啟動時動態(tài)配置參數(shù)方法
這篇文章主要介紹了詳解SpringBoot配置文件啟動時動態(tài)配置參數(shù)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java中CyclicBarrier和CountDownLatch的用法與區(qū)別
CyclicBarrier和CountDownLatch這兩個工具都是在java.util.concurrent包下,并且平時很多場景都會使用到。本文將會對兩者進(jìn)行分析,記錄他們的用法和區(qū)別,感興趣的可以了解一下2021-08-08
Java實(shí)現(xiàn)XML與JSON秒級轉(zhuǎn)換示例詳解
這篇文章主要為大家介紹了Java實(shí)現(xiàn)XML與JSON秒級轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

