Search in sources :

Example 1 with Sha256Hash

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);
}
Also used : User(org.apache.shiro.samples.sprhib.model.User) Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash)

Example 2 with Sha256Hash

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");
}
Also used : Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 3 with Sha256Hash

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)");
}
Also used : Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 4 with Sha256Hash

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);
}
Also used : org.hsqldb.jdbc.jdbcDataSource(org.hsqldb.jdbc.jdbcDataSource) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) Connection(java.sql.Connection)

Example 5 with Sha256Hash

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;
}
Also used : User(io.nutz.demo.simple.bean.User) Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) Date(java.util.Date)

Aggregations

Sha256Hash (org.apache.shiro.crypto.hash.Sha256Hash)9 User (io.nutz.demo.simple.bean.User)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 Shiro1CryptFormat (org.apache.shiro.crypto.hash.format.Shiro1CryptFormat)2 Subject (org.apache.shiro.subject.Subject)2 org.hsqldb.jdbc.jdbcDataSource (org.hsqldb.jdbc.jdbcDataSource)2 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)2 ANNISUserConfigurationManager (annis.security.ANNISUserConfigurationManager)1 ANNISUserRealm (annis.security.ANNISUserRealm)1 User (annis.security.User)1 Date (java.util.Date)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)1 SecureRandomNumberGenerator (org.apache.shiro.crypto.SecureRandomNumberGenerator)1 Hash (org.apache.shiro.crypto.hash.Hash)1