diff --git a/google/README.MD b/google/README.MD new file mode 100644 index 0000000..d776e3a --- /dev/null +++ b/google/README.MD @@ -0,0 +1,33 @@ +# Spring Boot + OAuth2 Google + +Spring Boot +Discord + +## Перед запуском +### Создание OAuth +Для запуска понадобиться обзавестить своим **Google OAuth**. + +1. Переходим в [Google APIs console credentials](https://console.cloud.google.com/apis/credentials?project=silent-fuze-322306) +2. Создаём новый OAuth Client ID + ![](docs/1.png) +3. Заполняем обязательно следующие поля + - **Application type**: `Web application` + - **Name**: `MY-GOOGLE-APP` + - **Authorization callback URL**: `http://127.0.0.1:8080/login/oauth2/code/google` + ![](docs/2.png) +5. Получаем **Client secret/Client ID** + ![](docs/3.png) + +### Настройка Spring +Открываем файл `src/main/resources/application.yml` и указываем там **Client ID** и **Client secrets**: + +```yaml +spring: + security: + oauth2: + client: + registration: + google: + client-id: 704750524716-tnqc2islqao8tobjbrgvtte0fq8o39uv.apps.googleusercontent.com + client-secret: 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 +``` diff --git a/google/build.gradle b/google/build.gradle new file mode 100644 index 0000000..b7fec0f --- /dev/null +++ b/google/build.gradle @@ -0,0 +1,22 @@ +plugins { + id 'org.springframework.boot' version '2.6.6' + id 'io.spring.dependency-management' version '1.0.11.RELEASE' + id 'java' +} + +group = 'example.oauth2' +version = '1.0-SNAPSHOT' +sourceCompatibility = JavaVersion.VERSION_11 + +repositories { + mavenCentral() +} + +dependencies { + implementation('org.springframework.boot:spring-boot-starter-web') + implementation('org.springframework.boot:spring-boot-starter-freemarker') + + implementation('org.springframework.boot:spring-boot-starter-security') + implementation('org.springframework.security:spring-security-oauth2-client') + implementation('org.springframework.security:spring-security-oauth2-jose:5.6.3') +} diff --git a/google/docs/1.png b/google/docs/1.png new file mode 100644 index 0000000..b23b7fa Binary files /dev/null and b/google/docs/1.png differ diff --git a/google/docs/2.png b/google/docs/2.png new file mode 100644 index 0000000..824f074 Binary files /dev/null and b/google/docs/2.png differ diff --git a/google/docs/3.png b/google/docs/3.png new file mode 100644 index 0000000..c7cf2fe Binary files /dev/null and b/google/docs/3.png differ diff --git a/google/docs/google.svg b/google/docs/google.svg new file mode 100644 index 0000000..c652a5a --- /dev/null +++ b/google/docs/google.svg @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/google/src/main/java/example/oauth2/google/ApplicationGoogle.java b/google/src/main/java/example/oauth2/google/ApplicationGoogle.java new file mode 100644 index 0000000..cf63bb7 --- /dev/null +++ b/google/src/main/java/example/oauth2/google/ApplicationGoogle.java @@ -0,0 +1,13 @@ +package example.oauth2.google; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ApplicationGoogle { + + public static void main(String[] args) { + SpringApplication.run(ApplicationGoogle.class, args); + } + +} diff --git a/google/src/main/java/example/oauth2/google/config/MvcConfig.java b/google/src/main/java/example/oauth2/google/config/MvcConfig.java new file mode 100644 index 0000000..424b397 --- /dev/null +++ b/google/src/main/java/example/oauth2/google/config/MvcConfig.java @@ -0,0 +1,15 @@ +package example.oauth2.google.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class MvcConfig implements WebMvcConfigurer { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("index"); + registry.addViewController("/secret").setViewName("secret"); + } +} diff --git a/google/src/main/java/example/oauth2/google/config/WebSecurityConfig.java b/google/src/main/java/example/oauth2/google/config/WebSecurityConfig.java new file mode 100644 index 0000000..3ffa17c --- /dev/null +++ b/google/src/main/java/example/oauth2/google/config/WebSecurityConfig.java @@ -0,0 +1,24 @@ +package example.oauth2.google.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@EnableWebSecurity +@Configuration +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + //@formatter:off + http + .authorizeRequests() + .antMatchers("/").permitAll() //Доступ разрешен всем пользователей + .anyRequest().authenticated() //Все остальные страницы требуют аутентификации + .and() + .oauth2Login() + ; + //@formatter:on + } +} diff --git a/google/src/main/resources/application.yml b/google/src/main/resources/application.yml new file mode 100644 index 0000000..3b182a8 --- /dev/null +++ b/google/src/main/resources/application.yml @@ -0,0 +1,17 @@ +server: + address: 127.0.0.1 + port: 8080 + +debug: false + +spring: + freemarker: + template-loader-path: classpath:/templates + suffix: .ftlh + security: + oauth2: + client: + registration: + google: + client-id: 704750524716-tnqc2islqao8tobjbrgvtte0fq8o39uv.apps.googleusercontent.com + client-secret: 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 diff --git a/google/src/main/resources/templates/includes/foother.ftlh b/google/src/main/resources/templates/includes/foother.ftlh new file mode 100644 index 0000000..308b1d0 --- /dev/null +++ b/google/src/main/resources/templates/includes/foother.ftlh @@ -0,0 +1,2 @@ + + diff --git a/google/src/main/resources/templates/includes/header.ftlh b/google/src/main/resources/templates/includes/header.ftlh new file mode 100644 index 0000000..a389738 --- /dev/null +++ b/google/src/main/resources/templates/includes/header.ftlh @@ -0,0 +1,9 @@ + + + + Google OAuth2 Example + + + +

Google OAuth2 Example

+
diff --git a/google/src/main/resources/templates/index.ftlh b/google/src/main/resources/templates/index.ftlh new file mode 100644 index 0000000..32580cb --- /dev/null +++ b/google/src/main/resources/templates/index.ftlh @@ -0,0 +1,7 @@ +<#include "includes/header.ftlh"> +

+ Home public page
+ ---> Secret Page <---
+ [ Login Google ] +

+<#include "includes/foother.ftlh"> diff --git a/google/src/main/resources/templates/secret.ftlh b/google/src/main/resources/templates/secret.ftlh new file mode 100644 index 0000000..c54425a --- /dev/null +++ b/google/src/main/resources/templates/secret.ftlh @@ -0,0 +1,3 @@ +<#include "includes/header.ftlh"> +

[TOP SECRET PAGE]

+<#include "includes/foother.ftlh"> diff --git a/settings.gradle b/settings.gradle index bc1372e..6609799 100644 --- a/settings.gradle +++ b/settings.gradle @@ -3,3 +3,4 @@ rootProject.name = 'spring-oauth2-examples' include('discord') include('github') include('twitch') +include('google')