Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions examples/spring-boot-oauth-u2m-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<version>3.5.12</version>
<relativePath></relativePath>
</parent>

Expand All @@ -17,8 +17,11 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<!-- Override transitive dependency version to fix known CVE.
This can likely be removed when upgrading to a newer Spring Boot version. -->
<jackson-bom.version>2.21.2</jackson-bom.version>
</properties>

<dependencies>
Expand All @@ -39,9 +42,5 @@
<artifactId>databricks-sdk-java</artifactId>
<version>0.103.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.databricks.sdk.core.commons.CommonsHttpClient;
import com.databricks.sdk.core.http.HttpClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
Expand All @@ -23,13 +21,6 @@ public HttpClient getHttpClient() {
return new CommonsHttpClient.Builder().withTimeoutSeconds(30).build();
}

@Bean
public ObjectMapper getObjectMapper() {
ObjectMapper m = new ObjectMapper();
m.registerModule(new JavaTimeModule());
return m;
}

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import com.databricks.sdk.core.http.HttpClient;
import com.databricks.sdk.core.oauth.Consent;
import com.databricks.sdk.core.oauth.OAuthClient;
import com.databricks.sdk.core.oauth.OpenIDConnectEndpoints;
import com.databricks.sdk.core.oauth.SessionCredentials;
import com.databricks.sdk.service.compute.ClusterDetails;
import com.databricks.sdk.service.compute.ListClustersRequest;
import com.databricks.sdk.service.oauth2.CreateCustomAppIntegration;
import com.databricks.sdk.service.oauth2.CreateCustomAppIntegrationOutput;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
Expand All @@ -18,7 +18,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
Expand All @@ -31,9 +31,6 @@ public class RootController {
@Autowired
private HttpClient hc;

@Autowired
private ObjectMapper mapper;

// Initialized by initializeApp(). This should be initialized in a more Spring-friendly way.
private OAuthClient client;
// Initialized by callback(). This should be initialized in a more Spring-friendly way.
Expand All @@ -47,16 +44,13 @@ private String getRedirectUrl() {
}

@GetMapping("/")
public String index(HttpSession session, Model model) throws JsonProcessingException {
public String index(HttpSession session, Model model) {
if (client != null) {
model.addAttribute("clientId", client.getClientId());
model.addAttribute("clientSecret", client.getClientSecret());
model.addAttribute("hostname", client.getHost());
}
SessionCredentials sessionCreds = (SessionCredentials) session.getAttribute("sessionCreds");
if (sessionCreds != null) {
model.addAttribute("sessionCreds", mapper.writeValueAsString(sessionCreds.getToken()));
}
model.addAttribute("authenticated", session.getAttribute("sessionCreds") != null);
return "index";
}

Expand All @@ -70,12 +64,16 @@ public String initializeApp(
@RequestParam(name="client_id") String clientId,
@RequestParam(name="client_secret") String clientSecret,
@RequestParam(name="hostname") String hostname) throws IOException {
DatabricksConfig config = new DatabricksConfig().setHost(hostname).setHttpClient(hc).resolve();
OpenIDConnectEndpoints oidcEndpoints = config.getDatabricksOidcEndpoints();
client = new OAuthClient.Builder()
.withClientId(clientId)
.withClientSecret(clientSecret)
.withHost(hostname)
.withRedirectUrl(getRedirectUrl())
.withHttpClient(hc)
.withOpenIDConnectEndpoints(oidcEndpoints)
.withScopes(List.of("all-apis", "offline_access"))
.build();
return "redirect:/";
}
Expand Down Expand Up @@ -104,13 +102,15 @@ public String makeNewApp(
.setHttpClient(hc);
AccountClient account = new AccountClient(c);
CreateCustomAppIntegrationOutput result = account.customAppIntegration().create(
"java-sdk-demo", Collections.singletonList(getRedirectUrl()));
new CreateCustomAppIntegration()
.setName("java-sdk-demo")
.setRedirectUrls(Collections.singletonList(getRedirectUrl())));

return initializeApp(result.getClientId(), result.getClientSecret(), hostname);
}

@GetMapping("/authenticate")
public String authenticate(HttpSession session, Model model) throws MalformedURLException, JsonProcessingException {
public String authenticate(HttpSession session, Model model) throws MalformedURLException {
if (client == null) {
model.addAttribute("authError", "Client is not yet initialized. Please login first.");
return index(session, model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1>Consent</h1>
<div th:if="${authError != null}">
<span th:text="${authError}"></span>
</div>
<p>session credentials: <span th:text="${sessionCreds}">Not authenticated</span></p>
<p>status: <span th:if="${authenticated}">Authenticated</span><span th:unless="${authenticated}">Not authenticated</span></p>
<h1>APIs</h1>
<a href="/list-clusters">List clusters</a>
</body>
Expand Down