---
# System prepended metadata

title: 'Spring Boot Google Login with Spring Security '
tags: [Spring Security]

---

---
title: 'Spring Boot Google Login with Spring Security '
disqus: hackmd
---
###### tags: `Spring Security` 
Spring Boot Google Login with Spring Security 
===

[TOC]

## 適用場景

想是Google去做登入後，才能訪問某些URL
特別可以用在不想要自己去寫註冊還有登入的邏輯

## 先備條件

## Oauth2 同意頁面

![](https://i.imgur.com/hnT4wga.png)

## 建立Oauth2 憑證

![](https://i.imgur.com/Mg3HJ8v.png)



這邊最重要的就是設定url的部分

務必要設定的一模一樣

```
http:localhost:8080/login/oauth2/code/google
```


![](https://i.imgur.com/sSYlKtw.png)




## 檔案結構目錄

![](https://i.imgur.com/LvkzngL.png)


## 程式碼部分

```xml=
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>
```


application.yml

```java=
spring:
  security:
    oauth2:
      client:
        registration:
          google :
            clientId: 大膽放上你的clientId，直接放就對了
            clientSecret:大膽放上你的clientSecret，直接放就對了

server:
  port: 8080
```


SecurityConfig

```java=
package com.javatechie.google.auth;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
       http.authorizeRequests()
               .anyRequest().authenticated()
               .and().oauth2Login();
    }
}

```

SpringSsoGoogleApplication

```java=
package com.javatechie.google.auth;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@SpringBootApplication
@RestController
public class SpringSsoGoogleApplication {

    @GetMapping
    public String welcome() {
        return "Welcome to Google !!";
    }

    @GetMapping("/user")
    public Principal user(Principal principal) {
        System.out.println("username : " + principal.getName());
        return principal;
    }


    public static void main(String[] args) {
        SpringApplication.run(SpringSsoGoogleApplication.class, args);
    }

}

```

# 結果

![](https://i.imgur.com/Z3eOkDG.png)

![](https://i.imgur.com/ZqsQ4QU.png)

前後兩次登入都是一致的

username : 109259804659623592334

username : 109259804659623592334


# Security 大致流程

![](https://i.imgur.com/SM3BwLS.png)

![](https://i.imgur.com/nK9bhwa.png)

## 參考連結

1. [Youtube教學](https://www.youtube.com/watch?v=qcz2jBLNOtc&t=347s)
2. [github](https://github.com/Java-Techie-jt/spring-sso-google)

[感覺非常完整的bilibili課程](https://www.bilibili.com/video/BV1mm4y1X7Hc?p=4)

https://juejin.cn/post/7025787630222573599

整合SpringSecurity和JWT实现登录认证和授权

[【项目实践】一文带你搞定Spring Security + JWT实现前后端分离下的认证授权](https://zhuanlan.zhihu.com/p/342755411) 這個有三個可以看

[必看詳細](https://www.callicoder.com/spring-boot-security-oauth2-social-login-part-1/)

https://medium.com/initgrep/spring-security-how-to-implement-username-and-password-authentication-b57353b710e3

https://spring.io/guides/tutorials/spring-boot-oauth2/



