| <?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> > <a href="index.source.html" class="el_package">org.apache.shiro.samples.spring</a> > <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 |
| * "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.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 = "create table users (\n" + |
| " username varchar(255) primary key,\n" + |
| " password varchar(255) not null\n" + |
| ");\n" + |
| "\n" + |
| "create table roles (\n" + |
| " role_name varchar(255) primary key\n" + |
| ");\n" + |
| "\n" + |
| "create table user_roles (\n" + |
| " username varchar(255) not null,\n" + |
| " role_name varchar(255) not null,\n" + |
| " constraint user_roles_uq unique ( username, role_name )\n" + |
| ");\n" + |
| "\n" + |
| "create table roles_permissions (\n" + |
| " role_name varchar(255) not null,\n" + |
| " permission varchar(255) not null,\n" + |
| " primary key (role_name, permission)\n" + |
| ");"; |
| |
| <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( <password>, <cryptographically strong randomly generated salt> (not the username!) ) |
| <span class="nc" id="L80"> String query = "insert into users values ('user1', '" + new Sha256Hash("user1", "user1").toBase64() + "' )";</span> |
| <span class="nc" id="L81"> jdbcTemplate.execute(query);</span> |
| <span class="nc" id="L82"> log.debug("Created user1.");</span> |
| |
| //password is 'user2' SHA hashed and base64 encoded: |
| <span class="nc" id="L85"> query = "insert into users values ( 'user2', '" + new Sha256Hash("user2", "user2").toBase64() + "' )";</span> |
| <span class="nc" id="L86"> jdbcTemplate.execute(query);</span> |
| <span class="nc" id="L87"> log.debug("Created user2.");</span> |
| |
| <span class="nc" id="L89"> query = "insert into roles values ( 'role1' )";</span> |
| <span class="nc" id="L90"> jdbcTemplate.execute(query);</span> |
| <span class="nc" id="L91"> log.debug("Created role1");</span> |
| |
| <span class="nc" id="L93"> query = "insert into roles values ( 'role2' )";</span> |
| <span class="nc" id="L94"> jdbcTemplate.execute(query);</span> |
| <span class="nc" id="L95"> log.debug("Created role2");</span> |
| |
| <span class="nc" id="L97"> query = "insert into roles_permissions values ( 'role1', 'permission1')";</span> |
| <span class="nc" id="L98"> jdbcTemplate.execute(query);</span> |
| <span class="nc" id="L99"> log.debug("Created permission 1 for role 1");</span> |
| |
| <span class="nc" id="L101"> query = "insert into roles_permissions values ( 'role1', 'permission2')";</span> |
| <span class="nc" id="L102"> jdbcTemplate.execute(query);</span> |
| <span class="nc" id="L103"> log.debug("Created permission 2 for role 1");</span> |
| |
| <span class="nc" id="L105"> query = "insert into roles_permissions values ( 'role2', 'permission1')";</span> |
| <span class="nc" id="L106"> jdbcTemplate.execute(query);</span> |
| <span class="nc" id="L107"> log.debug("Created permission 1 for role 2");</span> |
| |
| <span class="nc" id="L109"> query = "insert into user_roles values ( 'user1', 'role1' )";</span> |
| <span class="nc" id="L110"> jdbcTemplate.execute(query);</span> |
| <span class="nc" id="L111"> query = "insert into user_roles values ( 'user1', 'role2' )";</span> |
| <span class="nc" id="L112"> jdbcTemplate.execute(query);</span> |
| <span class="nc" id="L113"> log.debug("Assigned user1 roles role1 and role2");</span> |
| |
| <span class="nc" id="L115"> query = "insert into user_roles values ( 'user2', 'role2' )";</span> |
| <span class="nc" id="L116"> jdbcTemplate.execute(query);</span> |
| <span class="nc" id="L117"> log.debug("Assigned user2 role role2");</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> |