/** * Start the process of obtaining an access token by redirecting the browser to the authentication * server * * @param relativePath path relative to context root you want auth server to redirect back to * @param request * @param response * @throws IOException */ public void redirectRelative( String relativePath, HttpServletRequest request, HttpServletResponse response) throws IOException { KeycloakUriBuilder builder = KeycloakUriBuilder.fromUri(request.getRequestURL().toString()) .replacePath(request.getContextPath()) .replaceQuery(null) .path(relativePath); String redirect = builder.toTemplate(); redirect(redirect, request, response); }
/** * Start the process of obtaining an access token by redirecting the browser to the authentication * server * * @param redirectUri full URI you want auth server to redirect back to * @param request * @param response * @throws IOException */ public void redirect(String redirectUri, HttpServletRequest request, HttpServletResponse response) throws IOException { String state = getStateCode(); KeycloakUriBuilder uriBuilder = KeycloakUriBuilder.fromUri(authUrl) .queryParam(OAuth2Constants.CLIENT_ID, clientId) .queryParam(OAuth2Constants.REDIRECT_URI, redirectUri) .queryParam(OAuth2Constants.STATE, state); if (scope != null) { uriBuilder.queryParam(OAuth2Constants.SCOPE, scope); } URI url = uriBuilder.build(); String stateCookiePath = this.stateCookiePath; if (stateCookiePath == null) stateCookiePath = request.getContextPath(); if (stateCookiePath.equals("")) stateCookiePath = "/"; Cookie cookie = new Cookie(stateCookieName, state); cookie.setSecure(isSecure); cookie.setPath(stateCookiePath); response.addCookie(cookie); response.sendRedirect(url.toString()); }