์นด์นด์ค ๋ก๊ทธ์ธ - ์ปค์คํ ๋ก๊ทธ์ธ ํ์ด์ง
์ด์ ํ์ด์ง
์ด์ ํ์ด์ง ๋ด์ฉ์ ์ด์ด์ ์งํํฉ๋๋ค.
Code
Preview
์์ ํ๋ก์ธ์ค
1.
์ ์ ํ๋ก์ ํธ
2.
์คํ๋ง ์ํ๋ฆฌํฐ ์ค์
โข
~/config/SecufityConfig.java
โฆ
/** ๋๋ /login ๊ฒฝ๋ก ๋ชจ๋ ํ์ฉ
โฆ
์ปค์คํ
๋ก๊ทธ์ธ ํ์ด์ง ๊ฒฝ๋ก ์ง์ : /login
3.
์์ฒญ ๊ฒฝ๋ก ๋งคํ
โข
~/controller/HomeController.java
โฆ
๋ก๊ทธ์ธ ํ๋ฉด
โช
/login
โช
login.html
์์ ํ๋ก์ธ์ค
์คํ๋ง ์ํ๋ฆฌํฐ ์ค์
โข
/** ๋๋ /login ๊ฒฝ๋ก ๋ชจ๋ ํ์ฉ
โข
์ปค์คํ
๋ก๊ทธ์ธ ํ์ด์ง ๊ฒฝ๋ก ์ง์ : /login
/** ๋๋ /login ๊ฒฝ๋ก ๋ชจ๋ ํ์ฉ
http.authorizeRequests(requests -> requests
.antMatchers("/**").permitAll()
.anyRequest().authenticated());
Java
๋ณต์ฌ
http.authorizeRequests(requests -> requests
.antMatchers("/").permitAll()
.antMatchers("/login").permitAll()
.anyRequest().authenticated());
Java
๋ณต์ฌ
์ปค์คํ ๋ก๊ทธ์ธ ํ์ด์ง ๊ฒฝ๋ก ์ง์ : /login
http.oauth2Login(login -> login
.loginPage("/login")
.userInfoEndpoint()
.userService(oAuthService)
);
Java
๋ณต์ฌ
~/config/SecufityConfig.java
package com.aloha.kakaocustom.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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.web.SecurityFilterChain;
import com.aloha.kakaocustom.service.OAuthService;
@EnableWebSecurity
@Configuration
public class SecurityConfig {
@Autowired
private OAuthService oAuthService;
/**
* ๐ ์คํ๋ง ์ํ๋ฆฌํฐ ์ค์ ๋ฉ์๋
* @param http
* @return
* @throws Exception
*/
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// ๐ฉโ๐ผ ์ธ๊ฐ ์ค์
http.authorizeRequests(requests -> requests
.antMatchers("/**").permitAll()
.anyRequest().authenticated());
// ๐ฉโ๐ป๐ OAuth2 ๋ก๊ทธ์ธ
// โ
userInfoEndpoint() : ์ฌ์ฉ์ ์ ๋ณด ์ค์ ๊ฐ์ฒด ๊ฐ์ ธ์ค๊ธฐ
// โ
userService(oAuthService) : ์ฌ์ฉ์ ์ ๋ณด ์ค์ ๊ฐ์ฒด๋ก, ๋ก๊ทธ์ธ ํ ์ฒ๋ฆฌํ ๊ตฌํ ํด๋์ค ๋ฑ๋ก
// โ
loginPage(๊ฒฝ๋ก) : ์ปค์คํ
๋ก๊ทธ์ธ ํ์ด์ง ๊ฒฝ๋ก ์ง์
http.oauth2Login(login -> login
.loginPage("/login")
.userInfoEndpoint()
.userService(oAuthService)
);
return http.build();
}
}
Java
๋ณต์ฌ
์์ฒญ ๊ฒฝ๋ก ๋งคํ
โข
~/controller/HomeController.java
โฆ
๋ก๊ทธ์ธ ํ๋ฉด
โช
/login
โช
login.html
HomeController
@Slf4j
@Controller
public class HomeController {
/**
* ๋ฉ์ธ ํ๋ฉด
* ๐ [GET] - /
* ๐ index.html
* @return
*/
@GetMapping("/")
public String home(@AuthenticationPrincipal OAuth2User oauth2User
,Model model) {
log.info(":::::::::: ๋ฉ์ธ ํ๋ฉด ::::::::::");
CustomUser customUser = (CustomUser) oauth2User;
model.addAttribute("user", customUser);
return "/index";
}
/**
* ๋ก๊ทธ์ธ ํ๋ฉด
* ๐ [GET] - /login
* ๐ login.html
* @return
*/
@GetMapping("/login")
public String login() {
return "/login";
}
}
Java
๋ณต์ฌ
login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OAuth</title>
<!-- bootstrap css -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container col-12 col-lg-4">
<div class="px-4 py-5 mt-5 text-center">
<h1 class="display-5 fw-bold text-body-emphasis">๋ก๊ทธ์ธ</h1>
</div>
<!-- ๋ก๊ทธ์ธ ์์ญ -->
<main class="w-100 m-auto">
<form action="/login" method="post">
<!-- CSRF TOKEN -->
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
<div class="form-floating">
<input type="text" class="form-control" id="floatingInput" name="username" value="" placeholder="์์ด๋"
autofocus th:value="${userId}">
<label for="floatingInput">์์ด๋</label>
</div>
<div class="form-floating">
<input type="password" class="form-control" id="floatingPassword" name="password" placeholder="๋น๋ฐ๋ฒํธ">
<label for="floatingPassword">๋น๋ฐ๋ฒํธ</label>
</div>
<div class="form-check text-start my-3 d-flex justify-content-around">
<div class="item">
<input class="form-check-input" type="checkbox" name="remember-id" id="flexCheckDefault1" th:checked="${rememberId}">
<label class="form-check-label" for="flexCheckDefault1">์์ด๋ ์ ์ฅ</label>
</div>
<div class="item">
<input class="form-check-input" type="checkbox" name="remember-me" id="flexCheckDefault2">
<label class="form-check-label" for="flexCheckDefault2">์๋ ๋ก๊ทธ์ธ</label>
</div>
</div>
<!-- ๋ก๊ทธ์ธ ์๋ฌ -->
<th:block th:if="${param.error}">
<p class="text-center text-danger">์์ด๋ ๋๋ ๋น๋ฐ๋ฒํธ๋ฅผ ์๋ชป ์
๋ ฅํ์ต๋๋ค.</p>
</th:block>
<!-- ๋ก๊ทธ์์ ์๋ฃ -->
<th:block th:if="${param.logout}">
<p class="text-center text-success">์ ์์ ์ผ๋ก ๋ก๊ทธ์์ ๋์์ต๋๋ค.</p>
</th:block>
<div class="d-grid gap-2">
<button class="btn btn-lg btn-primary w-100 py-2" type="submit">๋ก๊ทธ์ธ</button>
<a href="/join" class="btn btn-lg btn-success w-100 py-2">ํ์๊ฐ์
</a>
<hr>
<a href="/oauth2/authorization/kakao">
<img src="/img/kakao_login_large.png" width="100%" alt="์นด์นด์ค ๋ก๊ทธ์ธ">
</a>
</div>
</form>
</main>
</div>
<!-- bootstrap js -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
HTML
๋ณต์ฌ