将saiku自带的H2嵌入式用户角色数据库迁移到mysql数据库
2016-06-14 22:37
495 查看
参考原文,略作修改:/article/6483683.html
以上链接文章的思路是正确的,有略微的遗漏,saiku-beans.properties中也存在与用户相关的H2数据库配置,并且在id为“userDaoDataSource”的bean中有用到,为了避免出现问题,将其一并替换,并修改原文中部分配置到配置文件中
1、修改web.xml,将原有的与用户相关的h2数据库配置注释
2、修改saiku-beans.xml中与用户相关的h2数据库配置
3、修改Database中的数据源为mysql并添加代码
修改initDB方法代码
修改loadUsers方法、checkUpdatedEncyption方法、updateForEncyption方法
4、修改数据源配置
文件:saiku-beans.properties
文件:applicationContext-spring-security-jdbc.properties
by 刘迎光
liuyg@liuyingguang.cn
saiku 研究学习交流群: 495266201
博客saiku专栏:http://blog.csdn.net/gsying1474/article/category/2712301
以上链接文章的思路是正确的,有略微的遗漏,saiku-beans.properties中也存在与用户相关的H2数据库配置,并且在id为“userDaoDataSource”的bean中有用到,为了避免出现问题,将其一并替换,并修改原文中部分配置到配置文件中
1、修改web.xml,将原有的与用户相关的h2数据库配置注释
<context-param> <param-name>db.url</param-name> <param-value>jdbc:h2:../../data/saiku;MODE=MySQL</param-value> </context-param> <context-param> <param-name>db.user</param-name> <param-value>sa</param-value> </context-param> <context-param> <param-name>db.password</param-name> <param-value></param-value> </context-param> |
<bean id="h2database" class="org.saiku.database.Database" init-method="init"> <property name="datasourceManager" ref="repositoryDsManager"/> <property name="datasourcetype" value="mysql" /><!-- add by liuyg --> <property name="url" value="${userdao.url}" /> <property name="user" value="${userdao.username}" /> <property name="pword" value="${userdao.password}" /> </bean> |
private String url ; private String user ; private String pword ; private String datasourcetype ; public String getDatasourcetype() { return datasourcetype; } public void setDatasourcetype(String datasourcetype) { this.datasourcetype = datasourcetype; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPword() { return pword; } public void setPword(String pword) { this.pword = pword; } |
private void initDB() { ds = new MysqlDataSource(); ((MysqlDataSource) ds).setUrl(url); ((MysqlDataSource) ds).setUser(user); ((MysqlDataSource) ds).setPassword(pword); } |
private void loadUsers() throws SQLException { Connection c = ds.getConnection(); Statement statement = c.createStatement(); statement.execute(" CREATE TABLE IF NOT EXISTS log ( time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, log TEXT); "); statement.execute(" CREATE TABLE IF NOT EXISTS users(user_id INT(11) NOT NULL AUTO_INCREMENT, " + " username VARCHAR(45) NOT NULL UNIQUE, password VARCHAR(100) NOT NULL, email VARCHAR(100), " + " enabled TINYINT NOT NULL DEFAULT 1, PRIMARY KEY(user_id)); "); statement.execute(" CREATE TABLE IF NOT EXISTS user_roles ( " + " user_role_id INT(11) NOT NULL AUTO_INCREMENT,username VARCHAR(45), " + " user_id INT(11) NOT NULL REFERENCES users(user_id), " + " ROLE VARCHAR(45) NOT NULL, " + " PRIMARY KEY (user_role_id)); "); ResultSet result = statement.executeQuery("select count(*) as c from log where log = 'insert users'"); result.next(); if (result.getInt("c") == 0) { statement.execute("INSERT INTO users (username,password,email, enabled) VALUES ('admin','admin', 'test@admin.com',TRUE);"); statement.execute("INSERT INTO users (username,password,enabled) VALUES ('smith','smith', TRUE);"); statement.execute("INSERT INTO user_roles (user_id, username, ROLE) VALUES (1, 'admin', 'ROLE_USER');"); statement.execute("INSERT INTO user_roles (user_id, username, ROLE) VALUES (1, 'admin', 'ROLE_ADMIN');"); statement.execute("INSERT INTO user_roles (user_id, username, ROLE) VALUES (2, 'smith', 'ROLE_USER');"); statement.execute("INSERT INTO log (log) VALUES('insert users');"); } String encrypt = servletContext.getInitParameter("db.encryptpassword"); if (encrypt.equals("true") && !checkUpdatedEncyption()) { updateForEncyption(); } } public boolean checkUpdatedEncyption() throws SQLException{ Connection c = ds.getConnection(); Statement statement = c.createStatement(); ResultSet result = statement.executeQuery("select count(*) as c from log where log = 'update passwords'"); result.next(); return result.getInt("c") != 0; } public void updateForEncyption() throws SQLException { Connection c = ds.getConnection(); Statement statement = c.createStatement(); statement.execute("ALTER TABLE users MODIFY COLUMN PASSWORD VARCHAR(100) DEFAULT NULL"); ResultSet result = statement.executeQuery("select username, password from users"); while (result.next()) { statement = c.createStatement(); String pword = result.getString("password"); String hashedPassword = passwordEncoder.encode(pword); String sql = "UPDATE users " + "SET password = '" + hashedPassword + "' WHERE username = '" + result.getString("username") + "'"; statement.executeUpdate(sql); } statement = c.createStatement(); statement.execute("INSERT INTO log (log) VALUES('update passwords');"); } |
文件:saiku-beans.properties
userdao.driverclass=com.mysql.jdbc.Driver userdao.url=jdbc:mysql://192.168.11.140:3306/saiku userdao.username=root userdao.password=root |
jdbcauth.driver=com.mysql.jdbc.Driver jdbcauth.url=jdbc:mysql://192.168.11.140:3306/saiku jdbcauth.username=root jdbcauth.password=root |
liuyg@liuyingguang.cn
saiku 研究学习交流群: 495266201
博客saiku专栏:http://blog.csdn.net/gsying1474/article/category/2712301
相关文章推荐
- windows mysql
- MySQL基础配置之mysql的默认字符编码的设置(my.ini设置字符编码)
- mysql中查询null或非null的方法
- mysql 查看表结构的指令
- mysql产生随机数并连接字符串
- MySQL数据库、数据表和字段字符集查询、修改和配置
- mysql 配置相关的变量
- MySQL垂直分割示例
- MySql的安装和使用
- 设置mysql允许外网访问
- FreeRADIUS + MySQL 安装配置笔记
- mysql启动问题access denied for user 'root'@'localhost'(using password:YES)
- MySQL配置文件my.cnf 详解
- 使用JDBC调用mysql存储过程
- MySQL创建用户后本地无法登录的原因
- mysql 存储过程实例
- MySQL分库分表总结参考
- mysql 中文乱码
- mysql Access denied for user root@localhost错误
- 分组查询使用的情况