Search

์นด์นด์˜ค ๋กœ๊ทธ์ธ - ์ปค์Šคํ…€ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€

์นด์นด์˜ค ๋กœ๊ทธ์ธ - ์ปค์Šคํ…€ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€

์นด์นด์˜ค ๋กœ๊ทธ์ธ - INDEX

์ด์ „ ํŽ˜์ด์ง€

์ด์ „ ํŽ˜์ด์ง€ ๋‚ด์šฉ์— ์ด์–ด์„œ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

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
๋ณต์‚ฌ