欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot整合Shiro之加鹽MD5加密的方法

 更新時(shí)間:2018年12月03日 11:00:34   作者:夢想周游全國的孩子  
這篇文章主要介紹了Springboot整合Shiro之加鹽MD5加密的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.自定義realm,在Shiro的配置類中加入以下bean

/**
  * 身份認(rèn)證 realm
  */
 @Bean
 public MyShiroRealm myShiroRealm(){
  MyShiroRealm myShiroRealm = new MyShiroRealm();
  System.out.println("myShiroRealm 注入成功");
  return myShiroRealm;
 }

2.重寫方法

// 身份認(rèn)證
 @Override
 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
  String username = (String) authenticationToken.getPrincipal();
  System.out.println("MyShiroRealm.....doGetAuthenticationInfo");
  UserInfo user=null;
  try {
   user = iUserInfoService.findByUsername(username);
  }catch (Exception e){
   e.printStackTrace();
  }
  if (user==null){
   return null;
  }
  // 進(jìn)行驗(yàn)證,將正確數(shù)據(jù)講給shiro處理
  SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
    user,
    user.getPassword(),
    ByteSource.Util.bytes(user.getCredentialsSalt()), // 加鹽后的密碼
    getName() // 指定當(dāng)前 Realm 的類名
  );

  // 返回給安全管理器,由 securityManager 比對密碼的正確性
  return authenticationInfo;
 }

需要注意的是SimpleAuthenticationInfo 類,我們需要把數(shù)據(jù)交給他,格式為(用戶,用戶密碼,鹽,當(dāng)前Realm的類名)

  // 進(jìn)行驗(yàn)證,將正確數(shù)據(jù)講給shiro處理
  SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
    user,
    user.getPassword(),
    ByteSource.Util.bytes(user.getCredentialsSalt()), // 加鹽后的密碼
    getName() // 指定當(dāng)前 Realm 的類名
  );

3.你還需要告訴shiro你是經(jīng)過加密的,在Config內(nèi)新建如下bean

@Bean
 public HashedCredentialsMatcher hashedCredentialsMatcher(){
  HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
  // 使用md5 算法進(jìn)行加密
  hashedCredentialsMatcher.setHashAlgorithmName("md5");
  // 設(shè)置散列次數(shù): 意為加密幾次
  hashedCredentialsMatcher.setHashIterations(2);

  return hashedCredentialsMatcher;
 }

并注冊:

 @Bean
 public MyShiroRealm myShiroRealm(){
  MyShiroRealm myShiroRealm = new MyShiroRealm();
  // 配置 加密 (在加密后,不配置的話會(huì)導(dǎo)致登陸密碼失敗)
  myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); //+++++++++++
  System.out.println("myShiroRealm 注入成功");
  return myShiroRealm;
 }

總結(jié)

以上所述是小編給大家介紹的Springboot整合Shiro之加鹽MD5加密的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論