1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.realm.text;
20
21 import org.apache.shiro.config.Ini;
22 import org.apache.shiro.util.CollectionUtils;
23 import org.apache.shiro.util.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class IniRealm extends TextConfigurationRealm {
46
47 public static final String USERS_SECTION_NAME = "users";
48 public static final String ROLES_SECTION_NAME = "roles";
49
50 private static transient final Logger log = LoggerFactory.getLogger(IniRealm.class);
51
52 private String resourcePath;
53 private Ini ini;
54
55 public IniRealm() {
56 super();
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public IniRealm(Ini ini) {
73 this();
74 processDefinitions(ini);
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public IniRealm(String resourcePath) {
92 this();
93 Ini ini = Ini.fromResourcePath(resourcePath);
94 this.ini = ini;
95 this.resourcePath = resourcePath;
96 processDefinitions(ini);
97 }
98
99 public String getResourcePath() {
100 return resourcePath;
101 }
102
103 public void setResourcePath(String resourcePath) {
104 this.resourcePath = resourcePath;
105 }
106
107
108
109
110
111
112
113 public Ini getIni() {
114 return ini;
115 }
116
117
118
119
120
121
122
123 public void setIni(Ini ini) {
124 this.ini = ini;
125 }
126
127 @Override
128 protected void onInit() {
129 super.onInit();
130
131
132
133
134 Ini ini = getIni();
135 String resourcePath = getResourcePath();
136
137 if (!CollectionUtils.isEmpty(this.users) || !CollectionUtils.isEmpty(this.roles)) {
138 if (!CollectionUtils.isEmpty(ini)) {
139 log.warn("Users or Roles are already populated. Configured Ini instance will be ignored.");
140 }
141 if (StringUtils.hasText(resourcePath)) {
142 log.warn("Users or Roles are already populated. resourcePath '{}' will be ignored.", resourcePath);
143 }
144
145 log.debug("Instance is already populated with users or roles. No additional user/role population " +
146 "will be performed.");
147 return;
148 }
149
150 if (CollectionUtils.isEmpty(ini)) {
151 log.debug("No INI instance configuration present. Checking resourcePath...");
152
153 if (StringUtils.hasText(resourcePath)) {
154 log.debug("Resource path {} defined. Creating INI instance.", resourcePath);
155 ini = Ini.fromResourcePath(resourcePath);
156 if (!CollectionUtils.isEmpty(ini)) {
157 setIni(ini);
158 }
159 }
160 }
161
162 if (CollectionUtils.isEmpty(ini)) {
163 String msg = "Ini instance and/or resourcePath resulted in null or empty Ini configuration. Cannot " +
164 "load account data.";
165 throw new IllegalStateException(msg);
166 }
167
168 processDefinitions(ini);
169 }
170
171 private void processDefinitions(Ini ini) {
172 if (CollectionUtils.isEmpty(ini)) {
173 log.warn("{} defined, but the ini instance is null or empty.", getClass().getSimpleName());
174 return;
175 }
176
177 Ini.Section rolesSection = ini.getSection(ROLES_SECTION_NAME);
178 if (!CollectionUtils.isEmpty(rolesSection)) {
179 log.debug("Discovered the [{}] section. Processing...", ROLES_SECTION_NAME);
180 processRoleDefinitions(rolesSection);
181 }
182
183 Ini.Section usersSection = ini.getSection(USERS_SECTION_NAME);
184 if (!CollectionUtils.isEmpty(usersSection)) {
185 log.debug("Discovered the [{}] section. Processing...", USERS_SECTION_NAME);
186 processUserDefinitions(usersSection);
187 } else {
188 log.info("{} defined, but there is no [{}] section defined. This realm will not be populated with any " +
189 "users and it is assumed that they will be populated programatically. Users must be defined " +
190 "for this Realm instance to be useful.", getClass().getSimpleName(), USERS_SECTION_NAME);
191 }
192 }
193 }