List of usage examples for org.apache.shiro.realm.jdbc JdbcRealm setPermissionsQuery
public void setPermissionsQuery(String permissionsQuery)
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 . ja v a2s .com "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 w w w. ja va2 s . co m*/ 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 w w w .ja va2 s . co 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: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);// w ww .j av a 2 s .co m } 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; }