SpringBoot Security密码加盐实例

目录
  • 修改加密和验证方法
  • 自定义 DaoAuthenticationProvider
  • 注册到ProciderManager中

修改加密和验证方法

    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @param salt 盐值
     * @return 加密字符串
     */
    public static String encryptPassword(String password,String salt) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
         return passwordEncoder.encode(password + salt);
    }
    /**
     * 判断密码是否相同
     *
     * @param rawPassword     真实密码
     * @param encodedPassword 加密后字符
     * @param salt 盐值
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword,String salt) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword + salt, encodedPassword);
    }

自定义 DaoAuthenticationProvider

import com.maruifu.common.core.domain.model.LoginUser;
import com.maruifu.common.utils.DateUtils;
import com.maruifu.common.utils.SecurityUtils;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.Authentication;
/**
 * 身份验证提供者
 * @author maruifu
 */
public class JwtAuthenticationProvider extends DaoAuthenticationProvider {
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        // 可以在此处覆写整个登录认证逻辑
        return super.authenticate(authentication);
    }
    /**
     * 重写加盐后验证逻辑
     * @param userDetails
     * @param authentication
     * @throws AuthenticationException
     */
    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        if (authentication.getCredentials() == null) {
            this.logger.debug("Failed to authenticate since no credentials provided");
            throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
        } else {
            String presentedPassword = authentication.getCredentials().toString();
            LoginUser loginUser =  (LoginUser)userDetails ;
            if (!SecurityUtils.matchesPassword(presentedPassword, userDetails.getPassword(), DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,loginUser.getUser().getCreateTime()))) {
                this.logger.debug("Failed to authenticate since password does not match stored value");
                throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
            }
        }
    }
}

注册到ProciderManager中

