001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.tynamo.shiro.extension.realm.text;
020
021 import org.apache.shiro.authc.*;
022 import org.apache.shiro.cache.CacheManager;
023 import org.apache.shiro.realm.text.PropertiesRealm;
024
025 /**
026 * Fixes some bugs with {@link org.apache.shiro.realm.text.PropertiesRealm}
027 *
028 */
029 public class ExtendedPropertiesRealm extends PropertiesRealm
030 {
031
032 boolean created;
033
034 public ExtendedPropertiesRealm(String resourcePath)
035 {
036 super();
037 setResourcePath(resourcePath);
038 onInit();
039 }
040
041 /**
042 * Eliminates the error generating NullPointerException,
043 * when trying to register for non-existent account.
044 * <p/>
045 *
046 * @see org.apache.shiro.realm.SimpleAccountRealm#doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken)
047 */
048 @Override
049 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException
050 {
051
052 UsernamePasswordToken upToken = (UsernamePasswordToken) token;
053 if (!accountExists(upToken.getUsername()))
054 {
055 throw new UnknownAccountException("Unknown account" + upToken.getUsername());
056 }
057
058 return super.doGetAuthenticationInfo(token);
059 }
060
061 @Override
062 public void setCacheManager(CacheManager authzInfoCacheManager)
063 {
064 if (created && getCacheManager() != null)
065 {
066 return;
067 }
068 super.setCacheManager(authzInfoCacheManager);
069 }
070
071
072 /**
073 * Remove initialization after installing cacheManager.
074 * This created problems of premature initialization,
075 * when not specified the name of realm, respectively,
076 * are generated nekkorektnye account with the name of the default realm,
077 * which then changed to the name specified in the config.
078 * <p/>
079 * <b>RU:</b>
080 * �£�±�¸Ñ€�°�µ�¼ �¸�½�¸Ñ†�¸�°�»�¸�·�°Ñ†�¸ÑŽ �¿�¾Ñ��»�µ уÑ�Ñ‚�°�½�¾�²�º�¸ cacheManager.
081 * Ñ�Ñ‚�¾ Ñ��¾�·�´�°�²�°�»�¾ �¿Ñ€�¾�±�»�µ�¼�¼Ñƒ �¿Ñ€�µ�¶�´�µ�²Ñ€�µ�¼�µ�½�½�¾�¹ �¸�½�¸Ñ†�¸�°�»�¸�·�°Ñ†�¸�¸,
082 * �º�¾�³�´�° �µÑ‰�µ �½�µ �·�°�´�°�½�¾ �¸�¼Ñ� realm, Ñ��¾�¾Ñ‚�²�µÑ‚Ñ�Ñ‚�²�µ�½�½�¾ Ñ�Ñ‚�¾ �¿�¾Ñ€�¾�¶�´�°�»�¾
083 * �½�µ�º�º�¾Ñ€�µ�ºÑ‚�½Ñ‹�µ �°�º�º�°Ñƒ�½Ñ‚Ñ‹ Ñ� �¸�¼�µ�½�µ�¼ realm �¿�¾ у�¼�¾�»Ñ‡�°�½�¸�µ, �º�¾Ñ‚�¾Ñ€�¾�µ �¿�¾Ñ‚�¾�¼ �¼�µ�½Ñ��»�¾Ñ�ÑŒ
084 * �½�° �¸�¼Ñ� �·�°�´�°�½�½�¾�µ �² �º�¾�½Ñ„�¸�³�µ.
085 *
086 * @see org.apache.shiro.realm.AuthorizingRealm#afterCacheManagerSet()
087 */
088 @Override
089 protected void afterCacheManagerSet()
090 {
091 if (created)
092 {
093 super.afterCacheManagerSet();
094 } else
095 {
096 setAuthorizationCache(null);
097 }
098 }
099
100 private void onCreated()
101 {
102 created = true;
103 }
104 }