其他分享
首页 > 其他分享> > Spring Security - 基本使用

Spring Security - 基本使用

作者:互联网

Service:

//实现UserDetailsService接口,重写loadUserByUsername方法
@Service
public class LeUserServiceImpl implements UserDetailsService {
    //操作数据库的mapper
    @Autowired
    LeUserMapper lum;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //调用mapper方法,返回一个用户
        LeUser leUserByName = lum.getLeUserByName(s);
        if(Objects.isNull(leUserByName)){
            throw new RuntimeException("用户不存在");
        }
        //把mapper查询到的结果包装为UserDetails
        return new LeUserLogin(leUserByName);
    }
}

 

Bean:

//重写UserDetails
public class LeUserLogin implements UserDetails {
    //自定义的user类
    private LeUser leUser;

    public LeUserLogin(LeUser leUser) {
        this.leUser = leUser;
    }

    //权限
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    //用户密码
    @Override
    public String getPassword() {
        return leUser.getUserPassword();
    }

    //用户名称
    @Override
    public String getUsername() {
        return leUser.getUserName();
    }

    //账号是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //账号是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //密码是否未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //是否激活
    @Override
    public boolean isEnabled() {
        return true;
    }
}

 

标签:基本,return,Spring,boolean,leUser,Override,Security,public,UserDetails
来源: https://www.cnblogs.com/leviAckman/p/16353459.html