Return new SimpleAuthenticationInfo(username,md5, getName()) after successful authentication in the realm;

Authentication succeeded Successfully get info down execute to assertCredentialsMatch (token,info);

 public abstract class AuthenticatingRealm extends CachingRealm implements Initializable {
 
protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
    CredentialsMatcher cm = getCredentialsMatcher();
    if (cm != null) {
        if (!cm.doCredentialsMatch(token, info)) {
            //not successful - throw an exception to indicate this:
            String msg = "Submitted credentials for token [" + token + "] did not match the expected credentials.";
            throw new IncorrectCredentialsException(msg);
        }
    } else {
        throw new AuthenticationException("A CredentialsMatcher must be configured in order to verify " +
                "credentials during authentication.  If you do not wish for credentials to be examined, you " +
                "can configure an " + AllowAllCredentialsMatcher.class.getName() + " instance.");
    }
}
 
}
 
Copy the code

Cm.docredentialsmatch (Token, info) The default password comparator can be used here or a custom password comparison can be used

What I’m doing here is custom password verification and it’s very simple it’s just inheriting from the class in the figure above and I’m out of date and I’m implementing the doCredentialsMatch method

public class Md5HashCredentialsMatcher extends SimpleCredentialsMatcher { @Override public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) { UsernamePasswordToken usertoken = (UsernamePasswordToken) token; // Note that token.getPassword() gets a char[], not toString(), the underlying implementation is not a direct string as we would like. You can only strongly convert Object tokenCredentials = encrypt.md5 (string.valueof (userToken.getPassword()), userToken.getUsername()); Object accountCredentials = getCredentials(info); Return equals(tokenCredentials, accountCredentials) return true if the encrypted password is the same as the encrypted password. Return false if the encrypted password is not the same as the encrypted password. }}Copy the code

Then configure it in spring-shiro.xml

<bean id="myRealm" class="spring.shiro.realm.MyRealm"> <property name="credentialsMatcher" ref="md5Matcher"/> <! --<property name="credentialsMatcher" ref="md5hash"/>--> </bean> <bean id="md5hash" class="spring.shiro.matcher.Md5HashCredentialsMatcher"/> <bean id="md5Matcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <! Property name="hashAlgorithmName" value="MD5"/> <! --> <property name="hashIterations" value="1024"/> </bean> <bean id="sha1Matcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <! -- Name of encryption algorithm --> <property name="hashAlgorithmName" value="SHA1"/> <! "> <property name="hashIterations" value="1024"/> </bean>Copy the code