1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
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 | |
|
33 | |
|
34 | |
|
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 | |
|
70 | |
|
71 | |
|
72 | 0 | JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource); |
73 | 0 | jdbcTemplate.execute(CREATE_TABLES); |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
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 | |
|
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 | |
} |