本文整理汇总了Java中org.apache.shiro.realm.Realm类的典型用法代码示例。如果您正苦于以下问题:Java Realm类的具体用法?Java Realm怎么用?Java Realm使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Realm类属于org.apache.shiro.realm包,在下文中一共展示了Realm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getRealm
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T getRealm(Class<? extends Realm> realmType){
RealmSecurityManager securityManager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
if(!CollectionUtils.isEmpty(securityManager.getRealms())){
for(Iterator<Realm> it = securityManager.getRealms().iterator(); it.hasNext();){
Realm realm = it.next();
if(realm.getClass().equals(realmType)){
return (T) realm;
}
}
}
return null;
}
示例2: getPrincipals
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Override
public PrincipalCollection getPrincipals() {
RealmSecurityManager manager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
SimplePrincipalCollection ret = new SimplePrincipalCollection();
for (Realm realm : manager.getRealms()) {
/*
if (realm instanceof ProfileRealm) {
String email = token.getEmail();
if (((ProfileRealm) realm).accountExists(email)) {
ret.add(email, realm.getName());
}
}
*/
}
ret.add(token.getEmail(), bearerTokenAuthenticatingRealm.getName());
return ret;
}
示例3: afterAttempt
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
/**
* 在每个Realm之后调用
*/
@Override
public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t) throws AuthenticationException {
AuthenticationInfo authenticationInfo = null;
if(singleRealmInfo == null){//当前没有通过验证
authenticationInfo = aggregateInfo;//保存之前所合并的
}else{//通过验证
if(aggregateInfo== null){//之前没有合并过
authenticationInfo = singleRealmInfo;//初始化
}else{
authenticationInfo = merge(singleRealmInfo, aggregateInfo);//合并
if(authenticationInfo.getPrincipals().getRealmNames().size() > 1){
System.out.println(authenticationInfo.getPrincipals().getRealmNames());
throw new AuthenticationException("[" + token.getClass() + "] " +
"这个认证令牌无法通过realm的验证,请确认您提供的令牌只允许通过1个realm验证");
}
}
}
return authenticationInfo;
}
示例4: doAuthenticate
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Override
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken)
throws AuthenticationException {
assertRealmsConfigured();
//根据不同类型的token找对应的的Realm
String realmKey = "";
if(authenticationToken instanceof MemberUserToken) {
realmKey = ((MemberUserToken)authenticationToken).getRealmKey();
} else if(authenticationToken instanceof SysUserToken) {
realmKey = ((SysUserToken)authenticationToken).getRealmKey();
}
if(StringUtils.isEmpty(realmKey)) {
// 抛异常还是支持multiple Realms
// return doMultiRealmAuthentication(realms, authenticationToken);
throw new AuthenticationException("不支持token:" + authenticationToken.getClass().getName());
} else {
Realm realm = lookupRealm(realmKey);
return doSingleRealmAuthentication(realm, authenticationToken);
}
}
示例5: createRealm
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Override
public Realm createRealm(Injector injector) {
Ini ini = new Ini();
if (users != null && !users.isEmpty()) {
ini.addSection("users").putAll(users);
}
if (roles != null && !roles.isEmpty()) {
ini.addSection("roles").putAll(roles);
}
IniRealm realm = new IniRealm(ini);
realm.setIni(ini);
if (name != null) {
realm.setName(name);
}
return realm;
}
示例6: createRealms
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
public Realms createRealms(Injector injector, Set<Realm> diRealms) {
// no configured realms, use DI Realms
if (realms == null || realms.isEmpty()) {
return new Realms(new ArrayList<>(diRealms));
}
// use configured realms...
// ignoring DI Realms if at least one config realm exists. This allows to fully override and/or order realms
// without recompiling
if (!diRealms.isEmpty() && LOGGER.isInfoEnabled()) {
String realmNames = diRealms.stream()
.map(r -> r.getName() != null ? r.getName() : r.getClass().getSimpleName()).collect(joining(", "));
LOGGER.info("Ignoring DI-originated Realms: " + realmNames + ". Using Realms from configuration instead.");
}
List<Realm> orderedRealms = new ArrayList<>(diRealms.size());
realms.forEach(rf -> orderedRealms.add(rf.createRealm(injector)));
return new Realms(orderedRealms);
}
示例7: testCreateRealms
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Test
public void testCreateRealms() {
BQRuntime bqRuntime = testFactory
.app("-c", "classpath:io/bootique/shiro/realm/RealmFactoryInheritanceIT.yml")
.autoLoadModules()
.createRuntime();
Object[] names = bqRuntime.getInstance(Realms.class).getRealms().stream().map(Realm::getName).toArray();
assertEquals(3, names.length);
assertEquals("Created by RealmFactory2", names[0]);
assertEquals("Created by RealmFactory1", names[1]);
assertEquals("Created by RealmFactory2", names[2]);
}
示例8: testCreateRealms_NoConfig
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Test
public void testCreateRealms_NoConfig() {
Realm r1 = Mockito.mock(Realm.class);
Realm r2 = Mockito.mock(Realm.class);
Set<Realm> diRealms = new HashSet<>(asList(r1, r2));
RealmsFactory realmsFactory = new RealmsFactory();
Realms realms = realmsFactory.createRealms(Guice.createInjector(), diRealms);
Assert.assertNotNull(realms);
assertEquals(2, realms.getRealms().size());
assertTrue(realms.getRealms().contains(r1));
assertTrue(realms.getRealms().contains(r2));
}
示例9: testCreateRealms_NoDi
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Test
public void testCreateRealms_NoDi() {
Injector injector = Guice.createInjector();
Realm r1 = Mockito.mock(Realm.class);
Realm r2 = Mockito.mock(Realm.class);
RealmFactory rf1 = Mockito.mock(RealmFactory.class);
Mockito.when(rf1.createRealm(injector)).thenReturn(r1);
RealmFactory rf2 = Mockito.mock(RealmFactory.class);
Mockito.when(rf2.createRealm(injector)).thenReturn(r2);
List<RealmFactory> configFactories = asList(rf1, rf2);
RealmsFactory realmsFactory = new RealmsFactory();
realmsFactory.setRealms(configFactories);
Realms realms = realmsFactory.createRealms(injector, Collections.emptySet());
Assert.assertNotNull(realms);
assertEquals(2, realms.getRealms().size());
assertEquals("Realm ordering got lost", r1, realms.getRealms().get(0));
assertEquals("Realm ordering got lost", r2, realms.getRealms().get(1));
}
示例10: testFullStack
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Test
public void testFullStack() {
Realm mockRealm = mockRealm();
BQRuntime runtime = testFactory.app()
.module(b -> ShiroModule.extend(b).addRealm(mockRealm))
.autoLoadModules()
.createRuntime();
Subject subject = new Subject.Builder(runtime.getInstance(SecurityManager.class)).buildSubject();
assertFalse(subject.isAuthenticated());
// try bad login
try {
subject.login(new UsernamePasswordToken("uname", "badpassword"));
Assert.fail("Should have thrown on bad auth");
} catch (AuthenticationException authEx) {
assertFalse(subject.isAuthenticated());
}
// try good login
subject.login(new UsernamePasswordToken("uname", "password"));
assertTrue(subject.isAuthenticated());
}
示例11: testFullStack_SecurityUtils
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Test
public void testFullStack_SecurityUtils() {
Realm mockRealm = mockRealm();
BQRuntime runtime = testFactory.app()
.module(b -> ShiroModule.extend(b).addRealm(mockRealm))
.autoLoadModules()
.createRuntime();
Subject subject = new Subject.Builder(runtime.getInstance(SecurityManager.class)).buildSubject();
assertNull(ThreadContext.getSubject());
// testing Shiro idiom of wrapping lambda in a subject...
subject.execute(() -> {
assertSame("Unexpected subject, thread state is disturbed", subject, SecurityUtils.getSubject());
});
}
示例12: doMultiRealmAuthentication
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Override
protected AuthenticationInfo doMultiRealmAuthentication(
Collection<Realm> realms, AuthenticationToken token) {
SSORealm ssoRealm = null;
SNSRealm snsRealm = null;
JDBCRealm jdbcRealm = null;
for (Realm realm : realms) {
if (realm instanceof SSORealm) {
ssoRealm = (SSORealm) realm;
} else if (realm instanceof SNSRealm) {
snsRealm = (SNSRealm) realm;
} else {
jdbcRealm = (JDBCRealm) realm;
}
}
//核心思想,判断token类型,选择realm
if(token instanceof SNSAuthenticationToken)
return doSingleRealmAuthentication(snsRealm, token);
else if(token instanceof SSOAuthenticationToken)
return doSingleRealmAuthentication(ssoRealm, token);
else
return doSingleRealmAuthentication(jdbcRealm, token);
}
示例13: getShiroFilterFactoryBean
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Bean(name = "shiroFilter")
@DependsOn("securityManager")
@ConditionalOnMissingBean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultSecurityManager securityManager, Realm realm, ShiroFilterRegistry registry) {
securityManager.setRealm(realm);
Map<String, String> filterDef = swapKeyValue(properties.getFilterChainDefinitions());
log.info("过虑器配置: {}", filterDef);
log.info("自定义过虑器: {}", registry.getFilterMap());
ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
shiroFilter.setSecurityManager(securityManager);
shiroFilter.setLoginUrl(properties.getLoginUrl());
shiroFilter.setSuccessUrl(properties.getSuccessUrl());
shiroFilter.setUnauthorizedUrl(properties.getUnauthorizedUrl());
shiroFilter.setFilterChainDefinitionMap(filterDef);
shiroFilter.getFilters().putAll(registry.getFilterMap());
return shiroFilter;
}
示例14: jdbcRealm
import org.apache.shiro.realm.Realm; //导入依赖的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;
}
示例15: securityManager
import org.apache.shiro.realm.Realm; //导入依赖的package包/类
@Bean
public DefaultWebSecurityManager securityManager() {
realms = realms == null ? new ArrayList<Realm>() : realms;
boolean existCasRealm = false;
for (Realm realm : realms) {
if (realm == casRealm()) {
existCasRealm = true;
break;
}
}
if (!existCasRealm) {
realms.add(0, casRealm());
}
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
//authenticator必须在realm前面设值,因为setRealm时会有条件的设置authenticator里的realm
manager.setAuthenticator(authenticator());
manager.setSubjectFactory(casSubjectFactory());
manager.setCacheManager(securityCacheManager());
manager.setSessionManager(sessionManager());
manager.setRealms(realms);
return manager;
}