package org.zerock.projectmeongmung.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class UserViewController {
@GetMapping("/login")
public String login() {
return "login";
}
@GetMapping("/signup")
public String signup() {
return "signup";
}
}
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class UserViewController {
@GetMapping("/login")
public String login() {
return "login";
}
@GetMapping("/signup")
public String signup() {
return "signup";
}
}
UserViewController.java 입니다...
package org.zerock.projectmeongmung.controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.zerock.projectmeongmung.dto.AddUserRequest;
import org.zerock.projectmeongmung.service.UserService;
@RequiredArgsConstructor
@Controller
public class UserApiController {
private final UserService userService;
@PostMapping("/user")
public String signup(AddUserRequest request) {
userService.save(request);
return "redirect:/login";
}
@GetMapping("/logout")
public String logout(HttpServletRequest request, HttpServletResponse response) {
new SecurityContextLogoutHandler().logout(request, response, SecurityContextHolder.getContext().getAuthentication());
return "redirect:/meongmung";
}
}
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.zerock.projectmeongmung.dto.AddUserRequest;
import org.zerock.projectmeongmung.service.UserService;
@RequiredArgsConstructor
@Controller
public class UserApiController {
private final UserService userService;
@PostMapping("/user")
public String signup(AddUserRequest request) {
userService.save(request);
return "redirect:/login";
}
@GetMapping("/logout")
public String logout(HttpServletRequest request, HttpServletResponse response) {
new SecurityContextLogoutHandler().logout(request, response, SecurityContextHolder.getContext().getAuthentication());
return "redirect:/meongmung";
}
}
UserApi 컨트롤러입니다..
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>로그인</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f5f5f5;
}
.login-container {
width: 300px;
padding: 20px;
background-color: #ffffff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 10px;
}
.login-container h1 {
text-align: center;
margin-bottom: 20px;
}
.login-container label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.login-container input[type="text"],
.login-container input[type="password"] {
width: 100%;
padding: 8px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
.login-container button {
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.login-container button:hover {
background-color: #45a049;
}
.login-container .link-container {
margin-top: 10px;
display: flex;
justify-content: space-between;
}
.login-container .link-container a {
text-decoration: none;
color: #4CAF50;
font-weight: bold;
}
</style>
</head>
<body>
<div class="login-container">
<h1>로그인</h1>
<form action="/login" method="post">
<input type="hidden" th:name="${_csrf?.parameterName}" th:value="${_csrf?.token}" />
<label for="username">아이디</label>
<input type="text" id="username" name="username" placeholder="영문, 숫자를 구분하여 8~20 글자 이내로 입력해주세요" required>
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" placeholder="영문, 숫자를 구분하여 8~20 글자 이내로 입력해주세요" required>
<button type="submit">로그인</button>
</form>
<div class="link-container">
<a href="#" onclick="history.back(); return false;">뒤로가기</a>
<button type="button" class="btn btn-secondary mt-3" onclick="location.href='/signup'">회원가입</button>
</div>
</div>
</body>
</html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>로그인</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f5f5f5;
}
.login-container {
width: 300px;
padding: 20px;
background-color: #ffffff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 10px;
}
.login-container h1 {
text-align: center;
margin-bottom: 20px;
}
.login-container label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.login-container input[type="text"],
.login-container input[type="password"] {
width: 100%;
padding: 8px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
.login-container button {
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.login-container button:hover {
background-color: #45a049;
}
.login-container .link-container {
margin-top: 10px;
display: flex;
justify-content: space-between;
}
.login-container .link-container a {
text-decoration: none;
color: #4CAF50;
font-weight: bold;
}
</style>
</head>
<body>
<div class="login-container">
<h1>로그인</h1>
<form action="/login" method="post">
<input type="hidden" th:name="${_csrf?.parameterName}" th:value="${_csrf?.token}" />
<label for="username">아이디</label>
<input type="text" id="username" name="username" placeholder="영문, 숫자를 구분하여 8~20 글자 이내로 입력해주세요" required>
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" placeholder="영문, 숫자를 구분하여 8~20 글자 이내로 입력해주세요" required>
<button type="submit">로그인</button>
</form>
<div class="link-container">
<a href="#" onclick="history.back(); return false;">뒤로가기</a>
<button type="button" class="btn btn-secondary mt-3" onclick="location.href='/signup'">회원가입</button>
</div>
</div>
</body>
</html>
로그인 html 입니다..
흠 spring security 라이브러리 사용하고 있는거임 ?
사용하고 있다면 타임리프에서 굳이 csrf 태그 굳이 안만들어줘도 ㅗ디고
나도 너랑 같은 증상있어서 왜 그런 증상이 발생하는지는 찾지 못했지만 예상하기로는 로그인이 성공하기 이전에 처음에 랜더링한 페이지로 이동하게 설정되어 있는데 그게 이미지라서 이미지로 리다이렉션이 일어난거 같음
formLogin에서 defaultSuccessUrl() 을 설정해보던지 successForwardUrl()을 설정해보셈 그것도 안된다면
successHandler를 이용해 response.sendRedirect() 으로 원하는 페이지로 리다이렉션 하면 될거임
securityFilterChain 설정 일부분 코드로 남겨두고감
@Bean public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { httpSecurity .authorizeHttpRequests(auth -> auth .requestMatchers("/").permitAll() .anyRequest().authenticated() ) .formLogin(login -> login.loginPage("/login").permitAll()
.successHandler(new AuthenticationSuccessHandler() { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { response.sendRedirect("/"); }
그리고 질문 여기서 하지말고 벡갤로 와라