Example usage for org.apache.shiro.realm.jdbc JdbcRealm init

List of usage examples for org.apache.shiro.realm.jdbc JdbcRealm init

Introduction

In this page you can find the example usage for org.apache.shiro.realm.jdbc JdbcRealm init.

Prototype

public final void init() 

Source Link

Document

Initializes this realm and potentially enables an authentication cache, depending on configuration.

Usage

From source file:com.aegeus.core.AuthenticationConfiguration.java

License:Apache License

@Bean
public JdbcRealm realm() {
    ConfigObject config = config();//from  w w w.j  a  v a 2s. c om

    String uri = String.format("jdbc:%s://%s:%d/%s", config.getWorkflow().getMetaStore().getType(),
            config.getWorkflow().getMetaStore().getHost(), config.getWorkflow().getMetaStore().getPort(),
            config.getWorkflow().getMetaStore().getDb());

    // initialize meta store database connection
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL(uri);
    ds.setUser(config.getWorkflow().getMetaStore().getUsername());
    ds.setPassword(config.getWorkflow().getMetaStore().getPassword());

    HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
    matcher.setHashAlgorithmName(Sha256Hash.ALGORITHM_NAME);

    JdbcRealm realm = new JdbcRealm();
    realm.setDataSource(ds);
    realm.setPermissionsLookupEnabled(true);
    realm.setAuthenticationQuery("SELECT pass FROM users WHERE user = ?");
    realm.setPermissionsQuery(
            "SELECT p.permission FROM permissions p INNER JOIN users u ON p.user_id = u.id WHERE u.user = ?");
    realm.setUserRolesQuery(
            "SELECT r.role FROM roles r INNER JOIN users u ON u.id = r.user_id WHERE u.user = ?");
    realm.setCredentialsMatcher(matcher);
    realm.init();

    return realm;
}