本文整理汇总了Java中org.apache.shiro.realm.jdbc.JdbcRealm类的典型用法代码示例。如果您正苦于以下问题:Java JdbcRealm类的具体用法?Java JdbcRealm怎么用?Java JdbcRealm使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JdbcRealm类属于org.apache.shiro.realm.jdbc包,在下文中一共展示了JdbcRealm类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreateRealm
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
@Test
public void testCreateRealm() throws NoSuchFieldException, IllegalAccessException {
DataSource ds = mock(DataSource.class);
DataSourceFactory mockDSFactory = mock((DataSourceFactory.class));
when(mockDSFactory.forName("testDS")).thenReturn(ds);
Injector injector = mock(Injector.class);
Mockito.when(injector.getInstance(DataSourceFactory.class)).thenReturn(mockDSFactory);
JdbcRealmFactory factory = new JdbcRealmFactory();
factory.setName("testName");
factory.setDatasource("testDS");
JdbcRealm realm = (JdbcRealm) factory.createRealm(injector);
assertEquals("testName", realm.getName());
Field dsField = JdbcRealm.class.getDeclaredField("dataSource");
dsField.setAccessible(true);
assertSame(ds, dsField.get(realm));
}
示例2: jdbcRealm
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
@Bean(name = "mainRealm")
@ConditionalOnMissingBean(name = "mainRealm")
@ConditionalOnProperty(prefix = "shiro.realm.jdbc", name = "enabled", havingValue = "true")
@DependsOn(value = {"dataSource", "lifecycleBeanPostProcessor", "credentialsMatcher"})
public Realm jdbcRealm(DataSource dataSource, CredentialsMatcher credentialsMatcher) {
JdbcRealm realm = new JdbcRealm();
if (shiroJdbcRealmProperties.getAuthenticationQuery() != null) {
realm.setAuthenticationQuery(shiroJdbcRealmProperties.getAuthenticationQuery());
}
if (shiroJdbcRealmProperties.getUserRolesQuery() != null) {
realm.setUserRolesQuery(shiroJdbcRealmProperties.getUserRolesQuery());
}
if (shiroJdbcRealmProperties.getPermissionsQuery() != null) {
realm.setPermissionsQuery(shiroJdbcRealmProperties.getPermissionsQuery());
}
if (shiroJdbcRealmProperties.getSalt() != null) {
realm.setSaltStyle(shiroJdbcRealmProperties.getSalt());
}
realm.setPermissionsLookupEnabled(shiroJdbcRealmProperties.isPermissionsLookupEnabled());
realm.setDataSource(dataSource);
realm.setCredentialsMatcher(credentialsMatcher);
return realm;
}
示例3: testHashedCredentialsMatcherWithJdbcRealm
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
@Test
public void testHashedCredentialsMatcherWithJdbcRealm(){
BeanUtilsBean.getInstance().getConvertUtils().register(new EnumConverter(), JdbcRealm.SaltStyle.class);
//使用testGeneratePassword生成的散列密码
login("classpath:shiro-jdbc-hashedCredentialsMatcher.ini", "liu", "123");
}
示例4: test
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
@Test
public void test(){
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//设置authenticator
ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
securityManager.setAuthenticator(authenticator);
//设置authorizer
ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer();
authorizer.setPermissionResolver(new WildcardPermissionResolver());
securityManager.setAuthorizer(authorizer);
//创建datasource
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://192.168.31.188:3306/shiro");
dataSource.setUsername("root");
dataSource.setPassword("[email protected]");
//设置real
JdbcRealm realm = new JdbcRealm();
realm.setDataSource(dataSource);
realm.setPermissionsLookupEnabled(true);
securityManager.setRealm(realm);
//将SecurityManager设置到SecurityUtils,方便全局使用
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
subject.login(token);
Assert.assertTrue(subject.isAuthenticated());
}
示例5: createRealm
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
@Override
public Realm createRealm(Injector injector) {
DataSource ds = findDataSource(injector.getInstance(DataSourceFactory.class));
JdbcRealm realm = new JdbcRealm();
if (name != null) {
realm.setName(name);
}
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;
}
示例6: jdbcRealm
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
@Bean(name = "jdbcRealm")
@DependsOn("lifecycleBeanPostProcessor")
public JdbcRealm jdbcRealm() {
JdbcRealm realm = new JdbcRealm();
HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
credentialsMatcher.setHashAlgorithmName(Sha256Hash.ALGORITHM_NAME);
realm.setCredentialsMatcher(credentialsMatcher);
realm.setDataSource(dataSource);
realm.init();
return realm;
}
示例7: createJdbcRealm
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
private JdbcRealm createJdbcRealm() {
JdbcRealm realm = new JdbcRealm();
realm.setDataSource(dataSource);
realm.setAuthenticationQuery("SELECT user_pass FROM JAWN_USERS where user_login = ?");
realm.setUserRolesQuery("select role_name from user_roles where username = ?");
realm.setPermissionsLookupEnabled(false);
realm.setSaltStyle(SaltStyle.NO_SALT); // README this of course needs to be implemented at some point
return realm;
}
示例8: provideRealm
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
@Singleton
@Provides
Realm provideRealm(DataSource dataSource) {
JdbcRealm realm = new JdbcRealm();
realm.setPermissionsLookupEnabled(true);
realm.setDataSource(dataSource);
realm.setCacheManager(new MemoryConstrainedCacheManager());
return realm;
}
示例9: getSalt
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
public JdbcRealm.SaltStyle getSalt() {
return salt;
}
示例10: setSalt
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
public void setSalt(JdbcRealm.SaltStyle salt) {
this.salt = salt;
}
示例11: setSaltStyle
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
@BQConfigProperty
public void setSaltStyle(JdbcRealm.SaltStyle saltStyle) {
this.saltStyle = saltStyle;
}
示例12: getUserList
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
/**
* Get userlist
* Returns list of all user from available realms
*
* @return 200 response
*/
@GET
@Path("userlist/{searchText}")
public Response getUserList(@PathParam("searchText") final String searchText) {
List<String> usersList = new ArrayList<>();
List<String> rolesList = new ArrayList<>();
try {
GetUserList getUserListObj = new GetUserList();
Collection realmsList = SecurityUtils.getRealmsList();
if (realmsList != null) {
for (Iterator<Realm> iterator = realmsList.iterator(); iterator.hasNext(); ) {
Realm realm = iterator.next();
String name = realm.getClass().getName();
if (LOG.isDebugEnabled()) {
LOG.debug("RealmClass.getName: " + name);
}
if (name.equals("org.apache.shiro.realm.text.IniRealm")) {
usersList.addAll(getUserListObj.getUserList((IniRealm) realm));
rolesList.addAll(getUserListObj.getRolesList((IniRealm) realm));
} else if (name.equals("org.apache.zeppelin.realm.LdapGroupRealm")) {
usersList.addAll(getUserListObj.getUserList((JndiLdapRealm) realm, searchText));
} else if (name.equals("org.apache.zeppelin.realm.LdapRealm")) {
usersList.addAll(getUserListObj.getUserList((LdapRealm) realm, searchText));
rolesList.addAll(getUserListObj.getRolesList((LdapRealm) realm));
} else if (name.equals("org.apache.zeppelin.realm.ActiveDirectoryGroupRealm")) {
usersList.addAll(getUserListObj.getUserList((ActiveDirectoryGroupRealm) realm,
searchText));
} else if (name.equals("org.apache.shiro.realm.jdbc.JdbcRealm")) {
usersList.addAll(getUserListObj.getUserList((JdbcRealm) realm));
}
}
}
} catch (Exception e) {
LOG.error("Exception in retrieving Users from realms ", e);
}
List<String> autoSuggestUserList = new ArrayList<>();
List<String> autoSuggestRoleList = new ArrayList<>();
Collections.sort(usersList);
Collections.sort(rolesList);
Collections.sort(usersList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1.matches(searchText + "(.*)") && o2.matches(searchText + "(.*)")) {
return 0;
} else if (o1.matches(searchText + "(.*)")) {
return -1;
}
return 0;
}
});
int maxLength = 0;
for (String user : usersList) {
if (StringUtils.containsIgnoreCase(user, searchText)) {
autoSuggestUserList.add(user);
maxLength++;
}
if (maxLength == 5) {
break;
}
}
for (String role : rolesList) {
if (StringUtils.containsIgnoreCase(role, searchText)) {
autoSuggestRoleList.add(role);
}
}
Map<String, List> returnListMap = new HashMap<>();
returnListMap.put("users", autoSuggestUserList);
returnListMap.put("roles", autoSuggestRoleList);
return new JsonResponse<>(Response.Status.OK, "", returnListMap).build();
}
示例13: JDBCUserStore
import org.apache.shiro.realm.jdbc.JdbcRealm; //导入依赖的package包/类
public JDBCUserStore() {
jdbcRealm = new JdbcRealm();
}