首页 > 其他分享 >springboot3 security 从始至终--03 UserDetails

springboot3 security 从始至终--03 UserDetails

时间:2023-02-09 23:58:19浏览次数:40  
标签:03 return String -- boolean springboot3 password public user

一、定义

/**
 * Provides core user information.
 *
 * <p>
 * Implementations are not used directly by Spring Security for security purposes. They
 * simply store user information which is later encapsulated into {@link Authentication}
 * objects. This allows non-security related user information (such as email addresses,
 * telephone numbers etc) to be stored in a convenient location.
 * <p>
 * Concrete implementations must take particular care to ensure the non-null contract
 * detailed for each method is enforced. See
 * {@link org.springframework.security.core.userdetails.User} for a reference
 * implementation (which you might like to extend or use in your code).
 *
 * @author Ben Alex
 * @see UserDetailsService
 * @see UserCache
 */
public interface UserDetails extends Serializable {

	/**
	 * Returns the authorities granted to the user. Cannot return <code>null</code>.
	 * @return the authorities, sorted by natural key (never <code>null</code>)
	 */
	Collection<? extends GrantedAuthority> getAuthorities();

	/**
	 * Returns the password used to authenticate the user.
	 * @return the password
	 */
	String getPassword();

	/**
	 * Returns the username used to authenticate the user. Cannot return
	 * <code>null</code>.
	 * @return the username (never <code>null</code>)
	 */
	String getUsername();

	/**
	 * Indicates whether the user's account has expired. An expired account cannot be
	 * authenticated.
	 * @return <code>true</code> if the user's account is valid (ie non-expired),
	 * <code>false</code> if no longer valid (ie expired)
	 */
	boolean isAccountNonExpired();

	/**
	 * Indicates whether the user is locked or unlocked. A locked user cannot be
	 * authenticated.
	 * @return <code>true</code> if the user is not locked, <code>false</code> otherwise
	 */
	boolean isAccountNonLocked();

	/**
	 * Indicates whether the user's credentials (password) has expired. Expired
	 * credentials prevent authentication.
	 * @return <code>true</code> if the user's credentials are valid (ie non-expired),
	 * <code>false</code> if no longer valid (ie expired)
	 */
	boolean isCredentialsNonExpired();

	/**
	 * Indicates whether the user is enabled or disabled. A disabled user cannot be
	 * authenticated.
	 * @return <code>true</code> if the user is enabled, <code>false</code> otherwise
	 */
	boolean isEnabled();

}

说人话,翻译过来就是

方法 作用
Collection<? extends GrantedAuthority> getAuthorities(); 用户的权限集
String getPassword(); 用户的加密后的密码
String getUsername(); 用户名
boolean isAccountNonExpired(); 账户是否过期
boolean isAccountNonLocked(); 账户是否锁定
boolean isCredentialsNonExpired(); 凭证是否过期
boolean isEnabled(); 用户是否可用

二、自定义UserDetails

默认情况下UserDetails支持用户名和密码的形式,如果某系统需要使用邮箱、手机号登录,或者其它用户信息,则需要自定义UserDetails的实现。

public class JwtUserDetails implements UserDetails {

    private static final long serialVersionUID = 1L;

    private String username;
    private String password;
    private String salt;
    private Collection<? extends GrantedAuthority> authorities;

    public JwtUserDetails(String username, String password, String salt, Collection<? extends GrantedAuthority> authorities) {
        this.username = username;
        this.password = password;
        this.salt = salt;
        this.authorities = authorities;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return password;
    }

    public String getSalt() {
        return salt;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}

标签:03,return,String,--,boolean,springboot3,password,public,user
From: https://www.cnblogs.com/zhouXX/p/17107514.html

相关文章

  • ELK企业级系统日志架构
    企业级日志系统架构——ELK(Elasticsearch、Filebeat、Kafka、Logstash、Kibana)原创 liugp 大数据与云原生技术分享 2023-01-2707:30 发表于江西收录于合集#ELK......
  • 开源分布式支持超大规模数据分析型数据仓库Apache Kylin实践-上
    @目录概述定义特性术语技术概念架构和组件生态圈部署Docker部署基于hadoop环境安装前置条件安装使用步骤官方样例Cube说明示例演示准备演示数据创建项目选择数据源创建Mode......
  • Apache Hudi 设计与架构解读
    1.简介ApacheHudi(简称:Hudi)允许您在现有的hadoop兼容存储之上存储大量数据,同时提供两种原语,使得除了经典的批处理之外,还可以在数据湖上进行流处理。这两种原语分别是:......
  • 第一章_C语言快速入门
    目录1C语言快速入门1.1信息在计算机中的表示1.2C语言快速入门1C语言快速入门1.1信息在计算机中的表示√1.2C语言快速入门第一个C++程序#include<iostream>#i......
  • 提示Composer安装-需要ext-mbstring
    今天尝试通过composerrequire安装phpoffice/phpspreadsheet容器,去使用excel的操作由于PHPExcel已经被废弃在PHP7.2中已经无法获取更新,官方重新开了一个新包phpspreadshe......
  • Prometheus安装部署及监控linux主机
    Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.Prometheus基本原理是通过HTTP协议周期性抓取被监控组件的状态,这样做的好......
  • .NET中Trace类和Debug类的应用举例
    原文网址:https://blog.csdn.net/yunhaic/article/details/4863322.NET中的Trace类和Debug类是System.Diagnostics命名空间下的两个用于调试的类,在软件开发中使用得当,可以......
  • python easyocr和cv2实现名片识别及裁剪摆正
    公司希望能做一个名片裁剪和识别功能。我来开发小程序,在寻找合适的api的途中,因嫌弃乙方弄得太慢,自己百度搞了个python版本的。很久没用python了好多基础方法都忘记了,实现的......
  • k8s磁盘挂载
    k8s自动化运维十-磁盘挂载原创 孟凡霄 平凡人笔记 2022-09-2300:22 发表于上海承接上文k8s自动化运维九列出磁盘分区fdisk-l物理分区只分了一个/dev/sda......
  • 10.C语言文件操作
    10.2文件的打开和关闭10.2.1文件指针在C语言中用一个指针变量指向一个文件,这个指针称为文件指针。typedefstruct{shortlevel; //缓冲区"满"或者"空"......