BCrypt加密方式

Bcrypt简介: bcrypt是一种跨平台的文件加密工具。bcrypt 使用的是布鲁斯•施内尔在1993年发布的 Blowfish 加密算法。由它加密的文件可在所有支持的操作系统和处理器上进行转移。它的口令必须是8至56个字符,并将在内部被转化为448位的密钥。简单的说,Bcrypt就是一款加密工具,可以比较方便地实现数据的加密工作。

第一种方法:Spring Security 提供了BCryptPasswordEncoder类,实现Spring的PasswordEncoder接口使用BCrypt强哈希方法来加密密码。 BCrypt强哈希方法 每次加密的结果都不一样。

1.引入依赖

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

2、添加配置类
我们在添加了spring security依赖后,所有的地址都被spring security所控制了,我们目前只是需要用到BCrypt密码加密的部分,所以我们要添加一个配置类,配置为所有地址都可以匿名访问。

package com.gx.util;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * 安全配置类
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    //authorizeRequests所有security全注解配置实现的开端,表示开始说明需要的权限
    //需要的权限分两部分,第一部分是拦截的路径,第二部分访问该路径需要的权限
    //antMatchers表示拦截什么路径,permitAll任何权限都可以访问,直接放行所有。
    //anyRequest()任何的请求,authenticated认证后才能访问
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();
    }
}

3、在SpringBoot引导类中,配置bean

@Bean
    public BCryptPasswordEncoder bcryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

4、密码加密、密码解密校验

package com.gx.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/bcrypt")
public class BCryptController {


    @Autowired
    BCryptPasswordEncoder encoder;

    private String str="";

    //加密
    @RequestMapping("/test")
    @ResponseBody
    public  String  test(){
      String password = encoder.encode("lyj123456");
      this.str = password;
       return password;
    }

    //解密
    @RequestMapping("/test2")
    @ResponseBody
    public String test2() {
        //encoder.matches(原密码,加密后的密码)
        if (encoder.matches("lyj123456", str)) {
           return  "成功 = "  + str;
        } else {
            return "失败 = " + str;

        }
    }

}

第二种方法:

1.引入依赖

          <dependency>
            <groupId>org.mindrot</groupId>
            <artifactId>jbcrypt</artifactId>
            <version>0.4</version>
        </dependency>

2、加密、解密

package com.gx.controller;

import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/jiayan")
public class JiaYanController {

    @RequestMapping("/text")
    @ResponseBody
    public String text(){
        //密码加密      BCrypt.gensalt():生成随机的盐
        String c= BCrypt.hashpw( "lyj123123",BCrypt.gensalt());
        //解密校验密码   BCrypt.checkpw(用户输入的密码 ,加密后的密码)
        boolean b = BCrypt.checkpw("lyj123123", c);
        return c+"   =  "+b;
    }


}


版权声明:本文为weixin_44548843原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。