当前位置: 首页 > news >正文

怎么自定义spring security对用户信息进行校验及密码的加密校验

先写一个spring security需要校验的字段类

其实UserDetails的子类的user已经有很多字段和功能,但是如果我们需要扩展的话就要重写UserDetails中的方法

package com.lzy.security;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;import java.util.Collection;
import java.util.Collections;
import java.util.Set;public class AccountUser implements UserDetails {//加上idprivate Long userId;private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;private static final Log logger = LogFactory.getLog(User.class);private String password;private final String username;private final Collection<?extends GrantedAuthority> authorities;private final boolean accountNonExpired;private final boolean accountNonLocked;private final boolean credentialsNonExpired;private final boolean enabled;public AccountUser(Long userId,String username, String password, Collection<? extends GrantedAuthority> authorities) {this(userId,username, password, true, true, true, true, authorities);}public AccountUser(Long userId,String username, String password, boolean enabled, boolean accountNonExpired,boolean credentialsNonExpired, boolean accountNonLocked,Collection<? extends GrantedAuthority> authorities) {Assert.isTrue(username != null && !"".equals(username) && password != null,"Cannot pass null or empty values to constructor");this.username = username;this.userId = userId;this.password = password;this.enabled = enabled;this.accountNonExpired = accountNonExpired;this.credentialsNonExpired = credentialsNonExpired;this.accountNonLocked = accountNonLocked;this.authorities = authorities;}@Overridepublic Collection<? extends GrantedAuthority> getAuthorities() {return this.authorities;}@Overridepublic String getPassword() {return this.password;}@Overridepublic String getUsername() {return this.username;}@Overridepublic boolean isAccountNonExpired() {return this.accountNonExpired;}@Overridepublic boolean isAccountNonLocked() {return this.accountNonLocked;}@Overridepublic boolean isCredentialsNonExpired() {return this.credentialsNonExpired;}@Overridepublic boolean isEnabled() {return this.enabled;}
}

这里获取数据行对象,并且返回我们需要校验的字段

package com.lzy.security;import com.lzy.entity.SysUser;
import com.lzy.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;import java.util.List;
@Service
public class UserDetailsServiceImpl implements UserDetailsService {@AutowiredISysUserService sysUserService;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//根据用户名查询用户信息SysUser sysUser = sysUserService.getByUsername(username);if (sysUser == null) {throw new UsernameNotFoundException("用户名不存在");}return new AccountUser(sysUser.getId(),sysUser.getUsername(),sysUser.getPassword(),getUserAuthority(sysUser.getId()));}public List<GrantedAuthority> getUserAuthority(Long userId) {//根据用户id查询用户权限return null;}
}

最后在spring security的配置文件中

注入

    @AutowiredUserDetailsServiceImpl userDetailsServiceImpl;

重写他的方法

    @Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userDetailsServiceImpl);}

最后,注释配置文件中的默认账号密码

  security:user:name: lzypassword: 123456

密码的加密校验

先引入BCryptPasswordEncoder 

    @BeanBCryptPasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}

再在显示配置中对他进行一个配置

    @Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userDetailsServiceImpl).passwordEncoder(passwordEncoder());}


http://www.mrgr.cn/news/10939.html

相关文章:

  • 深度学习--负采样技术及其扩展详解
  • 卡通人物表白/生日快乐网站源码html
  • 原生JS实现鼠标下滑模块自定位
  • wooyu漏洞库YYDS!!!入门之道:重现乌云漏洞库
  • 汇编基础指令
  • redis集群部署
  • MindSearch 部署
  • 《黑神话:悟空》游戏中的江苏元素
  • golang-gin使用中间件处理文本-时间字符串格式
  • 深入理解Pandas:数据处理的核心技能与应用(四)
  • 【红队技巧】.Net免杀 绕过主流杀软
  • SpringBoot文档之Logging的阅读笔记
  • Swift中的可选类型:揭开Optional的神秘面纱
  • 如何使用ssm实现品牌手机销售信息系统
  • 利用机器学习推动 vSOC 检测
  • 【ShuQiHere】从零开始实现逻辑回归:深入理解反向传播与梯度下降
  • Vue55 动画与过度
  • 【openwrt-21.02】openwrt-21.02 T750 按键GPIO自动进入刷机模式功能实现
  • 热点 | 爆款游戏的诞生与游戏出海的持续增长
  • Redis应用