一、BCrypt简介
Bcrypt简介: bcrypt是一种跨平台的文件加密工具。bcrypt 使用的是布鲁斯·施内尔在1993年发布的 Blowfish 加密算法。由它加密的文件可在所有支持的操作系统和处理器上进行转移。它的口令必须是8至56个字符,并将在内部被转化为448位的密钥。简单的说,Bcrypt就是一款加密工具,可以比较方便地实现数据的加密工作。
二、BCrypt简单使用
@RequestMapping("/add")
public Result add(@RequestBody TbSeller seller){
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String password = passwordEncoder.encode(seller.getPassword());
seller.setPassword(password);
try {
sellerService.add(seller);
return new Result(true, "增加成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false, "增加失败");
}
}
三、BCrypt与Spring Security结合使用
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 不登录可以访问 -->
<!--<http pattern="/favicon.ico" security="none" ></http>-->
<http pattern="/*.html" security="none" ></http>
<http pattern="/css/**" security="none" ></http>
<http pattern="/img/**" security="none" ></http>
<http pattern="/js/**" security="none" ></http>
<http pattern="/plugins/**" security="none" ></http>
<http pattern="/seller/add.do" security="none" ></http>
<!--配置页面的拦截规则 use-expressions是否启用SPEL表达式,默认true -->
<http use-expressions="false">
<!-- 如果不加use-expressions="false",则intercept-url中的access需要写成hasRole('ROLE_USER') -->
<!-- 当前用户必须有ROLE_USER的角色,才可以访问根目录及所有子目录的资源 -->
<intercept-url pattern="/**" access="ROLE_SELLER"/>
<!-- 开启表达登录功能 -->
<!--
参数说明:
login-processing-url : 配置登录提交的action,默认/login
login-page : 配置登录的页面
default-target-url : 登录成功后的访问页
authentication-failure-url : 登录失败的跳转页
username-parameter : 指定用户名<input>的name属性值,默认username
password-parameter : 指定密码<input>的name属性值,默认password
注意:页面上的form表单的method必须是post
-->
<form-login login-page="/shoplogin.html" default-target-url="/admin/index.html"
authentication-failure-url="/shoplogin.html" always-use-default-target="true"/>
<!-- 关闭csrf验证 -->
<csrf disabled="true"/>
<!--spring security默认拦截内置框架页,如iframe,需要如下配置取消拦截 -->
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
<!-- 退出
参数说明;
logout-url退出地址,默认/logout
logout-success-url退出成功的访问地址
-->
<logout/>
</http>
<!-- 认证管理器 -->
<authentication-manager>
<!-- 提供者 -->
<authentication-provider user-service-ref="userDetailService">
<password-encoder ref="bCryptPasswordEncoder"></password-encoder>
</authentication-provider>
</authentication-manager>
<beans:bean id="userDetailService" class="com.xxx.shop.service.UserDetailsServiceImpl">
<beans:property name="sellerService" ref="sellerService"></beans:property>
</beans:bean>
<!-- 引用dubbo 服务 -->
<dubbo:application name="xxx-shop-web" />
<dubbo:registry address="zookeeper://192.168.25.128:2181"/>
<!-- 通过接口去匹配dubbox上的实现类,并返回实现类,完成远程属性注入 -->
<dubbo:reference id="sellerService" interface="com.xxx.sellergoods.service.SellerService"></dubbo:reference>
<beans:bean id="bCryptPasswordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></beans:bean>
</beans:beans>