use of org.apache.shiro.crypto.hash.Sha256Hash in project shiro by apache.
the class DefaultUserService method createUser.
public void createUser(String username, String email, String password) {
User user = new User();
user.setUsername(username);
user.setEmail(email);
user.setPassword(new Sha256Hash(password).toHex());
userDAO.createUser(user);
}
use of org.apache.shiro.crypto.hash.Sha256Hash in project shiro by apache.
the class BootstrapDataPopulator method afterPropertiesSet.
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:
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
jdbcTemplate.execute(CREATE_TABLES);
// 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!) )
String query = "insert into users values ('user1', '" + new Sha256Hash("user1", "user1").toBase64() + "' )";
jdbcTemplate.execute(query);
log.debug("Created user1.");
// password is 'user2' SHA hashed and base64 encoded:
query = "insert into users values ( 'user2', '" + new Sha256Hash("user2", "user2").toBase64() + "' )";
jdbcTemplate.execute(query);
log.debug("Created user2.");
query = "insert into roles values ( 'role1' )";
jdbcTemplate.execute(query);
log.debug("Created role1");
query = "insert into roles values ( 'role2' )";
jdbcTemplate.execute(query);
log.debug("Created role2");
query = "insert into roles_permissions values ( 'role1', 'permission1')";
jdbcTemplate.execute(query);
log.debug("Created permission 1 for role 1");
query = "insert into roles_permissions values ( 'role1', 'permission2')";
jdbcTemplate.execute(query);
log.debug("Created permission 2 for role 1");
query = "insert into roles_permissions values ( 'role2', 'permission1')";
jdbcTemplate.execute(query);
log.debug("Created permission 1 for role 2");
query = "insert into user_roles values ( 'user1', 'role1' )";
jdbcTemplate.execute(query);
query = "insert into user_roles values ( 'user1', 'role2' )";
jdbcTemplate.execute(query);
log.debug("Assigned user1 roles role1 and role2");
query = "insert into user_roles values ( 'user2', 'role2' )";
jdbcTemplate.execute(query);
log.debug("Assigned user2 role role2");
}
use of org.apache.shiro.crypto.hash.Sha256Hash in project shiro by apache.
the class BootstrapDataPopulator method afterPropertiesSet.
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 insert the sample admin user at startup:
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
jdbcTemplate.execute("insert into roles values (1, 'user', 'The default role given to all users.')");
jdbcTemplate.execute("insert into roles values (2, 'admin', 'The administrator role only given to site admins')");
jdbcTemplate.execute("insert into roles_permissions values (2, 'user:*')");
jdbcTemplate.execute("insert into users(id,username,email,password) values (1, 'admin', 'sample@shiro.apache.org', '" + new Sha256Hash("admin").toHex() + "')");
jdbcTemplate.execute("insert into users_roles values (1, 2)");
}
use of org.apache.shiro.crypto.hash.Sha256Hash in project shiro by apache.
the class JDBCRealmTest method createSaltColumnSchema.
/**
* Creates a test database with a separate salt column in the users table. Sets the
* DataSource of the realm associated with the test to a DataSource connected to the database.
*/
protected void createSaltColumnSchema(String testName) {
jdbcDataSource ds = new jdbcDataSource();
ds.setDatabase("jdbc:hsqldb:mem:" + name);
ds.setUser("SA");
ds.setPassword("");
Connection conn = null;
Statement sql = null;
try {
conn = ds.getConnection();
sql = conn.createStatement();
sql.executeUpdate("create table users (username varchar(20), password varchar(20), password_salt varchar(20))");
Sha256Hash sha256Hash = new Sha256Hash(plainTextPassword, salt);
String password = sha256Hash.toHex();
sql.executeUpdate("insert into users values ('" + username + "', '" + password + "', '" + salt + "')");
} catch (SQLException ex) {
Assert.fail("Exception creating test database");
} finally {
JdbcUtils.closeStatement(sql);
JdbcUtils.closeConnection(conn);
}
createRolesAndPermissions(ds);
realmMap.get(testName).setDataSource(ds);
dsMap.put(testName, ds);
}
use of org.apache.shiro.crypto.hash.Sha256Hash in project nutzboot by nutzam.
the class MainLauncher method newUser.
protected static User newUser(String name, String password) {
User user = new User();
user.setName(name);
user.setSalt(R.UU32());
user.setPassword(new Sha256Hash(password, user.getSalt()).toHex());
user.setCreateTime(new Date());
return user;
}
Aggregations