public ApiKeyRealm(String name, CacheManager cacheManager, AuthIdentityReader<ApiKey> authIdentityReader, PermissionReader permissionReader, @Nullable String anonymousId) { super(null, AnonymousCredentialsMatcher.anonymousOrMatchUsing(new SimpleCredentialsMatcher())); _authIdentityReader = checkNotNull(authIdentityReader, "authIdentityReader"); _permissionReader = checkNotNull(permissionReader, "permissionReader"); _anonymousId = anonymousId; setName(checkNotNull(name, "name")); setAuthenticationTokenClass(ApiKeyAuthenticationToken.class); setPermissionResolver(permissionReader.getPermissionResolver()); setRolePermissionResolver(createRolePermissionResolver()); setCacheManager(prepareCacheManager(cacheManager)); setAuthenticationCachingEnabled(true); setAuthorizationCachingEnabled(true); // By default Shiro calls clearCache() for each user when they are logged out in order to prevent stale // credentials from being cached. However, if the cache manager implements InvalidatingCacheManager then it has // its own internal listeners that will invalidate the cache on any updates, making this behavior unnecessarily // expensive. _clearCaches = cacheManager != null && !(cacheManager instanceof InvalidatableCacheManager); _log.debug("Clearing of caches for realm {} is {}", name, _clearCaches ? "enabled" : "disabled"); }
/** * 凭证匹配器 * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 * 所以我们需要修改下doGetAuthenticationInfo中的代码; * ) * @return */ @Bean public SimpleCredentialsMatcher /*HashedCredentialsMatcher*/ hashedCredentialsMatcher(){ //1: // HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); // hashedCredentialsMatcher.setHashAlgorithmName("md5");//MD5算法; // hashedCredentialsMatcher.setHashIterations(2);//散列的次数 // return hashedCredentialsMatcher; //2:或: SimpleCredentialsMatcher simpleCredentialsMatcher= new SimpleCredentialsMatcher(){ @Override public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) { // // UsernamePasswordToken upToken = (UsernamePasswordToken) token; // //将用户在页面输入的原始密码加密 param : 1.用户页面填写的密码, 加密的盐 // //String pwd = Encrypt.md5(upToken.getPassword().toString(), upToken.getUsername()); // String pwd = md5(new String(upToken.getPassword()), upToken.getUsername()); // //3取出数据库加密的密码 // Object dbPwd = info.getCredentials(); //从AuthRealm doGetAuthenticationInfo传入的密码,数据库查询的密码. // // return this.equals(pwd,dbPwd); return true; } }; return simpleCredentialsMatcher; }
public AuthenticatingRealm() { this(null, new SimpleCredentialsMatcher()); }
public AuthenticatingRealm(CacheManager cacheManager) { this(cacheManager, new SimpleCredentialsMatcher()); }
public ShiroRealm() { super( new MemoryConstrainedCacheManager(), new SimpleCredentialsMatcher() ); }