добавлен пример для GitHub
@@ -1,6 +1,6 @@
|
|||||||
# Spring Boot + OAuth2 Discord
|
# Spring Boot + OAuth2 Discord
|
||||||
|
|
||||||
<img alt="Spring Boot" width="auto" height="100" src="docs/spring-boot.svg">
|
<img alt="Spring Boot" width="auto" height="100" src="../docs/spring-boot.svg">
|
||||||
<img alt="Discord" width="auto" height="100" src="docs/discord-app.png">
|
<img alt="Discord" width="auto" height="100" src="docs/discord-app.png">
|
||||||
|
|
||||||
## Перед запуском
|
## Перед запуском
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 157.8 141.6"
|
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg"
|
||||||
style="enable-background:new 0 0 157.8 141.6;" xml:space="preserve">
|
x="0px" y="0px" viewBox="0 0 157.8 141.6"
|
||||||
|
style="enable-background:new 0 0 157.8 141.6;"
|
||||||
|
xml:space="preserve">
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
.st0{fill:#6DB33F;}
|
.st0{fill:#6DB33F;}
|
||||||
.st1{fill:#FFFFFF;}
|
.st1{fill:#FFFFFF;}
|
||||||
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 2.0 KiB |
33
github/README.MD
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# Spring Boot + OAuth2 GitHub
|
||||||
|
|
||||||
|
<img alt="Spring Boot" width="auto" height="100" src="../docs/spring-boot.svg">
|
||||||
|
<img alt="Discord" width="auto" height="100" src="docs/github-white.svg">
|
||||||
|
|
||||||
|
## Перед запуском
|
||||||
|
### Создание OAuth App
|
||||||
|
Для запуска понадобиться обзавестить своим **GitHub OAuth App**.
|
||||||
|
|
||||||
|
1. Переходим в [Developer settings](https://github.com/settings/developers)
|
||||||
|
2. Создём новое OAuth приложение
|
||||||
|

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

|
||||||
|
4. Сгенерировать новый **Client secrets**
|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
### Настройка Spring
|
||||||
|
Открываем файл `src/main/resources/application.yml` и указываем там **Client ID** и **Client secrets**:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
spring:
|
||||||
|
security:
|
||||||
|
oauth2:
|
||||||
|
client:
|
||||||
|
registration:
|
||||||
|
github:
|
||||||
|
client-id: f9bbf16cc3a93663282f
|
||||||
|
client-secret: 16f203668e25bfe44513d2a7b4925d62bb100783
|
||||||
|
```
|
||||||
21
github/build.gradle
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
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-thymeleaf')
|
||||||
|
|
||||||
|
implementation('org.springframework.boot:spring-boot-starter-security')
|
||||||
|
implementation('org.springframework.security:spring-security-oauth2-client')
|
||||||
|
}
|
||||||
BIN
github/docs/1.png
Normal file
|
After Width: | Height: | Size: 29 KiB |
BIN
github/docs/2.png
Normal file
|
After Width: | Height: | Size: 77 KiB |
BIN
github/docs/3.png
Normal file
|
After Width: | Height: | Size: 20 KiB |
BIN
github/docs/4.png
Normal file
|
After Width: | Height: | Size: 47 KiB |
17
github/docs/github-white.svg
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<svg version="1.1" id="svg5" xmlns="http://www.w3.org/2000/svg"
|
||||||
|
x="0px" y="0px" viewBox="0 0 140.75667 141.04317"
|
||||||
|
width="140.75667mm" height="141.04317mm">
|
||||||
|
<style type="text/css">
|
||||||
|
.p1{fill:#000000;}
|
||||||
|
.p2{fill:#ffffff;}
|
||||||
|
</style>
|
||||||
|
<g>
|
||||||
|
<path class="p1"
|
||||||
|
d="m 63.960089,141.06136 c -2.07869,-0.20392 -5.071416,-0.64168 -7.154468,-1.04652 -0.970139,-0.18854 -2.002014,-0.3882 -2.293055,-0.44368 -0.291042,-0.0555 -0.727604,-0.1392 -0.970139,-0.18604 -1.185242,-0.22891 -5.104777,-1.45545 -7.64973,-2.39382 C 33.085168,132.2689 21.64106,123.63252 13.666217,112.67138 5.9643529,102.08545 1.8488529,91.600782 0.23569092,78.455712 c -0.35497003,-2.89252 -0.29645703,-13.23614 0.0913,-16.13959 C 2.0286439,49.574472 6.1144219,39.187672 13.406029,29.06682 17.662696,23.15851 23.531999,17.40801 29.72993,13.07335 33.925119,10.13934 39.313878,7.13172 43.664651,5.29598 46.798889,3.97354 53.768984,1.85733 56.276456,1.46688 62.544289,0.49088003 66.268899,0.13904 70.333099,0.13904 c 6.18077,0 14.38289,1.1476 19.70645,2.75722 4.63963,1.40283 7.80336,2.63828 11.657041,4.55215 7.02952,3.4911 12.98272,7.77249 18.52315,13.32137 4.38782,4.39451 7.59189,8.50765 10.7915,13.853292 8.72598,14.57865 11.6836,31.81972 8.50267,49.56528 -2.39549,13.36385 -9.31815,26.548638 -19.11567,36.407428 -5.35457,5.38805 -11.52016,9.82876 -18.48145,13.31109 -7.591851,3.79776 -13.998531,5.70816 -23.238951,6.92961 -2.40903,0.31844 -12.2337,0.46855 -14.71775,0.22488 z"/>
|
||||||
|
</g>
|
||||||
|
<g transform="translate(-39.423133,-67.161128)">
|
||||||
|
<path class="p2"
|
||||||
|
d="m 92.127545,185.85107 v -7.81825 l -2.160764,0.33016 c -1.643214,0.25108 -2.794499,0.31185 -4.806597,0.2537 -3.132989,-0.0905 -4.08004,-0.31154 -6.562779,-1.53152 -2.834421,-1.39278 -5.02603,-3.75166 -6.520589,-7.01825 -2.240327,-4.89658 -2.896711,-5.8545 -5.617726,-8.19847 -3.832281,-3.30126 -3.958719,-3.46167 -3.240384,-4.11102 0.316493,-0.2861 0.650602,-0.33602 2.248958,-0.33602 1.650335,0 2.024407,0.0598 3.094741,0.49508 0.669624,0.2723 1.542749,0.72002 1.940278,0.99493 1.084483,0.74999 2.852069,2.67639 4.546113,4.95459 2.708474,3.64241 5.400091,5.68101 8.17111,6.18868 2.90651,0.5325 9.780684,-0.50733 9.792119,-1.48121 0.0052,-0.43945 1.794629,-3.92514 2.41439,-4.70298 0.69084,-0.86705 1.563938,-1.72371 2.477865,-2.43121 0.315295,-0.24408 0.573264,-0.50145 0.573264,-0.57194 0,-0.0705 -0.813594,-0.22562 -1.807986,-0.34474 -4.283641,-0.51316 -9.518538,-1.67805 -12.214929,-2.71813 -6.733267,-2.59722 -10.615785,-5.87533 -12.970982,-10.95173 -3.055698,-6.58626 -3.644207,-16.6242 -1.381441,-23.56259 0.909502,-2.78883 3.103457,-6.65263 4.770772,-8.40186 l 0.526678,-0.55255 -0.44774,-1.8287 c -0.634959,-2.59334 -0.66174,-2.79551 -0.721293,-5.44467 -0.08078,-3.59346 0.34983,-5.96204 1.625616,-8.941752 l 0.6643,-1.55153 1.321211,0.0534 c 5.17266,0.20892 9.705159,1.95874 15.259205,5.890992 0.874926,0.61944 1.688358,1.18657 1.807628,1.26028 0.119271,0.0737 1.308423,-0.0652 2.642561,-0.30864 5.088076,-0.92852 7.902776,-1.18481 13.009036,-1.18453 4.28809,1.8e-4 7.55843,0.28627 11.58566,1.0133 l 2.58984,0.46754 1.81988,-1.23846 c 1.00094,-0.68115 2.13739,-1.41866 2.52545,-1.63891 3.50894,-1.991582 4.6451,-2.548472 6.2618,-3.069222 2.01946,-0.65049 4.50375,-1.11141 6.4382,-1.19452 l 1.23472,-0.0531 0.48218,0.97014 c 0.65366,1.31517 1.45959,3.860072 1.73164,5.468052 0.48761,2.88205 0.18503,7.01206 -0.73102,9.97788 l -0.35806,1.15928 0.77996,0.91371 c 5.36099,6.28037 7.18607,15.66141 5.06786,26.04914 -1.49787,7.34554 -5.27132,12.3103 -11.73506,15.43991 -2.53328,1.22657 -5.06228,2.15829 -7.14375,2.63189 -3.13795,0.71397 -7.9025,1.50736 -9.05216,1.50736 -0.34562,0 -0.669,0.0657 -0.71861,0.14596 -0.0496,0.0803 0.44644,0.61605 1.10234,1.19062 2.87471,2.51824 4.30536,5.23193 4.77944,9.06574 0.11011,0.89047 0.18483,5.75691 0.18483,12.03854 0,5.79913 0.0352,10.54387 0.0781,10.54387 0.043,0 0.8169,-0.24717 1.7198,-0.54927 8.7244,-2.91907 17.24503,-8.48166 23.61062,-15.41393 6.99037,-7.61265 11.37365,-15.66039 14.01865,-25.73831 1.78341,-6.7951 2.20063,-15.42925 1.12155,-23.20961 -0.49076,-3.53843 -1.96602,-8.96357 -3.45353,-12.7 -5.66442,-14.2283 -15.63949,-24.997052 -29.1136,-31.430102 -5.0033,-2.38876 -10.13893,-4.08984 -15.0372,-4.98078 -1.38888,-0.25263 -1.87001,-0.32177 -3.79236,-0.54498 -1.66217,-0.193 -7.14271,-0.43214 -8.11389,-0.35405 -0.58208,0.0468 -1.89177,0.1305 -2.91041,0.18597 -1.01865,0.0555 -2.09021,0.13266 -2.38125,0.17154 -0.29104,0.0389 -0.96892,0.11245 -1.50639,0.1635 -2.247392,0.21345 -5.534413,0.97619 -9.217536,2.13892 -3.406335,1.07534 -4.85773,1.65271 -8.149684,3.24198 -11.661466,5.62985 -20.72228,14.44568 -26.577579,25.858982 -4.361537,8.50161 -6.382446,16.3159 -6.618918,25.59349 -0.261298,10.25156 1.726956,19.06935 6.365002,28.22843 4.734968,9.35047 11.90202,17.24874 20.922467,23.05708 2.585812,1.66503 8.189422,4.40508 11.072438,5.4142 1.331722,0.46613 2.480843,0.85188 2.553603,0.85723 0.07276,0.005 0.132292,-3.50851 0.132292,-7.80854 z"/>
|
||||||
|
</g>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 5.0 KiB |
@@ -0,0 +1,13 @@
|
|||||||
|
package example.oauth2.github;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ApplicationGithub {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ApplicationGithub.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package example.oauth2.github.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.github.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
|
||||||
|
}
|
||||||
|
}
|
||||||
14
github/src/main/resources/application.yml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
server:
|
||||||
|
address: 127.0.0.1
|
||||||
|
port: 8080
|
||||||
|
|
||||||
|
debug: false
|
||||||
|
|
||||||
|
spring:
|
||||||
|
security:
|
||||||
|
oauth2:
|
||||||
|
client:
|
||||||
|
registration:
|
||||||
|
github:
|
||||||
|
client-id: f9bbf16cc3a93663282f
|
||||||
|
client-secret: 16f203668e25bfe44513d2a7b4925d62bb100783
|
||||||
18
github/src/main/resources/templates/index.html
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru"
|
||||||
|
xmlns="http://www.w3.org/1999/xhtml"
|
||||||
|
xmlns:th="https://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<title>GitHub OAuth2 Example</title>
|
||||||
|
<link rel="icon" href="data:;base64,=">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>GitHub OAuth2 Example</h1>
|
||||||
|
<hr>
|
||||||
|
<p>
|
||||||
|
Home public page<br>
|
||||||
|
---> <a th:href="@{/secret}">Secret Page</a> <---<br>
|
||||||
|
[ <a th:href="@{/oauth2/authorization/github}">Login GitHub</a> ]
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
12
github/src/main/resources/templates/secret.html
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru">
|
||||||
|
<head>
|
||||||
|
<title>GitHub OAuth2 Example</title>
|
||||||
|
<link rel="icon" href="data:;base64,=">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>GitHub OAuth2 Example</h1>
|
||||||
|
<hr>
|
||||||
|
<p style="color: red">[TOP SECRET PAGE]</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
rootProject.name = 'spring-oauth2-examples'
|
rootProject.name = 'spring-oauth2-examples'
|
||||||
|
|
||||||
include('discord')
|
include('discord')
|
||||||
|
include('github')
|
||||||
|
|||||||