• Spring Security密码加密工具类


    1. 添加Spring Security依赖

    <!--spring-security权限认证框架-->
    
    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    2. 注入BCryptPasswordEncoder工具类Bean

    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    
    @EnableSwagger2
    @SpringBootApplication
    @MapperScan("com.hundsun.one.mapper")
    public class OneApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(OneApplication.class, args);
        }
    
        /**
         * Spring Security权限认证框架 封装的密码加密工具类
         *
         * @return
         */
        @Bean
        public BCryptPasswordEncoder getBCryptPasswordEncoder(){
            return new BCryptPasswordEncoder();
        }
    }

    3. 生成密文及密文验证

    @SpringBootTest
    class OneApplicationTests {
    
        // spring security权限认证框架 封装的密码加密工具类
        @Autowired
        private BCryptPasswordEncoder pwdEncoder;
    
        @Test
        void xxx(){
            // 生成密文
            String encode = pwdEncoder.encode("123456");
            System.out.println("encode = " + encode);
            // 生成密文
            String encode1 = pwdEncoder.encode("123456");
            System.out.println("encode1 = " + encode1);
            // 验证密文
            boolean b = pwdEncoder.matches("123456", encode);
            System.out.println("b = " + b);
            // 验证密文
            boolean b1 = pwdEncoder.matches("123456", encode1);
            System.out.println("b1 = " + b1);
        }
    }

    4. 控制台打印结果

    // 两次输入的明文都是123456,生成的密文却是不相同的
    // 两次密文验证的结果都是true
    
    encode = $2a$10$tKbmm6laeGZ2mDcn3EdP0OUy3OlWbN15.sJnFpcUSG1yoBYLGUfye
    encode1 = $2a$10$51KayjetdJpMsWoomauz0OAp8ipaO6UOeSHCm01CW1I3gIkYyCr/.
    b = true
    b1 = true
  • 相关阅读:
    164 Maximum Gap 最大间距
    162 Find Peak Element 寻找峰值
    160 Intersection of Two Linked Lists 相交链表
    155 Min Stack 最小栈
    154 Find Minimum in Rotated Sorted Array II
    153 Find Minimum in Rotated Sorted Array 旋转数组的最小值
    152 Maximum Product Subarray 乘积最大子序列
    151 Reverse Words in a String 翻转字符串里的单词
    bzoj3994: [SDOI2015]约数个数和
    bzoj 4590: [Shoi2015]自动刷题机
  • 原文地址:https://www.cnblogs.com/w1440199392/p/15201246.html
Copyright © 2020-2023  润新知