本文整理匯總了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;
}