本文整理汇总了Java中org.keycloak.KeycloakSecurityContext类的典型用法代码示例。如果您正苦于以下问题:Java KeycloakSecurityContext类的具体用法?Java KeycloakSecurityContext怎么用?Java KeycloakSecurityContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
KeycloakSecurityContext类属于org.keycloak包,在下文中一共展示了KeycloakSecurityContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAccountUri
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
public String getAccountUri(HttpServletRequest req) {
KeycloakSecurityContext session = getSession(req);
String baseUrl = getAuthServerBaseUrl(req);
String realm = session.getRealm();
return KeycloakUriBuilder.fromUri(baseUrl).path(ServiceUrlConstants.ACCOUNT_SERVICE_PATH)
.queryParam("referrer", "app-jsp")
.queryParam("referrer_uri", getReferrerUri(req)).build(realm).toString();
}
示例2: create
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@Override
public <T, C> Action<T, C> create(final Action<T, C> action) {
return new Action<T, C>() {
@Override
public T call(HttpServerExchange exchange, C context) throws Exception {
if (exchange == null) {
return action.call(exchange, context);
}
KeycloakSecurityContext c = exchange.getAttachment(OIDCUndertowHttpFacade.KEYCLOAK_SECURITY_CONTEXT_KEY);
KeycloakSecurityContextAssociation.associate(c);
try {
return action.call(exchange, context);
} finally {
KeycloakSecurityContextAssociation.disassociate();
}
}
};
}
示例3: handleDeployment
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@Override
public void handleDeployment(DeploymentInfo info, ServletContext context) {
info.addThreadSetupAction(new KeycloakThreadSetupHandler());
info.addInnerHandlerChainWrapper(next -> exchange -> {
KeycloakSecurityContext c = exchange.getAttachment(OIDCUndertowHttpFacade.KEYCLOAK_SECURITY_CONTEXT_KEY);
if (c != null) {
KeycloakSecurityContextAssociation.associate(c);
}
try {
next.handleRequest(exchange);
} finally {
KeycloakSecurityContextAssociation.disassociate();
}
});
}
示例4: handle
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@Override
public void handle(Context context) {
OIDCFilterSessionStore.SerializableKeycloakAccount account = context.removeSession(KeycloakAccount.class.getName());
if (account != null) {
// Logout of the Keycloak server
KeycloakDeployment deployment = account.getKeycloakSecurityContext().getDeployment();
account.getKeycloakSecurityContext().logout(deployment);
}
// Cleanup the session of Keycloak metadata
context.removeSession(KeycloakSecurityContext.class.getName());
context.removeSession(REDIRECT_URI);
context.removeSession(SAVED_METHOD);
context.removeSession(SAVED_HEADERS);
context.removeSession(SAVED_BODY);
super.handle(context);
}
示例5: basicWorkflow
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@Test
public void basicWorkflow()
throws Exception
{
final SimpleAuthService authService = mock( SimpleAuthService.class );
final SimpleSecureSessionManager sm = new SimpleSecureSessionManager();
setField( sm, authService );
final String userID = ValueUtil.randomString();
final OidcKeycloakAccount account = mock( OidcKeycloakAccount.class );
final AccessToken token = new AccessToken();
setField( token, userID );
final KeycloakSecurityContext context =
new KeycloakSecurityContext( ValueUtil.randomString(), token, ValueUtil.randomString(), new IDToken() );
when( account.getKeycloakSecurityContext() ).thenReturn( context );
when( authService.findAccount() ).thenReturn( account );
final SessionInfo sessionInfo = sm.createSession();
assertNotNull( sessionInfo );
assertEquals( sessionInfo.getUserID(), userID );
}
示例6: generateToken
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
/**
* @see io.apiman.manager.ui.server.auth.ITokenGenerator#generateToken(javax.servlet.http.HttpServletRequest)
*/
@Override
public BearerTokenCredentialsBean generateToken(HttpServletRequest request) {
BearerTokenCredentialsBean bean = new BearerTokenCredentialsBean();
KeycloakSecurityContext session = (KeycloakSecurityContext) request.getAttribute(KeycloakSecurityContext.class.getName());
if (session != null) {
bean.setToken(session.getTokenString());
int nowInSeconds = getCurrentTime();
int expiresInSeconds = session.getToken().getExpiration();
if (expiresInSeconds <= nowInSeconds) {
bean.setRefreshPeriod(1);
} else {
bean.setRefreshPeriod(expiresInSeconds - nowInSeconds);
}
} else {
bean.setToken("LOGGED_OUT"); //$NON-NLS-1$
bean.setRefreshPeriod(30);
}
return bean;
}
示例7: getAccountUri
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
public String getAccountUri(HttpServletRequest req) {
KeycloakSecurityContext session = getSession(req);
String baseUrl = getAuthServerBaseUrl(req);
String realm = session.getRealm();
return KeycloakUriBuilder.fromUri(baseUrl).path(ServiceUrlConstants.ACCOUNT_SERVICE_PATH)
.queryParam("referrer", "app-profile-jsp")
.queryParam("referrer_uri", getReferrerUri(req)).build(realm).toString();
}
示例8: greetUser
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@GetMapping("/user/greet")
String greetUser(@AuthenticationPrincipal Principal principal, HttpServletRequest request) {
KeycloakSecurityContext keycloakSecurityContext = (KeycloakSecurityContext) request.getAttribute(KeycloakSecurityContext.class.getName());
log.info("User Greeting for {} {}", principal.getName(), keycloakSecurityContext.getToken().getPreferredUsername());
return String.format("Hello User %s %s %s", principal.getName(), keycloakSecurityContext.getToken().getPreferredUsername(), LocalDateTime.now());
}
示例9: greetAdmin
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@GetMapping("/admin/greet")
String greetAdmin(@AuthenticationPrincipal Principal principal, HttpServletRequest request) {
KeycloakSecurityContext keycloakSecurityContext = (KeycloakSecurityContext) request.getAttribute(KeycloakSecurityContext.class.getName());
log.info("Admin Greeting for {} {}", principal.getName(), keycloakSecurityContext.getToken().getPreferredUsername());
return String.format("Hello Admin %s %s %s", principal.getName(), keycloakSecurityContext.getToken().getPreferredUsername(), LocalDateTime.now());
}
示例10: holaSecured
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@GET
@Path("/hola-secured")
@Produces("text/plain")
@ApiOperation("Returns a message that is only available for authenticated users")
public String holaSecured() {
// this will set the user id as userName
String userName = securityContext.getUserPrincipal().getName();
if (securityContext.getUserPrincipal() instanceof KeycloakPrincipal) {
@SuppressWarnings("unchecked")
KeycloakPrincipal<KeycloakSecurityContext> kp = (KeycloakPrincipal<KeycloakSecurityContext>) securityContext.getUserPrincipal();
// this is how to get the real userName (or rather the login name)
userName = kp.getKeycloakSecurityContext().getToken().getName();
}
return "This is a Secured resource. You are logged as " + userName;
}
示例11: authenticate
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) authentication;
KeycloakSecurityContext ksc = (KeycloakSecurityContext)token.getCredentials();
Map<String, Object> otherClaims = ksc.getToken().getOtherClaims();
Organization org = null;
if (otherClaims.containsKey(AccessControlUtil.ORG_PROPERTY_NAME)) {
String orgMrn = (String) otherClaims.get(AccessControlUtil.ORG_PROPERTY_NAME);
logger.debug("Found org mrn: " + orgMrn);
org = organizationService.getOrganizationByMrnNoFilter(orgMrn);
if (org != null) {
if (otherClaims.containsKey(AccessControlUtil.PERMISSIONS_PROPERTY_NAME)) {
ArrayList<String> usersPermissions = (ArrayList<String>) otherClaims.get(AccessControlUtil.PERMISSIONS_PROPERTY_NAME);
for (String permission : usersPermissions) {
String[] auths = permission.split(",");
for (String auth : auths) {
logger.debug("Looking up role: " + auth);
List<Role> foundRoles = roleService.getRolesByIdOrganizationAndPermission(org.getId(), auth);
if (foundRoles != null) {
for (Role foundRole : foundRoles) {
logger.debug("Replacing role " + auth + ", with: " + foundRole.getRoleName());
grantedAuthorities.add(new KeycloakRole(foundRole.getRoleName()));
}
}
}
}
}
if (grantedAuthorities.isEmpty()) {
grantedAuthorities.add(new KeycloakRole("ROLE_USER"));
}
}
}
return new KeycloakAuthenticationToken(token.getAccount(), mapAuthorities(grantedAuthorities));
}
开发者ID:MaritimeConnectivityPlatform,项目名称:IdentityRegistry,代码行数:38,代码来源:MCKeycloakAuthenticationProvider.java
示例12: onStartWithServer
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@Override
public void onStartWithServer(ExecutionContext<HttpClientRequest<ByteBuf>> context, ExecutionInfo info) throws AbortExecutionException {
KeycloakSecurityContext securityContext = KeycloakSecurityContextAssociation.get();
if (securityContext != null) {
HttpClientRequest<ByteBuf> request = context.getRequest();
request.withHeader("Authorization", "Bearer " + securityContext.getTokenString());
context.put(KeycloakSecurityContextAssociation.class.getName(), securityContext);
} else {
KeycloakSecurityContextAssociation.disassociate();
}
}
示例13: onExceptionWithServer
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@Override
public void onExceptionWithServer(ExecutionContext<HttpClientRequest<ByteBuf>> context, Throwable exception, ExecutionInfo info) {
KeycloakSecurityContext securityContext = (KeycloakSecurityContext) context.get(KeycloakSecurityContextAssociation.class.getName());
if (securityContext != null) {
KeycloakSecurityContextAssociation.associate(securityContext);
} else {
KeycloakSecurityContextAssociation.disassociate();
}
}
示例14: onExecutionSuccess
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@Override
public void onExecutionSuccess(ExecutionContext<HttpClientRequest<ByteBuf>> context, HttpClientResponse<ByteBuf> response, ExecutionInfo info) {
KeycloakSecurityContext securityContext = (KeycloakSecurityContext) context.get(KeycloakSecurityContextAssociation.class.getName());
if (securityContext != null) {
KeycloakSecurityContextAssociation.associate(securityContext);
} else {
KeycloakSecurityContextAssociation.disassociate();
}
}
示例15: onExecutionFailed
import org.keycloak.KeycloakSecurityContext; //导入依赖的package包/类
@Override
public void onExecutionFailed(ExecutionContext<HttpClientRequest<ByteBuf>> context, Throwable finalException, ExecutionInfo info) {
KeycloakSecurityContext securityContext = (KeycloakSecurityContext) context.get(KeycloakSecurityContextAssociation.class.getName());
if (securityContext != null) {
KeycloakSecurityContextAssociation.associate(securityContext);
} else {
KeycloakSecurityContextAssociation.disassociate();
}
}