shiro 加密登陸 密碼加鹽處理

密碼加密登陸是爲了提升系統安全性,即便是管理員查看數據庫也得不到密碼web

 

使用shiro能夠很輕鬆的完成加密及登陸操做算法

1:加密工具數據庫

此工具用於註冊時對密碼進行加密apache

public static final String md5(String password, String salt){
    //加密方式
    String hashAlgorithmName = "MD5";
    //鹽:爲了即便相同的密碼不一樣的鹽加密後的結果也不一樣
    ByteSource byteSalt = ByteSource.Util.bytes(salt);
    //密碼
    Object source = password;
    //加密次數
    int hashIterations = 1024;
    SimpleHash result = new SimpleHash(hashAlgorithmName, source, byteSalt, hashIterations);
    return result.toString();
}

測試一下安全

public static void main(String[] args) {
    String password = md5("123456", "WHLH");
    System.out.println(password);
    //加密後的結果
    //3bcbb857c763d1429a24959cb8de2593
}

2:使用shiro登陸ide

Realm類工具

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {
    UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;
    String username = token.getUsername();
    //查詢用戶信息
    User user=userService.findByUsername(username);
    //取出鹽並編碼
    ByteSource salt = ByteSource.Util.bytes(user.getSalt());
    return new SimpleAuthenticationInfo(username, user.getPassword(),salt, getName());
}

3:修改自定義realm配置:加密算法和加密次數要和加密工具參數保持一致測試

<bean id="myRealm" class="cn.jaffreyen.web.shiro.MyRealm">
    <property name="credentialsMatcher">
        <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
            <!-- 加密算法 -->
            <property name="hashAlgorithmName" value="MD5"></property>
            <!-- 加密次數 -->
            <property name="hashIterations" value="1024"></property>
        </bean>
    </property>
</bean>

數據庫編碼

完成加密

相關文章
相關標籤/搜索