import com.maruifu.framework.security.handle.JwtAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
/**
 * spring security配置
 *
 * @author maruifu
 */
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig1 extends WebSecurityConfigurerAdapter {
    /**
     * 自定义用户认证逻辑
     */
    @Autowired
    private UserDetailsService userDetailsService;
    /**
     * 解决 无法直接注入 AuthenticationManager
     * 重写 加盐后验证逻辑
     *
     * @return
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean(){
        JwtAuthenticationProvider provider=new JwtAuthenticationProvider();
        provider.setUserDetailsService(userDetailsService);
        ProviderManager manager=new ProviderManager(provider);
        return manager;
    }
    ......省略configure方法
}

以上就是SpringBoot Security密码加盐实例的详细内容,更多关于SpringBoot Security密码加盐的资料请关注我们其它相关文章!

(0)

相关推荐

  • SpringBoot整合Security权限控制登录首页

    目录 在 pom 文件中增加thymeleaf页面支持 application.yml 配置文件 login 页面 controller目录下跳转配置 UserController 在 pom 文件中增加thymeleaf页面支持 <!-- 引入页面模板 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thym

  • SpringBoot Security实现单点登出并清除所有token

    目录 需求 记录token 清除token 解决登出时长过长 需求 A.B.C 系统通过 sso 服务实现登录 A.B.C 系统分别获取 Atoken.Btoken.Ctoken 三个 token 其中某一个系统主动登出后,其他两个系统也登出 至此全部 Atoken.Btoken.Ctoken 失效 记录token pom 文件引入依赖 Redis数据库依赖 hutool:用于解析token <dependency> <groupId>org.springframework.boo

  • SpringBoot 整合Security权限控制的初步配置

    正文 在源码目录下新建 config 目录, 在该目录下新建 WebSecurityConfig 类文件 /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding co

  • SpringBoot security安全认证登录的实现方法

    目录 前言 一.登录时序图 二.配置与代码 1.引入库 2.代码文件 参考文档 前言 本文章主要从spring security安全认证登录内部调用流程来流程分析登录过程. 一.登录时序图 时序原图 二.配置与代码 1.引入库 pom.xml: <!-- Spring框架基本的核心工具 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-contex

  • SpringBoot整合SpringSecurity实现JWT认证的项目实践

    目录 前言 1.创建SpringBoot工程 2.导入SpringSecurity与JWT的相关依赖 3.定义SpringSecurity需要的基础处理类 4. 构建JWT token工具类 5.实现token验证的过滤器 6. SpringSecurity的关键配置 7. 编写Controller进行测试 前言 微服务架构,前后端分离目前已成为互联网项目开发的业界标准,其核心思想就是前端(APP.小程序.H5页面等)通过调用后端的API接口,提交及返回JSON数据进行交互.在前后端分离项目中,

  • SpringBoot整合Spring Security过滤器链加载执行流程源码分析(最新推荐)

    目录 1.引言 2.Spring Security过滤器链加载 2.1.注册名为 springSecurityFilterChain的过滤器 3.查看 DelegatingFilterProxy类 4.查看 FilterChainProxy类 4.1 查看 doFilterInternal方法 4.2 查看 getFilters方法 5 查看 SecurityFilterChain接口 6. 查看 SpringBootWebSecurityConfiguration类 总结: 1.引言 在 Sp

  • Springboot详解整合SpringSecurity实现全过程

    目录 使用Basic认证模式 使用form表形式登录 实现权限控制 自定义登录页面 结合数据库实现RBAC权限模型权限控制 java代码 动态绑定数据库所有权限 使用Basic认证模式 1.maven依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.

  • SpringBoot Security密码加盐实例

    目录 修改加密和验证方法 自定义 DaoAuthenticationProvider 注册到ProciderManager中 修改加密和验证方法 /** * 生成BCryptPasswordEncoder密码 * * @param password 密码 * @param salt 盐值 * @return 加密字符串 */ public static String encryptPassword(String password,String salt) { BCryptPasswordEnco

  • 关于python中密码加盐的学习体会小结

    给密码加密是什么:用户注册的密码一般网站管理人员会利用md5方法加密,这种加密方法的好处是它是单向加密的,也就是说,你只有在提前知道某一串密码对应的md5加密码,才能反推出密码是多少,虽然有极小的几率可能造成两个密码加密之后的值相等(这种现象称为碰撞),不过基本上不用担心,因为概率是极低的.在常用的hashlib模块里还有sha1()等方法,它的本质和md5是一致的,只是产生的结果是160 bit字节,通常用一个40位的16进制字符串表示.而md5是最常见的加密算法,生成速度很快,生成结果是固定

  • Flask框架中密码的加盐哈希加密和验证功能的用法详解

    密码加密简介 密码存储的主要形式: 明文存储:肉眼就可以识别,没有任何安全性. 加密存储:通过一定的变换形式,使得密码原文不易被识别. 密码加密的几类方式: 明文转码加密:BASE64, 7BIT等,这种方式只是个障眼法,不是真正的加密. 对称算法加密:DES, RSA等. 签名算法加密:也可以理解为单向哈希加密,比如MD5, SHA1等.加密算法固定,容 易被暴力破解.如果密码相同,得到的哈希值是一样的. 加盐哈希加密:加密时混入一段"随机"字符串(盐值)再进行哈希加密.即使密码相同

  • Spring Security 密码验证动态加盐的验证处理方法

    本文个人博客地址:https://www.leafage.top/posts/detail/21697I2R 最近几天在改造项目,需要将gateway整合security在一起进行认证和鉴权,之前gateway和auth是两个服务,auth是shiro写的一个,一个filter和一个配置,内容很简单,生成token,验证token,没有其他的安全检查,然后让对项目进行重构. 先是要整合gateway和shiro,然而因为gateway是webflux,而shiro-spring是webmvc,所

  • Springboot整合Shiro之加盐MD5加密的方法

    1.自定义realm,在Shiro的配置类中加入以下bean /** * 身份认证 realm */ @Bean public MyShiroRealm myShiroRealm(){ MyShiroRealm myShiroRealm = new MyShiroRealm(); System.out.println("myShiroRealm 注入成功"); return myShiroRealm; } 2.重写方法 // 身份认证 @Override protected Authe

  • SpringBoot使用jasypt加解密密码的实现方法(二)

    在我们的服务中不可避免的需要使用到一些秘钥(数据库.redis等) 开发和测试环境还好,但生产如果采用明文配置讲会有安全问题,jasypt是一个通用的加解密库,我们可以使用它. <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</ve

  • SpringBoot使用jasypt加解密密码的实现方法

    jasypt是一个通用的加解密库,我们可以使用它在配置文件中对数据库密码进行加密,以确保其安全性. 1.注入依赖 <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> 2.

  • SpringBoot配置文件的加载位置实例详解

    springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正常运行.在一些特殊的情况下,我们需要做修改一些配置,或者需要有自己的配置属性. SpringBoot启动会扫描以下位置的application.yml或者 application.properties文件作为SpringBoot的默认配置文件. -file:./config/    -file:./

  • SpringBoot Shiro配置自定义密码加密器代码实例

    shiro主要有三大功能模块: 1. Subject:主体,一般指用户. 2. SecurityManager:安全管理器,管理所有Subject,可以配合内部安全组件.(类似于SpringMVC中的DispatcherServlet) 3. Realms:用于进行权限信息的验证,一般需要自己实现. 细分功能 1. Authentication:身份认证/登录(账号密码验证). 2. Authorization:授权,即角色或者权限验证. 3. Session Manager:会话管理,用户登录

  • Springboot整合Shiro的代码实例

    这篇文章主要介绍了Springboot整合Shiro的代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.导入依赖 <!--shiro--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</versio

随机推荐