0

add google oauth2

This commit is contained in:
lcarlyl
2022-04-21 18:34:10 +03:00
parent 3fa106491a
commit ae394f5ae0
15 changed files with 155 additions and 0 deletions

View File

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

View File

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

View File

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

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

View File

@@ -0,0 +1,2 @@
</body>
</html>

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

View File

@@ -0,0 +1,7 @@
<#include "includes/header.ftlh">
<p>
Home public page<br>
---&gt; <a href="/secret">Secret Page</a> &lt;---<br>
[ <a href="/oauth2/authorization/google">Login Google</a> ]
</p>
<#include "includes/foother.ftlh">

View File

@@ -0,0 +1,3 @@
<#include "includes/header.ftlh">
<p style="color: red">[TOP SECRET PAGE]</p>
<#include "includes/foother.ftlh">