本文整理汇总了Java中org.apache.shiro.subject.SimplePrincipalCollection类的典型用法代码示例。如果您正苦于以下问题:Java SimplePrincipalCollection类的具体用法?Java SimplePrincipalCollection怎么用?Java SimplePrincipalCollection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SimplePrincipalCollection类属于org.apache.shiro.subject包,在下文中一共展示了SimplePrincipalCollection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPrincipals
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的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;
}
示例2: add
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
@Override
protected void add(SimpleAccount account) {
String username = (String)account.getPrincipals().getPrimaryPrincipal();
// Let's add some additional principals for testing
SimplePrincipalCollection principalCollection = new SimplePrincipalCollection();
principalCollection.addAll(account.getPrincipals());
principalCollection.add(counter.getAndIncrement(), "integerRealm");
TestObjPrincipal objPrinc = new TestObjPrincipal(username.toUpperCase()+" "+username.toUpperCase());
principalCollection.add(objPrinc, "objRealm");
account.setPrincipals(principalCollection);
super.add(account); //To change body of generated methods, choose Tools | Templates.
}
示例3: buildAuthenticationInfo
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
/**
* create authentication info, by default, this create
* SimpleAuthenticationInfo with principals using access token as primary
* principal and a map contains attributes {@link OAuth#OAUTH_ACCESS_TOKEN}
* and {@link OAuth#OAUTH_EXPIRES_IN} and {@link OAuth#OAUTH_REFRESH_TOKEN}
* and {@link OAuthConstants#OAUTH_TOKEN_TIME} and
* {@link OAuthConstants#OAUTH_SCOPES}, the credentials set to byte array of
* access token. if sub-class override requestAttributes and returned
* attributes contains key {@link OAuthConstants#OAUTH_PRINCIPAL}, then the
* value will be used as primary principal.
*
* @param clientToken
* the client token
* @param oAuthResponse
* OAuth access token response
* @return authentication info
*/
protected AuthenticationInfo buildAuthenticationInfo(OAuthClientToken clientToken,
OAuthAccessTokenResponse oAuthResponse) {
String accessToken = oAuthResponse.getAccessToken();
Date tokenTime = new Date();
Map<String, Object> attributes = requestAttributes(oAuthResponse);
if (attributes == null)
attributes = new HashMap<String, Object>();
else
attributes = new HashMap<String, Object>(attributes);
List<Object> principals = new ArrayList<Object>();
if (attributes.containsKey(OAuthConstants.OAUTH_PRINCIPAL))
principals.add(attributes.get(OAuthConstants.OAUTH_PRINCIPAL));
else
principals.add(accessToken);
attributes.put(OAuth.OAUTH_ACCESS_TOKEN, accessToken);
attributes.put(OAuth.OAUTH_EXPIRES_IN, oAuthResponse.getExpiresIn());
attributes.put(OAuth.OAUTH_REFRESH_TOKEN, oAuthResponse.getRefreshToken());
attributes.put(OAuthConstants.OAUTH_TOKEN_TIME, tokenTime);
attributes.put(OAuthConstants.OAUTH_SCOPES, clientToken.getScopes());
principals.add(attributes);
PrincipalCollection collection = new SimplePrincipalCollection(principals, getName());
return new SimpleAuthenticationInfo(collection, accessToken);
}
示例4: testAuthorization
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
@Test
public void testAuthorization() throws Exception {
buildTestAuthorizationConfig();
// Fails because the configuration requirement in nexus authorizing realm isn't initialized
// thus NPE
SimplePrincipalCollection principal = new SimplePrincipalCollection("username", realm.getName());
Assert.assertTrue(realm.hasRole(principal, "role"));
// Verify the permission
Assert.assertTrue(realm.isPermitted(principal, new WildcardPermission("app:config:read")));
// Verify other method not allowed
Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:config:create")));
Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:config:update")));
Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:config:delete")));
// Verify other permission not allowed
Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:ui:read")));
Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:ui:create")));
Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:ui:update")));
Assert.assertFalse(realm.isPermitted(principal, new WildcardPermission("app:ui:delete")));
}
示例5: testCacheClearing
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
@Test
public void testCacheClearing() throws Exception {
SecuritySystem securitySystem = this.lookup(SecuritySystem.class);
MockRealmB mockRealmB = (MockRealmB) this.lookup(Realm.class, "MockRealmB");
// cache should be empty to start
Assert.assertTrue(mockRealmB.getAuthorizationCache().keys().isEmpty());
Assert.assertTrue(securitySystem.isPermitted(
new SimplePrincipalCollection("jcool", mockRealmB.getName()), "test:heHasIt"));
// now something will be in the cache, just make sure
Assert.assertFalse(mockRealmB.getAuthorizationCache().keys().isEmpty());
// now if we update a user the cache should be cleared
User user = securitySystem.getUser("bburton", "MockUserManagerB");
// different user, doesn't matter, in the future we should get a little more fine grained
securitySystem.updateUser(user);
// empty again
Assert.assertTrue(mockRealmB.getAuthorizationCache().keys().isEmpty());
}
示例6: doGetAuthenticationInfo
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
String password = new String(upToken.getPassword());
String userId = upToken.getUsername();
// username == password
try {
if (userId.endsWith(password) && userManager.getUser(userId) != null) {
return new SimpleAuthenticationInfo(new SimplePrincipalCollection(token.getPrincipal(),
this.getName()), userId);
}
else {
throw new IncorrectCredentialsException("User [" + userId + "] bad credentials.");
}
}
catch (UserNotFoundException e) {
throw new UnknownAccountException("User [" + userId + "] not found.");
}
}
示例7: doGetAuthorizationInfo
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// 根据用户名查找角色,请根据需求实现
String username = (String) principals.getPrimaryPrincipal();
User user = userService.findByName(username);
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) principals;
principalCollection.clear();
principalCollection.add(user, getName());
RolePermission rolePermission = user.new RolePermission();
authorizationInfo.setRoles(rolePermission.getRoleSet());
authorizationInfo.setStringPermissions(rolePermission.getPermissionSet());
return authorizationInfo;
}
示例8: doCredentialsMatch
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
@Override
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
JsonWebToken jsonWebToken = (JsonWebToken) token;
JWTVerifier verifier = new JWTVerifier(secret, audience);
try {
Map<String, Object> map = verifier.verify(jsonWebToken.getToken());
SimpleAuthenticationInfo authenticationInfo = (SimpleAuthenticationInfo) info;
String realmName = authenticationInfo.getPrincipals().getRealmNames().iterator().next();
SimplePrincipalCollection principals = new SimplePrincipalCollection();
principals.add(map.get("iss"), realmName);
authenticationInfo.setPrincipals(principals);
return true;
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException | SignatureException
| IOException | JWTVerifyException e) {
log.debug(e.getMessage());
return false;
}
}
示例9: main
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
public static void main(String[] args) {
String base64="zGkG0zevFbnIioooS3MfnnbWeBUeiZrScKVJms0CZAdJ2MYTddPkvBHGmMhvgdKA5QJk8FSb9T1Y1tFlUnnCsIvcK+iX4cfrwD7voGdU5bW9AWjwNvl3BDrAgRf+hvjrI3T5nBTFeW7uI6GzfFrIx92qER9lQ97g19Dn555uwIzf0ULvc8jICZTraLLrf2avh1hy2kUJJblO6u5IHiBbAXBNitZ6W1yjLEWmSFnb+EsEWAGB3WwV1u1HZO045LB4G57UIH4QGM0xfJjWWeahiTS4j9IAEJBbghwfL1A5pdzFiXJzzA5GF85vtP+6jLwQfGaQJyv35PvNNsDmCqFe8eUSBLji5z5y/y+yKfZk9izXiEvFjKQ5kqMqKfLMp+Vn5OuO+syc4CfJL4PLI16vwVUPV1EWAzyxUhK7DtD5OMVcLPwVtwZ11dG88wkZtjXvBymLyGCj5/Tk8gTWYsdcNKD5i8WvbMLT45S4iWsZxa/5offIiCipkkqoqvxCppJLTzBoaR/wlqoa1Bc/cvpijiJTIbSCj+iFloQRdax1mMQ";
base64 = ensurePadding(base64);
byte[] decoded = Base64.decode(base64);
byte[] serialized = decoded;
CipherService cipherService = new AesCipherService();
if (cipherService != null) {
ByteSource byteSource = cipherService.decrypt(decoded, new byte[]{-112, -15, -2, 108, -116, 100, -28, 61, -99, 121, -104, -120, -59, -58, -102, 104});
serialized = byteSource.getBytes();
}
Serializer<PrincipalCollection> serializer = new DefaultSerializer<PrincipalCollection>();
;
System.out.println(serializer.deserialize(serialized));
SimplePrincipalCollection p=(SimplePrincipalCollection) serializer.deserialize(serialized);
System.out.println(p.getPrimaryPrincipal());
System.out.println(p.getRealmNames());
System.out.println(p);
}
示例10: createAuthenticationInfo
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
/**
* Method description
*
*
* @param token
* @param result
*
* @return
*/
private AuthenticationInfo createAuthenticationInfo(PublicKeyToken token,
AuthenticationResult result) {
User user = result.getUser();
Collection<String> groups = authenticate(result);
SimplePrincipalCollection collection = new SimplePrincipalCollection();
/*
* the first (primary) principal should be a unique identifier
*/
collection.add(user.getId(), NAME);
collection.add(user, NAME);
collection.add(new GroupNames(groups), NAME);
return new SimpleAuthenticationInfo(collection, token.getPublicKey());
}
示例11: createAuthenticationInfo
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
/**
* Method description
*
*
* @param token
* @param result
*
* @return
*/
private AuthenticationInfo createAuthenticationInfo(
UsernamePasswordToken token, AuthenticationResult result) {
User user = result.getUser();
Collection<String> groups = authenticate(
new String(token.getPassword()), result);
SimplePrincipalCollection collection = new SimplePrincipalCollection();
/*
* the first (primary) principal should be a unique identifier
*/
collection.add(user.getId(), NAME);
collection.add(user, NAME);
collection.add(new GroupNames(groups), NAME);
return new SimpleAuthenticationInfo(collection, token.getPassword());
}
示例12: setAuthProvider
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
@Override
public void setAuthProvider(AuthProvider authProvider) {
if (authProvider instanceof ShiroAuthProviderImpl) {
ShiroAuthProviderImpl shiroAuthProvider = (ShiroAuthProviderImpl)authProvider;
this.vertx = shiroAuthProvider.getVertx();
this.securityManager = shiroAuthProvider.getSecurityManager();
// generate the subject back from the provider
SubjectContext subjectContext = new DefaultSubjectContext();
PrincipalCollection coll = new SimplePrincipalCollection(username, shiroAuthProvider.getRealmName());
subjectContext.setPrincipals(coll);
subject = securityManager.createSubject(subjectContext);
} else {
throw new IllegalArgumentException("Not a ShiroAuthProviderImpl");
}
}
示例13: testSetSubjectManually
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
/**
* Method description
*
*/
@Test
public void testSetSubjectManually()
{
assertFalse(SecurityUtils.getSubject().isAuthenticated());
PrincipalCollection col = new SimplePrincipalCollection("hansolo", "test");
Subject subject =
new Subject.Builder().authenticated(true).principals(col).buildSubject();
rule.setSubject(subject);
assertTrue(SecurityUtils.getSubject().isAuthenticated());
assertEquals("hansolo", SecurityUtils.getSubject().getPrincipal());
}
示例14: getCacheScope
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
/** get cache for application scope */
private ScopedCache<String, V> getCacheScope( K key ) {
PrincipalIdentifier principal;
if ( key instanceof SimplePrincipalCollection) {
SimplePrincipalCollection spc = (SimplePrincipalCollection) key;
principal = (PrincipalIdentifier) spc.getPrimaryPrincipal();
} else {
principal = (PrincipalIdentifier)key;
}
CacheScope scope = new CacheScope(new SimpleId(principal.getApplicationId(), "application"));
ScopedCache<String, V> scopedCache = cacheFactory.getScopedCache(scope);
return scopedCache;
}
示例15: pluginBindRunnablePrivileges
import org.apache.shiro.subject.SimplePrincipalCollection; //导入依赖的package包/类
/**
*
* @param plugin
* @param action
*/
@Override
public Runnable pluginBindRunnablePrivileges(Plugin plugin, Runnable action) {
if (isInited()) {
PrincipalCollection plugPrincipals = new SimplePrincipalCollection(plugin.getClassName(), pluginRealm.getName());
Subject plugSubject = new Subject.Builder().principals(plugPrincipals).authenticated(true).buildSubject();
try {
plugSubject.getSession().setTimeout(-1);
} catch (Exception e) {
LOG.warn("ERROR retrieving session for user \"{}\"", plugin.getClassName());
}
return plugSubject.associateWith(action);
}
return action;
}