本文整理汇总了Java中org.keycloak.OAuth2Constants类的典型用法代码示例。如果您正苦于以下问题:Java OAuth2Constants类的具体用法?Java OAuth2Constants怎么用?Java OAuth2Constants使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OAuth2Constants类属于org.keycloak包,在下文中一共展示了OAuth2Constants类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: login
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
@Override
public RefreshableKeycloakSecurityContext login(String username, String password) throws VerificationException {
final MultiValueMap<String,String> body = new LinkedMultiValueMap<>();
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
body.set("username", username);
body.set("password", password);
body.set(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD);
AccessTokenResponse response = template.postForObject(keycloakDeployment.getTokenUrl(), new HttpEntity<>(body, headers), AccessTokenResponse.class);
return KeycloakSpringAdapterUtils.createKeycloakSecurityContext(keycloakDeployment, response);
}
示例2: consumeGrantTokenService
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
protected void consumeGrantTokenService(final MultivaluedMap<String, String> mvm) {
boolean isPublic = config.isPublicClient();
String authorization = "";
if (isPublic) { // if client is public access type
mvm.putSingle(OAuth2Constants.CLIENT_ID,
config.getClientId());
} else {
authorization = BasicAuthHelper.createHeader(config.getClientId(),
config.getClientSecret());
}
TokenService client = createTokenService();
AccessTokenResponse response = client.grantToken(config.getRealm(),
authorization,
mvm);
int requestTime = Time.currentTime();
expirationTime = requestTime + response.getExpiresIn();
this.accessTokenResponse = response;
}
示例3: testGrantPublicToken
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
@Test
public void testGrantPublicToken() throws Exception {
when(response.getExpiresIn()).thenReturn(1000l);
when(config.isPublicClient()).thenReturn(true);
this.tokenManager.grantToken();
ArgumentCaptor<MultivaluedMap> mapCaptor = ArgumentCaptor.forClass(MultivaluedMap.class);
verify(this.tokenService,
times(1)).grantToken(eq("realm1"),
anyString(),
mapCaptor.capture());
MultivaluedMap<String, String> mvm = mapCaptor.getValue();
Assert.assertEquals("password",
mvm.get(OAuth2Constants.GRANT_TYPE).get(0));
Assert.assertEquals("user1",
mvm.get("username").get(0));
Assert.assertEquals("password1",
mvm.get("password").get(0));
Assert.assertEquals("clientId1",
mvm.get(OAuth2Constants.CLIENT_ID).get(0));
}
示例4: testRefreshToken
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
@Test
public void testRefreshToken() throws Exception {
when(response.getExpiresIn()).thenReturn(1000l);
this.tokenManager.accessTokenResponse = response;
this.tokenManager.expirationTime = 0;
this.tokenManager.minTokenValidity = 100;
String s = this.tokenManager.getAccessTokenString();
ArgumentCaptor<MultivaluedMap> mapCaptor = ArgumentCaptor.forClass(MultivaluedMap.class);
verify(this.tokenService,
times(1)).grantToken(eq("realm1"),
anyString(),
mapCaptor.capture());
MultivaluedMap<String, String> mvm = mapCaptor.getValue();
Assert.assertEquals("refresh_token",
mvm.get(OAuth2Constants.GRANT_TYPE).get(0));
Assert.assertEquals("refreshToken",
mvm.get("refresh_token").get(0));
}
示例5: testRefreshPublicToken
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
@Test
public void testRefreshPublicToken() throws Exception {
when(response.getExpiresIn()).thenReturn(1000l);
when(config.isPublicClient()).thenReturn(true);
this.tokenManager.accessTokenResponse = response;
this.tokenManager.expirationTime = 0;
this.tokenManager.minTokenValidity = 100;
String s = this.tokenManager.getAccessTokenString();
ArgumentCaptor<MultivaluedMap> mapCaptor = ArgumentCaptor.forClass(MultivaluedMap.class);
verify(this.tokenService,
times(1)).grantToken(eq("realm1"),
anyString(),
mapCaptor.capture());
MultivaluedMap<String, String> mvm = mapCaptor.getValue();
Assert.assertEquals("refresh_token",
mvm.get(OAuth2Constants.GRANT_TYPE).get(0));
Assert.assertEquals("refreshToken",
mvm.get("refresh_token").get(0));
Assert.assertEquals("clientId1",
mvm.get(OAuth2Constants.CLIENT_ID).get(0));
}
示例6: logout
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
private void logout(AccessTokenResponse accessTokenResponse) {
try(CloseableHttpClient client = HttpClientBuilder.create().build()) {
HttpPost post = new HttpPost(KeycloakUriBuilder.fromUri(baseUrl)
.path(ServiceUrlConstants.TOKEN_SERVICE_LOGOUT_PATH).build(LiveOak.LIVEOAK_APP_REALM));
List<NameValuePair> formparams = new ArrayList<>();
formparams.add(new BasicNameValuePair(OAuth2Constants.REFRESH_TOKEN, accessTokenResponse.getRefreshToken()));
formparams.add(new BasicNameValuePair(OAuth2Constants.CLIENT_ID, "liveoak-admin-client"));
UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8");
post.setEntity(form);
client.execute(post);
} catch (IOException e) {
// Not a problem we need to report
}
this.accessTokenResponse = null;
}
示例7: grantTokenParameters
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
/**
* Generate parameters required for initial token grant.
*/
@Nonnull
private MultivaluedMap<String, String> grantTokenParameters()
{
final MultivaluedHashMap<String, String> results = initParameters();
results.put( OAuth2Constants.GRANT_TYPE, Collections.singletonList( _config.getGrantType() ) );
results.put( "username", Collections.singletonList( _config.getUsername() ) );
results.put( "password", Collections.singletonList( _config.getPassword() ) );
return results;
}
示例8: initParameters
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
/**
* Initialize parameters, ensuring clientID is set if required.
*/
@Nonnull
private MultivaluedHashMap<String, String> initParameters()
{
final MultivaluedHashMap<String, String> results = new MultivaluedHashMap<>();
if ( _config.isPublicClient() )
{
results.put( OAuth2Constants.CLIENT_ID, Collections.singletonList( _config.getClientID() ) );
}
return results;
}
示例9: createPasswordConfig
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
@Nonnull
public static KeycloakConfig createPasswordConfig( @Nonnull final String serverUrl,
@Nonnull final String realm,
@Nonnull final String clientID,
@Nonnull final String username,
@Nonnull final String password )
{
return new KeycloakConfig( serverUrl, realm, OAuth2Constants.PASSWORD, clientID, username, password, null );
}
示例10: getAuthUrl
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
public String getAuthUrl(HttpServletRequest servletRequest) {
String redirect = redirectUrl(servletRequest);
String state = UUID.randomUUID().toString();
String authUrl = keycloakDeployment.getAuthUrl().clone()
.queryParam(OAuth2Constants.CLIENT_ID, keycloakDeployment.getResourceName())
.queryParam(OAuth2Constants.REDIRECT_URI, redirect)
.queryParam(OAuth2Constants.STATE, state)
.build().toString();
return authUrl;
}
示例11: grantToken
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
@Override
public void grantToken() {
MultivaluedMap<String, String> mvm = new CaseInsensitiveMap<String>();
mvm.putSingle(OAuth2Constants.GRANT_TYPE,
"password");
mvm.putSingle("username",
config.getUsername());
mvm.putSingle("password",
config.getPassword());
consumeGrantTokenService(mvm);
}
示例12: refreshToken
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
private void refreshToken() {
MultivaluedMap<String, String> mvm = new CaseInsensitiveMap<String>();
mvm.putSingle(OAuth2Constants.GRANT_TYPE,
"refresh_token");
mvm.putSingle("refresh_token",
accessTokenResponse.getRefreshToken());
consumeGrantTokenService(mvm);
}
示例13: doCommenceLogin
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
public HttpResponse doCommenceLogin(StaplerRequest request, StaplerResponse response,
@Header("Referer") final String referer) throws IOException {
request.getSession().setAttribute(REFERER_ATTRIBUTE, referer);
String redirect = redirectUrl(request);
String state = UUID.randomUUID().toString();
String authUrl = getKeycloakDeployment().getAuthUrl().clone()
.queryParam(OAuth2Constants.CLIENT_ID, getKeycloakDeployment().getResourceName())
.queryParam(OAuth2Constants.REDIRECT_URI, redirect).queryParam(OAuth2Constants.STATE, state)
.queryParam(OAuth2Constants.RESPONSE_TYPE, OAuth2Constants.CODE).build().toString();
return new HttpRedirect(authUrl);
}
示例14: accessToken
import org.keycloak.OAuth2Constants; //导入依赖的package包/类
public String accessToken() throws IOException {
if (openConnections.get() == 0 && accessTokenResponse == null) {
synchronized (this) {
if (accessTokenResponse == null) {
// Not connected
int attempts = 0;
boolean intr = false;
try {
while (accessTokenResponse == null && attempts < MAX_RETRIES) {
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
HttpPost post = new HttpPost(KeycloakUriBuilder.fromUri(baseUrl)
.path(ServiceUrlConstants.TOKEN_SERVICE_DIRECT_GRANT_PATH).build(LiveOak.LIVEOAK_APP_REALM));
List<NameValuePair> formparams = new ArrayList<>();
formparams.add(new BasicNameValuePair("username", "liveoak-server"));
String initialPassword = System.getProperty("liveoak.initial.password");
if (initialPassword != null) {
formparams.add(new BasicNameValuePair("password", initialPassword));
} else {
formparams.add(new BasicNameValuePair("password", "password"));
}
formparams.add(new BasicNameValuePair(OAuth2Constants.CLIENT_ID, "liveoak-admin-client"));
UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8");
post.setEntity(form);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (status == 404) {
attempts++;
wait(TIMEOUT);
continue;
} else if (status != 200) {
String json = EntityUtils.toString(entity);
throw new IOException("Bad status: " + status + ", response: " + json);
}
if (entity == null) {
throw new IOException("No Entity");
}
accessTokenResponse = JsonSerialization.readValue(EntityUtils.toString(entity), AccessTokenResponse.class);
} catch (InterruptedException e) {
intr = true;
}
}
} finally {
if (intr) Thread.currentThread().interrupt();
}
}
}
}
openConnections.incrementAndGet();
return accessTokenResponse.getToken();
}