How to stock and use a shiro's salt from database

前端 未结 4 715
余生分开走
余生分开走 2021-02-06 06:44

I use shiro in application for the authenticate. I use hashed password with a salt and I store them in my database like this :

    private User createUserWithHas         


        
4条回答
  •  离开以前
    2021-02-06 07:42

    Have you looked at PasswordMatcher / PasswordService?

    This already has all of the encoding/decoding/compare logic built-in. To use it:

    Storing password in database:

    PasswordService service = new DefaultPasswordService(); // or use injection or shiro.ini to populate this
    
    private User createUserWithHashedPassword(String inName, String inFirstName, String inLastName, String inPassword){
    
      String hashedPasswordBase64 = service.encryptPassword(inPassword);
    
      return new User(inName,inFirstName,inLastName,hashedPasswordBase64,strSalt);
    }
    

    Then you can simply use PasswordMatcher as the matcher in your realm.

    realm.setCredentialsMatcher(new PasswordMatcher());
    

    or in shiro.ini:

    matcher = org.apache.shiro.authc.credential.PasswordMatcher
    realm.credentialsMatcher = $matcher
    

提交回复
热议问题