1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.samples.sprhib.dao; |
20 | |
|
21 | |
import org.apache.shiro.crypto.hash.Sha256Hash; |
22 | |
import org.hibernate.SessionFactory; |
23 | |
import org.springframework.beans.factory.InitializingBean; |
24 | |
import org.springframework.beans.factory.annotation.Autowired; |
25 | |
import org.springframework.jdbc.core.JdbcTemplate; |
26 | |
import org.springframework.stereotype.Component; |
27 | |
|
28 | |
import javax.sql.DataSource; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
@Component |
34 | 0 | public class BootstrapDataPopulator implements InitializingBean { |
35 | |
|
36 | |
private DataSource dataSource; |
37 | |
@SuppressWarnings({"FieldCanBeLocal"}) |
38 | |
private SessionFactory sessionFactory; |
39 | |
|
40 | |
@Autowired |
41 | |
public void setDataSource(DataSource dataSource) { |
42 | 0 | this.dataSource = dataSource; |
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
@Autowired |
47 | |
public void setSessionFactory(SessionFactory sessionFactory) { |
48 | 0 | this.sessionFactory = sessionFactory; |
49 | 0 | } |
50 | |
|
51 | |
public void afterPropertiesSet() throws Exception { |
52 | |
|
53 | |
|
54 | 0 | JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource); |
55 | |
|
56 | 0 | jdbcTemplate.execute("insert into roles values (1, 'user', 'The default role given to all users.')"); |
57 | 0 | jdbcTemplate.execute("insert into roles values (2, 'admin', 'The administrator role only given to site admins')"); |
58 | 0 | jdbcTemplate.execute("insert into roles_permissions values (2, 'user:*')"); |
59 | 0 | jdbcTemplate.execute("insert into users(id,username,email,password) values (1, 'admin', 'sample@shiro.apache.org', '" + new Sha256Hash("admin").toHex() + "')"); |
60 | 0 | jdbcTemplate.execute("insert into users_roles values (1, 2)"); |
61 | |
|
62 | |
|
63 | 0 | } |
64 | |
} |