add google oauth2
This commit is contained in:
33
google/README.MD
Normal file
33
google/README.MD
Normal file
@@ -0,0 +1,33 @@
|
||||
# Spring Boot + OAuth2 Google
|
||||
|
||||
<img alt="Spring Boot" width="auto" height="100" src="../docs/spring-boot.svg">
|
||||
<img alt="Discord" width="auto" height="100" src="docs/google.svg">
|
||||
|
||||
## Перед запуском
|
||||
### Создание OAuth
|
||||
Для запуска понадобиться обзавестить своим **Google OAuth**.
|
||||
|
||||
1. Переходим в [Google APIs console credentials](https://console.cloud.google.com/apis/credentials?project=silent-fuze-322306)
|
||||
2. Создаём новый OAuth Client ID
|
||||

|
||||
3. Заполняем обязательно следующие поля
|
||||
- **Application type**: `Web application`
|
||||
- **Homepage URL**: `http://127.0.0.1:8080`
|
||||
- **Authorization callback URL**: `http://127.0.0.1:8080/login/oauth2/code/google`
|
||||

|
||||
5. Получаем **Client secret/Client ID**
|
||||

|
||||
|
||||
### Настройка 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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
|
||||
```
|
||||
22
google/build.gradle
Normal file
22
google/build.gradle
Normal file
@@ -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')
|
||||
}
|
||||
BIN
google/docs/1.png
Normal file
BIN
google/docs/1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 47 KiB |
BIN
google/docs/2.png
Normal file
BIN
google/docs/2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 42 KiB |
BIN
google/docs/3.png
Normal file
BIN
google/docs/3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
9
google/docs/google.svg
Normal file
9
google/docs/google.svg
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<svg viewBox="0 0 24 24" width="24" height="24" xmlns="http://www.w3.org/2000/svg">
|
||||
<g transform="matrix(1, 0, 0, 1, 27.009001, -39.238998)">
|
||||
<path fill="#4285F4" d="M -3.264 51.509 C -3.264 50.719 -3.334 49.969 -3.454 49.239 L -14.754 49.239 L -14.754 53.749 L -8.284 53.749 C -8.574 55.229 -9.424 56.479 -10.684 57.329 L -10.684 60.329 L -6.824 60.329 C -4.564 58.239 -3.264 55.159 -3.264 51.509 Z"/>
|
||||
<path fill="#34A853" d="M -14.754 63.239 C -11.514 63.239 -8.804 62.159 -6.824 60.329 L -10.684 57.329 C -11.764 58.049 -13.134 58.489 -14.754 58.489 C -17.884 58.489 -20.534 56.379 -21.484 53.529 L -25.464 53.529 L -25.464 56.619 C -23.494 60.539 -19.444 63.239 -14.754 63.239 Z"/>
|
||||
<path fill="#FBBC05" d="M -21.484 53.529 C -21.734 52.809 -21.864 52.039 -21.864 51.239 C -21.864 50.439 -21.724 49.669 -21.484 48.949 L -21.484 45.859 L -25.464 45.859 C -26.284 47.479 -26.754 49.299 -26.754 51.239 C -26.754 53.179 -26.284 54.999 -25.464 56.619 L -21.484 53.529 Z"/>
|
||||
<path fill="#EA4335" d="M -14.754 43.989 C -12.984 43.989 -11.404 44.599 -10.154 45.789 L -6.734 42.369 C -8.804 40.429 -11.514 39.239 -14.754 39.239 C -19.444 39.239 -23.494 41.939 -25.464 45.859 L -21.484 48.949 C -20.534 46.099 -17.884 43.989 -14.754 43.989 Z"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
17
google/src/main/resources/application.yml
Normal file
17
google/src/main/resources/application.yml
Normal file
@@ -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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
|
||||
@@ -0,0 +1,2 @@
|
||||
</body>
|
||||
</html>
|
||||
9
google/src/main/resources/templates/includes/header.ftlh
Normal file
9
google/src/main/resources/templates/includes/header.ftlh
Normal file
@@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<title>Google OAuth2 Example</title>
|
||||
<link rel="icon" href="data:;base64,=">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Google OAuth2 Example</h1>
|
||||
<hr>
|
||||
7
google/src/main/resources/templates/index.ftlh
Normal file
7
google/src/main/resources/templates/index.ftlh
Normal file
@@ -0,0 +1,7 @@
|
||||
<#include "includes/header.ftlh">
|
||||
<p>
|
||||
Home public page<br>
|
||||
---> <a href="/secret">Secret Page</a> <---<br>
|
||||
[ <a href="/oauth2/authorization/google">Login Google</a> ]
|
||||
</p>
|
||||
<#include "includes/foother.ftlh">
|
||||
3
google/src/main/resources/templates/secret.ftlh
Normal file
3
google/src/main/resources/templates/secret.ftlh
Normal file
@@ -0,0 +1,3 @@
|
||||
<#include "includes/header.ftlh">
|
||||
<p style="color: red">[TOP SECRET PAGE]</p>
|
||||
<#include "includes/foother.ftlh">
|
||||
Reference in New Issue
Block a user