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

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

Introduction

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

Prototype

public void setPermissionsLookupEnabled(boolean permissionsLookupEnabled) 

Source Link

Document

Enables lookup of permissions during authorization.

Usage

From source file:cnki.shiro.helloworld.JdbcRelamTest.java

public static void main(String[] args) {

    System.out.println("Hello shiro!");

    MysqlDataSource datasource = new MysqlDataSource();

    datasource.setUser("cnki");

    datasource.setPassword("cnki");

    datasource.setServerName("192.168.100.51");

    // datasource.setDriverClassName("com.mysql.jdbc.Driver");

    datasource.setUrl("jdbc:mysql://192.168.100.51:3306/test");

    // datasource.setMaxActive(10);

    org.apache.shiro.realm.jdbc.JdbcRealm jdbcRealm = new JdbcRealm();

    jdbcRealm.setDataSource(datasource);

    jdbcRealm.setPermissionsLookupEnabled(true);

    jdbcRealm.setAuthenticationQuery("SELECT PASSWORD FROM account WHERE name = ?");

    jdbcRealm.setUserRolesQuery(/*from   w  ww .jav  a  2 s  .co m*/
            "SELECT NAME FROM role WHERE id =(SELECT roleId FROM account_role WHERE userId = (SELECT id FROM account WHERE NAME = ?))");

    jdbcRealm.setPermissionsQuery(
            "SELECT NAME FROM permission WHERE id in (SELECT permissionId FROM permission_role WHERE (SELECT id FROM role WHERE NAME = ?))");

    DefaultSecurityManager security = new DefaultSecurityManager(jdbcRealm);

    SecurityUtils.setSecurityManager(security);
    Subject currentUser = SecurityUtils.getSubject();
    if (!currentUser.isAuthenticated()) {

        UsernamePasswordToken token = new UsernamePasswordToken("ynp", "2222");

        token.setRememberMe(true);
        try {
            currentUser.login(token);

            System.out.println("login successfully");

        } catch (UnknownAccountException uae) {

            System.out.println("There is no user with username of " + token.getPrincipal());

        } catch (IncorrectCredentialsException ice) {

            System.out.println("Password for account " + token.getPrincipal() + " was incorrect!");

        } catch (LockedAccountException lae) {

            System.out.println("The account for username " + token.getPrincipal() + " is locked.  " +

                    "Please contact your administrator to unlock it.");

        }

        // ... catch more exceptions here (maybe custom ones specific to
        // your application?

        catch (AuthenticationException ae) {

            // unexpected condition? error?

        }

    }

    // say who they are:

    // print their identifying principal (in this case, a username):

    System.out.println("User [" + currentUser.getPrincipal() + "] logged in successfully.");

    // test a role:

    if (currentUser.hasRole("admin")) {

        System.out.println("May the admin be with you!");

    } else {

        System.out.println("Hello, mere mortal.");

    }

    // test a typed permission (not instance-level)

    if (currentUser.isPermitted("write")) {
        System.out.println("You can write!.");
    } else {

        System.out.println("Sorry, lightsaber rings are for schwartz masters only.");
    }

    // a (very powerful) Instance Level permission:

    if (currentUser.isPermitted("winnebago:drive:eagle5")) {

        System.out.println("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +

                "Here are the keys - have fun!");

    } else {

        System.out.println("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");

    }

    // all done - log out!

    currentUser.logout();

}

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

License:Apache License

@Bean
public JdbcRealm realm() {
    ConfigObject config = config();//from ww  w .  ja v  a2s . 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;
}

From source file:com.snail.controller.test.JdbcReamTest.java

