Coverage Report - org.apache.shiro.samples.spring.BootstrapDataPopulator
 
Classes in this File Line Coverage Branch Coverage Complexity
BootstrapDataPopulator
0%
0/37
N/A
1
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.shiro.samples.spring;
 20  
 
 21  
 import javax.sql.DataSource;
 22  
 
 23  
 import org.springframework.beans.factory.InitializingBean;
 24  
 import org.springframework.jdbc.core.JdbcTemplate;
 25  
 
 26  
 import org.slf4j.Logger;
 27  
 import org.slf4j.LoggerFactory;
 28  
 
 29  
 import org.apache.shiro.crypto.hash.Sha256Hash;
 30  
 
 31  
 /**
 32  
  * A data populator that creates a set of security tables and test data that can be used by the
 33  
  * Shiro Spring sample application to demonstrate the use of the {@link org.apache.shiro.realm.jdbc.JdbcRealm}
 34  
  * The tables created by this class follow the default table and column names that {@link org.apache.shiro.realm.jdbc.JdbcRealm} uses.
 35  
  *
 36  
  */
 37  0
 public class BootstrapDataPopulator implements InitializingBean {
 38  
 
 39  
     private static final String CREATE_TABLES = "create table users (\n" +
 40  
             "    username varchar(255) primary key,\n" +
 41  
             "    password varchar(255) not null\n" +
 42  
             ");\n" +
 43  
             "\n" +
 44  
             "create table roles (\n" +
 45  
             "    role_name varchar(255) primary key\n" +
 46  
             ");\n" +
 47  
             "\n" +
 48  
             "create table user_roles (\n" +
 49  
             "    username varchar(255) not null,\n" +
 50  
             "    role_name varchar(255) not null,\n" +
 51  
             "    constraint user_roles_uq unique ( username, role_name )\n" +
 52  
             ");\n" +
 53  
             "\n" +
 54  
             "create table roles_permissions (\n" +
 55  
             "    role_name varchar(255) not null,\n" +
 56  
             "    permission varchar(255) not null,\n" +
 57  
             "    primary key (role_name, permission)\n" +
 58  
             ");";
 59  
 
 60  0
     private static final Logger log = LoggerFactory.getLogger(BootstrapDataPopulator.class);
 61  
 
 62  0
     protected DataSource dataSource = null;
 63  
 
 64  
     public void setDataSource(DataSource dataSource) {
 65  0
         this.dataSource = dataSource;
 66  0
     }
 67  
 
 68  
     public void afterPropertiesSet() throws Exception {
 69  
         //because we're using an in-memory hsqldb for the sample app, a new one will be created each time the
 70  
         //app starts, so create the tables and insert the 2 sample users on bootstrap:
 71  
 
 72  0
         JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
 73  0
         jdbcTemplate.execute(CREATE_TABLES);
 74  
 
 75  
         //password is 'user1' SHA hashed and base64 encoded:
 76  
         //The first argument to the hash constructor is the actual value to be hased.  The 2nd is the
 77  
         //salt.  In this simple demo scenario, the username and the password are the same, but to clarify the
 78  
         //distinction, you would see this in practice:
 79  
         //new Sha256Hash( <password>, <cryptographically strong randomly generated salt> (not the username!) )
 80  0
         String query = "insert into users values ('user1', '" + new Sha256Hash("user1", "user1").toBase64() + "' )";
 81  0
         jdbcTemplate.execute(query);
 82  0
         log.debug("Created user1.");
 83  
 
 84  
         //password is 'user2' SHA hashed and base64 encoded:
 85  0
         query = "insert into users values ( 'user2', '"  + new Sha256Hash("user2", "user2").toBase64() + "' )";
 86  0
         jdbcTemplate.execute(query);
 87  0
         log.debug("Created user2.");
 88  
 
 89  0
         query = "insert into roles values ( 'role1' )";
 90  0
         jdbcTemplate.execute(query);
 91  0
         log.debug("Created role1");
 92  
 
 93  0
         query = "insert into roles values ( 'role2' )";
 94  0
         jdbcTemplate.execute(query);
 95  0
         log.debug("Created role2");
 96  
 
 97  0
         query = "insert into roles_permissions values ( 'role1', 'permission1')";
 98  0
         jdbcTemplate.execute(query);
 99  0
         log.debug("Created permission 1 for role 1");
 100  
 
 101  0
         query = "insert into roles_permissions values ( 'role1', 'permission2')";
 102  0
         jdbcTemplate.execute(query);
 103  0
         log.debug("Created permission 2 for role 1");
 104  
 
 105  0
         query = "insert into roles_permissions values ( 'role2', 'permission1')";
 106  0
         jdbcTemplate.execute(query);
 107  0
         log.debug("Created permission 1 for role 2");
 108  
 
 109  0
         query = "insert into user_roles values ( 'user1', 'role1' )";
 110  0
         jdbcTemplate.execute(query);
 111  0
         query = "insert into user_roles values ( 'user1', 'role2' )";
 112  0
         jdbcTemplate.execute(query);
 113  0
         log.debug("Assigned user1 roles role1 and role2");
 114  
 
 115  0
         query = "insert into user_roles values ( 'user2', 'role2' )";
 116  0
         jdbcTemplate.execute(query);
 117  0
         log.debug("Assigned user2 role role2");
 118  0
     }
 119  
 
 120  
 }