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 */ 019package org.tynamo.shiro.extension.realm.text; 020 021import org.apache.shiro.authc.*; 022import org.apache.shiro.cache.CacheManager; 023import org.apache.shiro.realm.text.PropertiesRealm; 024 025/** 026 * Fixes some bugs with {@link org.apache.shiro.realm.text.PropertiesRealm} 027 * 028 */ 029public 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 * 079 * @see org.apache.shiro.realm.AuthorizingRealm#afterCacheManagerSet() 080 */ 081 @Override 082 protected void afterCacheManagerSet() 083 { 084 if (created) 085 { 086 super.afterCacheManagerSet(); 087 } else 088 { 089 setAuthorizationCache(null); 090 } 091 } 092}