use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class ShiroSecurityContext method getUserPrincipal.
@Override
public Principal getUserPrincipal() {
Principal result;
Subject subject = getSubject();
PrincipalCollection shiroPrincipals = subject.getPrincipals();
if (shiroPrincipals != null) {
result = shiroPrincipals.oneByType(Principal.class);
if (result == null) {
result = new ObjectPrincipal(shiroPrincipals.getPrimaryPrincipal());
}
} else {
result = originalSecurityContext.getUserPrincipal();
}
return result;
}
use of org.apache.shiro.subject.PrincipalCollection in project killbill by killbill.
the class TestKillBillAuth0Realm method testCheckAuth0Connection.
@Test(groups = "external", enabled = false)
public void testCheckAuth0Connection() throws Exception {
// Convenience method to verify your Auth0 connectivity
final Properties props = new Properties();
props.setProperty("org.killbill.security.auth0.url", "https://XXX.us.auth0.com");
props.setProperty("org.killbill.security.auth0.clientId", "YYY");
props.setProperty("org.killbill.security.auth0.clientSecret", "ZZZ");
props.setProperty("org.killbill.security.auth0.apiIdentifier", "WWW");
props.setProperty("org.killbill.security.auth0.databaseConnectionName", "Username-Password-Authentication");
props.setProperty("org.killbill.security.auth0.allowedClockSkew", "2000s");
final ConfigSource customConfigSource = new SimplePropertyConfigSource(props);
final SecurityConfig securityConfig = new ConfigurationObjectFactory(customConfigSource).build(SecurityConfig.class);
final KillBillAuth0Realm auth0Realm = new KillBillAuth0Realm(securityConfig, clock);
final String username = "test@example.com";
final String password = "password";
// Check authentication
final AuthenticationToken token = new UsernamePasswordToken(username, password);
final AuthenticationInfo authenticationInfo = auth0Realm.getAuthenticationInfo(token);
System.out.println(authenticationInfo);
// Check permissions
final PrincipalCollection principals = new SimplePrincipalCollection(username, username);
final AuthorizationInfo authorizationInfo = auth0Realm.doGetAuthorizationInfo(principals);
System.out.println("Roles: " + authorizationInfo.getRoles());
System.out.println("Permissions: " + authorizationInfo.getStringPermissions());
// Check JWT
final Claims claims = auth0Realm.verifyJWT("JWT");
System.out.println("Token claims: " + claims);
}
use of org.apache.shiro.subject.PrincipalCollection in project ff4j by ff4j.
the class ApacheShiroAuthorizationManager method getCurrentUserName.
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("rawtypes")
public String getCurrentUserName() {
String username = "N/A";
Subject subject = SecurityUtils.getSubject();
PrincipalCollection principalCollection = subject.getPrincipals();
if (principalCollection != null && !principalCollection.isEmpty()) {
Collection<Map> principalMaps = subject.getPrincipals().byType(Map.class);
if (CollectionUtils.isEmpty(principalMaps)) {
username = subject.getPrincipal().toString();
} else {
username = (String) principalMaps.iterator().next().get(PRINCIPAL_USERNAME);
}
}
return username;
}
Aggregations