0

добавлен пример для GitHub

This commit is contained in:
2022-04-17 16:45:58 +03:00
parent 674ef5db88
commit d1d3e5df18
16 changed files with 177 additions and 7 deletions

View File

@@ -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);
}
}

View File

@@ -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");
}
}

View File

@@ -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
}
}

View 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

View 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>
---&gt; <a th:href="@{/secret}">Secret Page</a> &lt;---<br>
[ <a th:href="@{/oauth2/authorization/github}">Login GitHub</a> ]
</p>
</body>
</html>

View 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>