List of usage examples for org.apache.shiro.realm.jdbc JdbcRealm JdbcRealm
JdbcRealm
From source file:ch.reboundsoft.shinobi.authstore.realm.JdbcRealmFactory.java
@Inject public JdbcRealmFactory(NinjaProperties ninjaProperties, RealmDataSource ds) { realm = new JdbcRealm(); realm.setDataSource(ds.getDataSource()); realm.setAuthenticationQuery(ninjaProperties.get("shinobi.db.authenticationQuery")); realm.setUserRolesQuery(ninjaProperties.get("shinobi.db.userRolesQuery")); realm.setPermissionsQuery(ninjaProperties.get("shinobi.db.permissionsQuery")); realm.setPermissionsLookupEnabled(true); PasswordMatcher pm = new PasswordMatcher(); pm.setPasswordService(new DefaultPasswordService()); realm.setCredentialsMatcher(pm);/*from ww w.j a va2s . com*/ }
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(/* w ww . ja va 2 s .c o 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();/* w w w . j a va2s . 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.imos.sample.JDBCConnection.java
public static void main(String[] args) { try {//from w w w .j av a 2 s . c o m Realm myRealm = new CustomRealm(); CredentialsMatcher customMatcher = new CustomCredentialsMatcher(); // myRealm.setCredentialsMatcher(customMatcher); MysqlDataSource mysqlDS = new MysqlDataSource(); mysqlDS.setUser("root"); mysqlDS.setPassword("admin"); mysqlDS.setDatabaseName("sampledb"); mysqlDS.setPort(3306); mysqlDS.setUrl("localhost"); try (Connection connection = mysqlDS.getConnection()) { } catch (Exception e) { } Connection connection = mysqlDS.getConnection(); System.out.println(connection.getMetaData().getDatabaseProductName()); System.out.println(connection.getSchema()); connection.close(); JdbcRealm jdbcRealm = new JdbcRealm(); jdbcRealm.setDataSource(mysqlDS); } catch (SQLException ex) { Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex); } }
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 . java 2s . com 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/*from w ww .ja 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 . j a v 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; }
From source file:org.apache.airavata.security.userstore.JDBCUserStore.java
License:Apache License
public JDBCUserStore() { jdbcRealm = new JdbcRealm(); }