欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Security用戶(hù)定義?

 更新時(shí)間:2022年02月18日 14:10:34   作者:周杰倫本人  
這篇文章主要介紹了Spring?Security用戶(hù)定義,大家都知道?Spring?Security的用戶(hù)定義有很多方式,其實(shí)主要有兩種,基于內(nèi)存的和基于數(shù)據(jù)庫(kù)的,下面我給大家簡(jiǎn)單介紹一下這兩種方式,需要的朋友可以參考下

基于內(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)文章希望大家以后多多支持腳本之家!

  • Kryo序列化及反序列化用法示例

    Kryo序列化及反序列化用法示例

    這篇文章主要介紹了Kryo序列化及反序列化用法示例,小編覺(jué)得挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下。
    2017-10-10
  • java中對(duì)象為null時(shí)的打印輸出方式

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

    這篇文章主要介紹了java中對(duì)象為null時(shí)的打印輸出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • java 面向?qū)ο竺嬖嚰\

    java 面向?qū)ο竺嬖嚰\

    這篇文章主要介紹了java 面向?qū)ο竺嬖嚰\的相關(guān)資料,這里整理了面向?qū)ο蟮幕A(chǔ)知識(shí),幫助大家學(xué)習(xí)理解此部分的知識(shí),需要的朋友可以參考下
    2016-11-11
  • idea安裝hsdis的方法

    idea安裝hsdis的方法

    這篇文章主要介紹了idea安裝hsdis,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • 輕松掌握java中介者模式

    輕松掌握java中介者模式

    這篇文章主要幫助大家輕松掌握java中介者模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • SpringBoot bean查詢(xún)加載順序流程詳解

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

    當(dāng)你在項(xiàng)目啟動(dòng)時(shí)需要提前做一個(gè)業(yè)務(wù)的初始化工作時(shí),或者你正在開(kāi)發(fā)某個(gè)中間件需要完成自動(dòng)裝配時(shí)。你會(huì)聲明自己的Configuration類(lèi),但是可能你面對(duì)的是好幾個(gè)有互相依賴(lài)的Bean
    2023-03-03
  • SpringBoot使用Mybatis-Generator配置過(guò)程詳解

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

    這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 關(guān)于SpringBoot中的請(qǐng)求映射及使用

    關(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
  • mybatis中關(guān)于in的使用方法及說(shuō)明

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

    這篇文章主要介紹了mybatis中關(guān)于in的使用方法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 最新評(píng)論