blob: d2e22b8d588f26d9e66a2a578ddc9299503116ac [file] [log] [blame]
<?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>BootstrapDataPopulator.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 :: Samples :: Spring</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.samples.spring</a> &gt; <span class="el_source">BootstrapDataPopulator.java</span></div><h1>BootstrapDataPopulator.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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.samples.spring;
import javax.sql.DataSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.crypto.hash.Sha256Hash;
/**
* A data populator that creates a set of security tables and test data that can be used by the
* Shiro Spring sample application to demonstrate the use of the {@link org.apache.shiro.realm.jdbc.JdbcRealm}
* The tables created by this class follow the default table and column names that {@link org.apache.shiro.realm.jdbc.JdbcRealm} uses.
*
*/
<span class="nc" id="L37">public class BootstrapDataPopulator implements InitializingBean {</span>
private static final String CREATE_TABLES = &quot;create table users (\n&quot; +
&quot; username varchar(255) primary key,\n&quot; +
&quot; password varchar(255) not null\n&quot; +
&quot;);\n&quot; +
&quot;\n&quot; +
&quot;create table roles (\n&quot; +
&quot; role_name varchar(255) primary key\n&quot; +
&quot;);\n&quot; +
&quot;\n&quot; +
&quot;create table user_roles (\n&quot; +
&quot; username varchar(255) not null,\n&quot; +
&quot; role_name varchar(255) not null,\n&quot; +
&quot; constraint user_roles_uq unique ( username, role_name )\n&quot; +
&quot;);\n&quot; +
&quot;\n&quot; +
&quot;create table roles_permissions (\n&quot; +
&quot; role_name varchar(255) not null,\n&quot; +
&quot; permission varchar(255) not null,\n&quot; +
&quot; primary key (role_name, permission)\n&quot; +
&quot;);&quot;;
<span class="nc" id="L60"> private static final Logger log = LoggerFactory.getLogger(BootstrapDataPopulator.class);</span>
<span class="nc" id="L62"> protected DataSource dataSource = null;</span>
public void setDataSource(DataSource dataSource) {
<span class="nc" id="L65"> this.dataSource = dataSource;</span>
<span class="nc" id="L66"> }</span>
public void afterPropertiesSet() throws Exception {
//because we're using an in-memory hsqldb for the sample app, a new one will be created each time the
//app starts, so create the tables and insert the 2 sample users on bootstrap:
<span class="nc" id="L72"> JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);</span>
<span class="nc" id="L73"> jdbcTemplate.execute(CREATE_TABLES);</span>
//password is 'user1' SHA hashed and base64 encoded:
//The first argument to the hash constructor is the actual value to be hased. The 2nd is the
//salt. In this simple demo scenario, the username and the password are the same, but to clarify the
//distinction, you would see this in practice:
//new Sha256Hash( &lt;password&gt;, &lt;cryptographically strong randomly generated salt&gt; (not the username!) )
<span class="nc" id="L80"> String query = &quot;insert into users values ('user1', '&quot; + new Sha256Hash(&quot;user1&quot;, &quot;user1&quot;).toBase64() + &quot;' )&quot;;</span>
<span class="nc" id="L81"> jdbcTemplate.execute(query);</span>
<span class="nc" id="L82"> log.debug(&quot;Created user1.&quot;);</span>
//password is 'user2' SHA hashed and base64 encoded:
<span class="nc" id="L85"> query = &quot;insert into users values ( 'user2', '&quot; + new Sha256Hash(&quot;user2&quot;, &quot;user2&quot;).toBase64() + &quot;' )&quot;;</span>
<span class="nc" id="L86"> jdbcTemplate.execute(query);</span>
<span class="nc" id="L87"> log.debug(&quot;Created user2.&quot;);</span>
<span class="nc" id="L89"> query = &quot;insert into roles values ( 'role1' )&quot;;</span>
<span class="nc" id="L90"> jdbcTemplate.execute(query);</span>
<span class="nc" id="L91"> log.debug(&quot;Created role1&quot;);</span>
<span class="nc" id="L93"> query = &quot;insert into roles values ( 'role2' )&quot;;</span>
<span class="nc" id="L94"> jdbcTemplate.execute(query);</span>
<span class="nc" id="L95"> log.debug(&quot;Created role2&quot;);</span>
<span class="nc" id="L97"> query = &quot;insert into roles_permissions values ( 'role1', 'permission1')&quot;;</span>
<span class="nc" id="L98"> jdbcTemplate.execute(query);</span>
<span class="nc" id="L99"> log.debug(&quot;Created permission 1 for role 1&quot;);</span>
<span class="nc" id="L101"> query = &quot;insert into roles_permissions values ( 'role1', 'permission2')&quot;;</span>
<span class="nc" id="L102"> jdbcTemplate.execute(query);</span>
<span class="nc" id="L103"> log.debug(&quot;Created permission 2 for role 1&quot;);</span>
<span class="nc" id="L105"> query = &quot;insert into roles_permissions values ( 'role2', 'permission1')&quot;;</span>
<span class="nc" id="L106"> jdbcTemplate.execute(query);</span>
<span class="nc" id="L107"> log.debug(&quot;Created permission 1 for role 2&quot;);</span>
<span class="nc" id="L109"> query = &quot;insert into user_roles values ( 'user1', 'role1' )&quot;;</span>
<span class="nc" id="L110"> jdbcTemplate.execute(query);</span>
<span class="nc" id="L111"> query = &quot;insert into user_roles values ( 'user1', 'role2' )&quot;;</span>
<span class="nc" id="L112"> jdbcTemplate.execute(query);</span>
<span class="nc" id="L113"> log.debug(&quot;Assigned user1 roles role1 and role2&quot;);</span>
<span class="nc" id="L115"> query = &quot;insert into user_roles values ( 'user2', 'role2' )&quot;;</span>
<span class="nc" id="L116"> jdbcTemplate.execute(query);</span>
<span class="nc" id="L117"> log.debug(&quot;Assigned user2 role role2&quot;);</span>
<span class="nc" id="L118"> }</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>