public static void main(String[] args) {

    System.out.println("Hello shiro!");

    MysqlDataSource datasource = new MysqlDataSource();
    datasource.setUser("root");
    datasource.setPassword("12345");
    datasource.setServerName("localhost");
    // datasource.setDriverClassName("com.mysql.jdbc.Driver");
    datasource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8");
    // datasource.setMaxActive(10);

    org.apache.shiro.realm.jdbc.JdbcRealm jdbcRealm = new JdbcRealm();
    jdbcRealm.setDataSource(datasource);
    jdbcRealm.setPermissionsLookupEnabled(true);
    jdbcRealm.setAuthenticationQuery("SELECT password FROM users WHERE username = ?");
    jdbcRealm.setUserRolesQuery("SELECT rolename FROM user_roles WHERE username= ?");
    jdbcRealm.setPermissionsQuery("SELECT permission FROM user_permissions WHERE rolename= ?");
    //      jdbcRealm
    //            .setPermissionsQuery("SELECT NAME FROM permission WHERE id in (SELECT permissionId FROM permission_role WHERE (SELECT id FROM role WHERE NAME = ?))");
    DefaultSecurityManager security = new DefaultSecurityManager(jdbcRealm);
    SecurityUtils.setSecurityManager(security);
    Subject currentUser = SecurityUtils.getSubject();
    if (!currentUser.isAuthenticated()) {
        //lilei/*from ww w.j  a v  a 2  s  .  c o m*/
        UsernamePasswordToken token = new UsernamePasswordToken("lilei", "1234");
        token.setRememberMe(true);
        try {
            currentUser.login(token);
            System.out.println("login successfully");
        } catch (UnknownAccountException uae) {
            System.out.println("There is no user with username of " + token.getPrincipal());
        } catch (IncorrectCredentialsException ice) {
            System.out.println("Password for account " + token.getPrincipal() + " was incorrect!");
        } catch (LockedAccountException lae) {
            System.out.println("The account for username " + token.getPrincipal() + " is locked.  "
                    + "Please contact your administrator to unlock it.");
        }
        // ... catch more exceptions here (maybe custom ones specific to
        // your application?
        catch (AuthenticationException ae) {
            // unexpected condition? error?
        }
    }
    // say who they are:
    // print their identifying principal (in this case, a username):
    System.out.println("User [" + currentUser.getPrincipal() + "] logged in successfully.");
    // test a role:
    if (currentUser.hasRole("admin")) {
        System.out.println("May the admin be with you!");
    } else {
        System.out.println("Hello, mere mortal.");
    }

    // test a typed permission (not instance-level)

    if (currentUser.isPermitted("write")) {
        System.out.println("You can write!.");
    } else {

        System.out.println("Sorry, lightsaber rings are for schwartz masters only.");
    }
    // a (very powerful) Instance Level permission: TODO
    if (currentUser.isPermitted("winnebago:drive:eagle5")) {
        System.out.println("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  "
                + "Here are the keys - have fun!");
    } else {
        System.out.println("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
    }
    // all done - log out!
    currentUser.logout();
}

From source file:com.vectorization.server.node.AppInjector.java

License:Open Source License

@Singleton
@Provides//w  w  w . j a v a  2 s .  c  o m
Realm provideRealm(DataSource dataSource) {
    JdbcRealm realm = new JdbcRealm();
    realm.setPermissionsLookupEnabled(true);
    realm.setDataSource(dataSource);
    realm.setCacheManager(new MemoryConstrainedCacheManager());
    return realm;
}

From source file:io.bootique.shiro.jdbc.realm.JdbcRealmFactory.java

License:Apache License

@Override
public Realm createRealm(Injector injector) {

    DataSource ds = findDataSource(injector.getInstance(DataSourceFactory.class));

    JdbcRealm realm = new JdbcRealm();

    if (name != null) {
        realm.setName(name);/*from w w  w.ja  v a2  s  . c om*/
    }

    realm.setDataSource(ds);
    realm.setPermissionsLookupEnabled(lookupPermissions);

    if (authenticationQuery != null) {
        realm.setAuthenticationQuery(authenticationQuery);
    }

    if (permissionsQuery != null) {
        realm.setPermissionsQuery(permissionsQuery);
    }

    if (saltStyle != null) {
        realm.setSaltStyle(saltStyle);
    }

    if (userRolesQuery != null) {
        realm.setUserRolesQuery(userRolesQuery);
    }

    return realm;
}