ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 第一步 编写实体类 ~~~ import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; import java.util.Date; /** * <p> * 用户表 * </p> * */ @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @TableName("sys_user") public class Users implements Serializable { private static final long serialVersionUID = 1L; /** * id */ @TableId(type = IdType.ASSIGN_ID) private String id; /** * 登录账号 */ private String username; /** * 真实姓名 */ private String realname; /** * 密码 */ @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private String password; /** * md5密码盐 */ @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private String salt; /** * 头像 */ private String avatar; /** * 生日 */ @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") private Date birthday; /** * 性别(1:男 2:女) */ private Integer sex; /** * 电子邮件 */ private String email; /** * 电话 */ private String phone; /** * 部门code(当前选择登录部门) */ private String orgCode; /**部门名称*/ private transient String orgCodeTxt; /** * 状态(1:正常 2:冻结 ) */ private Integer status; /** * 删除状态(0,正常,1已删除) */ @TableLogic private Integer delFlag; /** * 工号,唯一键 */ private String workNo; /** * 职务,关联职务表 */ private String post; /** * 座机号 */ private String telephone; /** * 创建人 */ private String createBy; /** * 创建时间 */ private Date createTime; /** * 更新人 */ private String updateBy; /** * 更新时间 */ private Date updateTime; /** * 同步工作流引擎1同步0不同步 */ private Integer activitiSync; /** * 身份(0 普通成员 1 上级) */ private Integer userIdentity; /** * 负责部门 */ private String departIds; /** * 多租户id配置,编辑用户的时候设置 */ private String relTenantIds; /**设备id uniapp推送用*/ private String clientId; } ~~~ # 第二步 编写mapper ~~~ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.maicaii.models.entity.Users; import org.springframework.stereotype.Repository; @Repository public interface UsersMapper extends BaseMapper<Users> { } ~~~ # 第三步 编写security配置类 ~~~ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.maicaii.models.entity.Users; import org.maicaii.models.mapper.UsersMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import java.util.List; @Service("userDetailsService") public class MyUserDetailsService implements UserDetailsService { @Autowired private UsersMapper usersMapper; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String password = passwordEncoder.encode("aaa");//加密密码 //使用userMapper方法,使用用户名查询数据库 QueryWrapper<Users> wrapper = new QueryWrapper<>();//查询构造器 wrapper.eq("username",username);//条件 Users users= usersMapper.selectOne(wrapper);//查询一条数据 if (users==null){//用户不存在 throw new UsernameNotFoundException("用户不存在"); } List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("auths");//权限集合 return new User(users.getUsername(),password,auths);//用户名,密码,权限 } } ~~~ # 第四步 编写加密方式和设置自定义用户 ~~~ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; //第二种 @Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService)//自定义设置类 .passwordEncoder(password()); } @Bean //加密方式 public PasswordEncoder password(){ return new BCryptPasswordEncoder(); } } ~~~ ![](https://img.kancloud.cn/06/17/061703ff6922a20b60559133bc12ad45_1602x677.png)