| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>TextConfigurationRealm.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: Jar Bundle</a> > <a href="../index.html" class="el_bundle">shiro-core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.realm.text</a> > <span class="el_source">TextConfigurationRealm.java</span></div><h1>TextConfigurationRealm.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.realm.text; |
| |
| import org.apache.shiro.authc.SimpleAccount; |
| import org.apache.shiro.authz.Permission; |
| import org.apache.shiro.authz.SimpleRole; |
| import org.apache.shiro.config.ConfigurationException; |
| import org.apache.shiro.realm.SimpleAccountRealm; |
| import org.apache.shiro.util.PermissionUtils; |
| import org.apache.shiro.util.StringUtils; |
| |
| import java.text.ParseException; |
| import java.util.*; |
| |
| |
| /** |
| * A SimpleAccountRealm that enables text-based configuration of the initial User, Role, and Permission objects |
| * created at startup. |
| * <p/> |
| * Each User account definition specifies the username, password, and roles for a user. Each Role definition |
| * specifies a name and an optional collection of assigned Permissions. Users can be assigned Roles, and Roles can be |
| * assigned Permissions. By transitive association, each User 'has' all of their Role's Permissions. |
| * <p/> |
| * User and user-to-role definitions are specified via the {@link #setUserDefinitions} method and |
| * Role-to-permission definitions are specified via the {@link #setRoleDefinitions} method. |
| * |
| * @since 0.9 |
| */ |
| public class TextConfigurationRealm extends SimpleAccountRealm { |
| |
| //TODO - complete JavaDoc |
| |
| private volatile String userDefinitions; |
| private volatile String roleDefinitions; |
| |
| public TextConfigurationRealm() { |
| <span class="fc" id="L54"> super();</span> |
| <span class="fc" id="L55"> }</span> |
| |
| /** |
| * Will call 'processDefinitions' on startup. |
| * |
| * @since 1.2 |
| * @see <a href="https://issues.apache.org/jira/browse/SHIRO-223">SHIRO-223</a> |
| */ |
| @Override |
| protected void onInit() { |
| <span class="fc" id="L65"> super.onInit();</span> |
| <span class="fc" id="L66"> processDefinitions();</span> |
| <span class="fc" id="L67"> }</span> |
| |
| public String getUserDefinitions() { |
| <span class="fc" id="L70"> return userDefinitions;</span> |
| } |
| |
| /** |
| * <p>Sets a newline (\n) delimited String that defines user-to-password-and-role(s) key/value pairs according |
| * to the following format: |
| * <p/> |
| * <p><code><em>username</em> = <em>password</em>, role1, role2,...</code></p> |
| * <p/> |
| * <p>Here are some examples of what these lines might look like:</p> |
| * <p/> |
| * <p><code>root = <em>reallyHardToGuessPassword</em>, administrator<br/> |
| * jsmith = <em>jsmithsPassword</em>, manager, engineer, employee<br/> |
| * abrown = <em>abrownsPassword</em>, qa, employee<br/> |
| * djones = <em>djonesPassword</em>, qa, contractor<br/> |
| * guest = <em>guestPassword</em></code></p> |
| * |
| * @param userDefinitions the user definitions to be parsed and converted to Map.Entry elements |
| */ |
| public void setUserDefinitions(String userDefinitions) { |
| <span class="fc" id="L90"> this.userDefinitions = userDefinitions;</span> |
| <span class="fc" id="L91"> }</span> |
| |
| public String getRoleDefinitions() { |
| <span class="fc" id="L94"> return roleDefinitions;</span> |
| } |
| |
| /** |
| * Sets a newline (\n) delimited String that defines role-to-permission definitions. |
| * <p/> |
| * <p>Each line within the string must define a role-to-permission(s) key/value mapping with the |
| * equals character signifies the key/value separation, like so:</p> |
| * <p/> |
| * <p><code><em>rolename</em> = <em>permissionDefinition1</em>, <em>permissionDefinition2</em>, ...</code></p> |
| * <p/> |
| * <p>where <em>permissionDefinition</em> is an arbitrary String, but must people will want to use |
| * Strings that conform to the {@link org.apache.shiro.authz.permission.WildcardPermission WildcardPermission} |
| * format for ease of use and flexibility. Note that if an individual <em>permissionDefnition</em> needs to |
| * be internally comma-delimited (e.g. <code>printer:5thFloor:print,info</code>), you will need to surround that |
| * definition with double quotes (&quot;) to avoid parsing errors (e.g. |
| * <code>&quot;printer:5thFloor:print,info&quot;</code>). |
| * <p/> |
| * <p><b>NOTE:</b> if you have roles that don't require permission associations, don't include them in this |
| * definition - just defining the role name in the {@link #setUserDefinitions(String) userDefinitions} is |
| * enough to create the role if it does not yet exist. This property is really only for configuring realms that |
| * have one or more assigned Permission. |
| * |
| * @param roleDefinitions the role definitions to be parsed at initialization |
| */ |
| public void setRoleDefinitions(String roleDefinitions) { |
| <span class="fc" id="L120"> this.roleDefinitions = roleDefinitions;</span> |
| <span class="fc" id="L121"> }</span> |
| |
| protected void processDefinitions() { |
| try { |
| <span class="fc" id="L125"> processRoleDefinitions();</span> |
| <span class="fc" id="L126"> processUserDefinitions();</span> |
| <span class="nc" id="L127"> } catch (ParseException e) {</span> |
| <span class="nc" id="L128"> String msg = "Unable to parse user and/or role definitions.";</span> |
| <span class="nc" id="L129"> throw new ConfigurationException(msg, e);</span> |
| <span class="fc" id="L130"> }</span> |
| <span class="fc" id="L131"> }</span> |
| |
| protected void processRoleDefinitions() throws ParseException { |
| <span class="fc" id="L134"> String roleDefinitions = getRoleDefinitions();</span> |
| <span class="fc bfc" id="L135" title="All 2 branches covered."> if (roleDefinitions == null) {</span> |
| <span class="fc" id="L136"> return;</span> |
| } |
| <span class="fc" id="L138"> Map<String, String> roleDefs = toMap(toLines(roleDefinitions));</span> |
| <span class="fc" id="L139"> processRoleDefinitions(roleDefs);</span> |
| <span class="fc" id="L140"> }</span> |
| |
| protected void processRoleDefinitions(Map<String, String> roleDefs) { |
| <span class="pc bpc" id="L143" title="2 of 4 branches missed."> if (roleDefs == null || roleDefs.isEmpty()) {</span> |
| <span class="nc" id="L144"> return;</span> |
| } |
| <span class="fc bfc" id="L146" title="All 2 branches covered."> for (String rolename : roleDefs.keySet()) {</span> |
| <span class="fc" id="L147"> String value = roleDefs.get(rolename);</span> |
| |
| <span class="fc" id="L149"> SimpleRole role = getRole(rolename);</span> |
| <span class="fc bfc" id="L150" title="All 2 branches covered."> if (role == null) {</span> |
| <span class="fc" id="L151"> role = new SimpleRole(rolename);</span> |
| <span class="fc" id="L152"> add(role);</span> |
| } |
| |
| <span class="fc" id="L155"> Set<Permission> permissions = PermissionUtils.resolveDelimitedPermissions(value, getPermissionResolver());</span> |
| <span class="fc" id="L156"> role.setPermissions(permissions);</span> |
| <span class="fc" id="L157"> }</span> |
| <span class="fc" id="L158"> }</span> |
| |
| protected void processUserDefinitions() throws ParseException { |
| <span class="fc" id="L161"> String userDefinitions = getUserDefinitions();</span> |
| <span class="fc bfc" id="L162" title="All 2 branches covered."> if (userDefinitions == null) {</span> |
| <span class="fc" id="L163"> return;</span> |
| } |
| |
| <span class="fc" id="L166"> Map<String, String> userDefs = toMap(toLines(userDefinitions));</span> |
| |
| <span class="fc" id="L168"> processUserDefinitions(userDefs);</span> |
| <span class="fc" id="L169"> }</span> |
| |
| protected void processUserDefinitions(Map<String, String> userDefs) { |
| <span class="pc bpc" id="L172" title="2 of 4 branches missed."> if (userDefs == null || userDefs.isEmpty()) {</span> |
| <span class="nc" id="L173"> return;</span> |
| } |
| <span class="fc bfc" id="L175" title="All 2 branches covered."> for (String username : userDefs.keySet()) {</span> |
| |
| <span class="fc" id="L177"> String value = userDefs.get(username);</span> |
| |
| <span class="fc" id="L179"> String[] passwordAndRolesArray = StringUtils.split(value);</span> |
| |
| <span class="fc" id="L181"> String password = passwordAndRolesArray[0];</span> |
| |
| <span class="fc" id="L183"> SimpleAccount account = getUser(username);</span> |
| <span class="fc bfc" id="L184" title="All 2 branches covered."> if (account == null) {</span> |
| <span class="fc" id="L185"> account = new SimpleAccount(username, password, getName());</span> |
| <span class="fc" id="L186"> add(account);</span> |
| } |
| <span class="fc" id="L188"> account.setCredentials(password);</span> |
| |
| <span class="fc bfc" id="L190" title="All 2 branches covered."> if (passwordAndRolesArray.length > 1) {</span> |
| <span class="fc bfc" id="L191" title="All 2 branches covered."> for (int i = 1; i < passwordAndRolesArray.length; i++) {</span> |
| <span class="fc" id="L192"> String rolename = passwordAndRolesArray[i];</span> |
| <span class="fc" id="L193"> account.addRole(rolename);</span> |
| |
| <span class="fc" id="L195"> SimpleRole role = getRole(rolename);</span> |
| <span class="fc bfc" id="L196" title="All 2 branches covered."> if (role != null) {</span> |
| <span class="fc" id="L197"> account.addObjectPermissions(role.getPermissions());</span> |
| } |
| } |
| } else { |
| <span class="fc" id="L201"> account.setRoles(null);</span> |
| } |
| <span class="fc" id="L203"> }</span> |
| <span class="fc" id="L204"> }</span> |
| |
| protected static Set<String> toLines(String s) { |
| <span class="fc" id="L207"> LinkedHashSet<String> set = new LinkedHashSet<String>();</span> |
| <span class="fc" id="L208"> Scanner scanner = new Scanner(s);</span> |
| <span class="fc bfc" id="L209" title="All 2 branches covered."> while (scanner.hasNextLine()) {</span> |
| <span class="fc" id="L210"> set.add(scanner.nextLine());</span> |
| } |
| <span class="fc" id="L212"> return set;</span> |
| } |
| |
| protected static Map<String, String> toMap(Collection<String> keyValuePairs) throws ParseException { |
| <span class="pc bpc" id="L216" title="2 of 4 branches missed."> if (keyValuePairs == null || keyValuePairs.isEmpty()) {</span> |
| <span class="nc" id="L217"> return null;</span> |
| } |
| |
| <span class="fc" id="L220"> Map<String, String> pairs = new HashMap<String, String>();</span> |
| <span class="fc bfc" id="L221" title="All 2 branches covered."> for (String pairString : keyValuePairs) {</span> |
| <span class="fc" id="L222"> String[] pair = StringUtils.splitKeyValue(pairString);</span> |
| <span class="pc bpc" id="L223" title="1 of 2 branches missed."> if (pair != null) {</span> |
| <span class="fc" id="L224"> pairs.put(pair[0].trim(), pair[1].trim());</span> |
| } |
| <span class="fc" id="L226"> }</span> |
| |
| <span class="fc" id="L228"> return pairs;</span> |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |