Skip to content

Commit 9a11673

Browse files
authored
Add Oauth2 sample for Java 11 (GoogleCloudPlatform#1383)
* Add Oauth2 sample for Java 11 * Update comments * Update README with install instructionons * Update pom * remove systempath dependency * Add license and update path to dependency * Update name * Fix name * Fix license * Update dep * fix parent
1 parent 9f7d910 commit 9a11673

File tree

15 files changed

+586
-11
lines changed

15 files changed

+586
-11
lines changed

.kokoro/tests/run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fi
6767
# Package local jetty dependency for Java11 samples
6868
if [[ "$JAVA_VERSION" == "11" ]]; then
6969
cd appengine-java11/appengine-simple-jetty-main/
70-
mvn package
70+
mvn install
7171
cd ../../
7272
fi
7373

appengine-java11/gaeinfo/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ Copyright 2019 Google Inc.
4242
<!-- Dependency from folder `../appengine-simple-jetty-main` -->
4343
<!-- See README for instructions for more information -->
4444
<dependency>
45-
<groupId>com.google.appengine.demo</groupId>
45+
<groupId>com.example.appengine.demo</groupId>
4646
<artifactId>simple-jetty-main</artifactId>
4747
<version>1</version>
48-
<scope>system</scope>
49-
<systemPath>${project.basedir}/../appengine-simple-jetty-main/target/simple-jetty-main-1.jar</systemPath>
48+
<scope>provided</scope>
5049
</dependency>
5150

5251
<dependency>

appengine-java11/guestbook-cloud-firestore/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@
3131
<!-- Dependency from folder `../appengine-simple-jetty-main` -->
3232
<!-- See README for instructions for more information -->
3333
<dependency>
34-
<groupId>com.google.appengine.demo</groupId>
34+
<groupId>com.example.appengine.demo</groupId>
3535
<artifactId>simple-jetty-main</artifactId>
3636
<version>1</version>
37-
<scope>system</scope>
38-
<systemPath>${project.basedir}/../appengine-simple-jetty-main/target/simple-jetty-main-1.jar</systemPath>
37+
<scope>provided</scope>
3938
</dependency>
4039

4140
<dependency>

appengine-java11/guestbook-cloud-firestore/src/main/appengine/app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313

1414
runtime: java11
1515
instance_class: F2
16-
entrypoint: 'java -cp * com.google.appengine.demo.jettymain.Main guestbook.war'
16+
entrypoint: 'java -cp * com.example.appengine.demo.jettymain.Main guestbook.war'

appengine-java11/oauth2/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Google OAuth 2.0 on Google App Engine Standard with Java 11
2+
3+
This sample shows how to implement an OAuth 2.0 flow using the
4+
[Google OAuth Client Library for Java](client) to access protected data stored
5+
on Google services. OAuth 2.0 is a standard specification for allowing end
6+
users to securely authorize a client application to access protected
7+
server-side resources.
8+
9+
[client]: https://developers.google.com/api-client-library/java/google-api-java-client/oauth2
10+
11+
## Setup you Google Cloud Project
12+
13+
- Make sure [`gcloud`](https://cloud.google.com/sdk/docs/) is installed and initialized:
14+
```
15+
gcloud init
16+
```
17+
- If this is the first time you are creating an App Engine project
18+
```
19+
gcloud app create
20+
```
21+
22+
## Setup the Sample App
23+
24+
- Copy the sample apps to your local machine:
25+
```
26+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
27+
```
28+
29+
- Add the [appengine-simple-jetty-main](../README.md#appengine-simple-jetty-main)
30+
Main class to your classpath:
31+
```
32+
cd java-docs-samples/appengine-java11/appengine-simple-jetty-main
33+
mvn install
34+
```
35+
- In the [Cloud Developers Console](https://cloud.google.com/console) >
36+
API Manager > Credentials, create a OAuth Client ID for a Web Application.
37+
You will need to provide an authorized redirect URI
38+
origin: `https://<PROJECT_ID>.appspot.com/oauth2callback`.
39+
40+
- Replace `CLIENT_ID` and `CLIENT_SECRET` with these values in your
41+
[app.yaml](/src/main/appengine/app.yaml)
42+
43+
- Move into the `appengine-java11/oauth2` directory and compile the app:
44+
```
45+
cd ../oauth2
46+
mvn package
47+
```
48+
49+
## Deploy to App Engine Standard
50+
51+
```
52+
mvn clean package appengine:deploy -Dapp.deploy.projectId=<your-project-id>
53+
```
54+
55+
To view your app, use command:
56+
```
57+
gcloud app browse
58+
```
59+
Or navigate to http://<project-id>.appspot.com URL.

appengine-java11/oauth2/pom.xml

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
20+
<modelVersion>4.0.0</modelVersion>
21+
<packaging>war</packaging>
22+
<version>1.0-SNAPSHOT</version>
23+
<groupId>com.example.appengine</groupId>
24+
<artifactId>oauth2-sample</artifactId>
25+
26+
<!--
27+
The parent pom defines common style checks and testing strategies for our samples.
28+
Removing or replacing it should not affect the execution of the samples in anyway.
29+
-->
30+
<parent>
31+
<groupId>com.google.cloud.samples</groupId>
32+
<artifactId>shared-configuration</artifactId>
33+
<version>1.0.11</version>
34+
</parent>
35+
36+
<properties>
37+
<maven.compiler.source>11</maven.compiler.source>
38+
<maven.compiler.target>11</maven.compiler.target>
39+
</properties>
40+
41+
<!-- All dependencies are scoped as provided.
42+
Dependent-jars are created and added to
43+
${project.build.directory}/appengine-staging -->
44+
<dependencies>
45+
<!-- Dependency from folder `../appengine-simple-jetty-main` -->
46+
<!-- See README for instructions for more information -->
47+
<dependency>
48+
<groupId>com.example.appengine.demo</groupId>
49+
<artifactId>simple-jetty-main</artifactId>
50+
<version>1</version>
51+
<scope>provided</scope>
52+
</dependency>
53+
54+
<!-- OAuth 2.0 packages -->
55+
<dependency>
56+
<groupId>com.google.oauth-client</groupId>
57+
<artifactId>google-oauth-client</artifactId>
58+
<version>1.28.0</version>
59+
<scope>provided</scope>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>com.google.oauth-client</groupId>
64+
<artifactId>google-oauth-client-servlet</artifactId>
65+
<version>1.28.0</version>
66+
<scope>provided</scope>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>com.google.apis</groupId>
71+
<artifactId>google-api-services-oauth2</artifactId>
72+
<version>v2-rev131-1.23.0</version>
73+
<scope>provided</scope>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>javax.servlet</groupId>
78+
<artifactId>javax.servlet-api</artifactId>
79+
<version>4.0.1</version>
80+
<type>jar</type>
81+
<scope>provided</scope>
82+
</dependency>
83+
84+
<dependency>
85+
<groupId>jstl</groupId>
86+
<artifactId>jstl</artifactId>
87+
<version>1.2</version>
88+
<scope>provided</scope>
89+
</dependency>
90+
91+
</dependencies>
92+
93+
<build>
94+
<finalName>oauth2</finalName>
95+
<plugins>
96+
<plugin>
97+
<groupId>com.google.cloud.tools</groupId>
98+
<artifactId>appengine-maven-plugin</artifactId>
99+
<version>2.0.0-rc5</version>
100+
<configuration>
101+
<version>oauth2</version>
102+
</configuration>
103+
</plugin>
104+
105+
<plugin>
106+
<groupId>org.eclipse.jetty</groupId>
107+
<artifactId>jetty-maven-plugin</artifactId>
108+
<version>9.4.2.v20170220</version>
109+
</plugin>
110+
111+
<plugin>
112+
<groupId>org.apache.maven.plugins</groupId>
113+
<artifactId>maven-dependency-plugin</artifactId>
114+
<version>3.1.1</version>
115+
<executions>
116+
<execution>
117+
<id>copy</id>
118+
<phase>prepare-package</phase>
119+
<goals>
120+
<goal>copy-dependencies</goal>
121+
</goals>
122+
<configuration>
123+
<outputDirectory>
124+
${project.build.directory}/appengine-staging
125+
</outputDirectory>
126+
</configuration>
127+
</execution>
128+
</executions>
129+
</plugin>
130+
</plugins>
131+
</build>
132+
133+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
runtime: java11
16+
instance_class: F2
17+
entrypoint: 'java -cp * com.example.appengine.demo.jettymain.Main oauth2.war'
18+
19+
env_variables:
20+
CLIENT_ID: '<YOUR_CLIENT_ID>'
21+
CLIENT_SECRET: '<YOUR_CLIENT_SECRET>'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine;
18+
19+
import java.io.IOException;
20+
import javax.servlet.annotation.WebServlet;
21+
import javax.servlet.http.HttpServlet;
22+
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletResponse;
24+
25+
@SuppressWarnings("serial")
26+
@WebServlet(urlPatterns = "/logout")
27+
public class LogoutServlet extends HttpServlet {
28+
29+
@Override
30+
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
31+
req.getSession().invalidate();
32+
resp.sendRedirect("/");
33+
}
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine;
18+
19+
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
20+
import com.google.api.client.extensions.servlet.auth.oauth2.AbstractAuthorizationCodeServlet;
21+
import java.io.IOException;
22+
import javax.servlet.ServletException;
23+
import javax.servlet.annotation.WebServlet;
24+
import javax.servlet.http.HttpServletRequest;
25+
import javax.servlet.http.HttpServletResponse;
26+
27+
@SuppressWarnings("serial")
28+
@WebServlet(urlPatterns = "/login")
29+
public class Oauth2AuthorizationCodeServlet extends AbstractAuthorizationCodeServlet {
30+
31+
@Override
32+
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
33+
throws IOException, ServletException {
34+
resp.sendRedirect("/");
35+
}
36+
37+
@Override
38+
protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
39+
return Utils.getRedirectUri(req);
40+
}
41+
42+
@Override
43+
protected AuthorizationCodeFlow initializeFlow() throws IOException {
44+
return Utils.newFlow();
45+
}
46+
47+
@Override
48+
protected String getUserId(HttpServletRequest req) throws ServletException, IOException {
49+
return Utils.getUserId(req);
50+
}
51+
}

0 commit comments

Comments
 (0)