Skip to content

Commit

Permalink
Merge pull request #28 from kookmin-sw/로그인-기능-및-화면-수정
Browse files Browse the repository at this point in the history
로그인 기능 및 화면 수정
  • Loading branch information
hightuv authored Mar 30, 2024
2 parents 8a7a1b0 + 9e3240d commit cc1872c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/example/WebOrder/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.example.WebOrder.config;

import com.example.WebOrder.controller.LoginController;
import com.example.WebOrder.controller.LoginHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
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.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand All @@ -15,6 +14,8 @@
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public LoginHandler loginHandler() {return new LoginHandler();}

@Bean
public PasswordEncoder passwordEncoder(){
Expand All @@ -32,7 +33,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
)
.formLogin(formLogin -> formLogin
.loginPage("/login")
.defaultSuccessUrl("/home")
.successHandler(loginHandler())
.failureHandler(loginHandler())
.permitAll()
)
.logout(logout -> logout
Expand Down
22 changes: 3 additions & 19 deletions src/main/java/com/example/WebOrder/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import com.example.WebOrder.dto.LoginFormDto;
import com.example.WebOrder.dto.UserFormDto;
import com.example.WebOrder.entity.User;
import com.example.WebOrder.service.LoginService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;



// 로그인과 관련된 url을 매핑한 컨트롤러
@Slf4j
@Controller
public class LoginController {
Expand All @@ -24,10 +24,6 @@ public LoginController(LoginService loginService) {
this.loginService = loginService;
}

@GetMapping("index")
public String getIndex(){
return "html/index";
}

@GetMapping("/home")
public String getHome(){
Expand All @@ -40,18 +36,6 @@ public String getLoginForm(@ModelAttribute("loginFormDto") LoginFormDto dto){
return "html/loginForm";
}

@PostMapping("/login")
public String login(@Valid @ModelAttribute("loginFormDto") LoginFormDto dto){
log.info("로그인 시도");
if (loginService.isLoginAttemptValid(dto)){
log.info("로그인 성공");
return "redirect:/home";
}
else {
log.info("로그인 실패");
return "redirect:/login?error=true";
}
}

@GetMapping("/register")
public String registerForm(@ModelAttribute("userFormDto") UserFormDto dto) {
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/example/WebOrder/controller/LoginHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.WebOrder.controller;

import com.example.WebOrder.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Controller;

import java.io.IOException;

//스프링 내부 구현을 이용하기 위한 LoginHandler
@Slf4j
@Controller
public class LoginHandler implements AuthenticationFailureHandler, AuthenticationSuccessHandler {

//시큐리티 내부 구현 Method
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
log.info("로그인 성공 : {}", ((User) authentication.getPrincipal()).getUsername());
response.sendRedirect("/home");
}


@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
log.info("로그인 실패: {}", request.getParameter("username"));
response.sendRedirect("/login?error");
}
}
2 changes: 2 additions & 0 deletions src/main/resources/static/css/sidebar.css
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@ header .image-text .header-text {
color: var(--sidebar-color);
font-size: 22px;
transition: var(--tran-03);
cursor: pointer;
}

.sidebar.close header .toggle {
transform: translateY(-50%);
cursor: pointer;
}

body.dark .sidebar header .toggle {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/templates/html/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</a>
</li>
<li class="nav-link">
<a href="#">
<a href="/myprofile">
<i class='bx bx-baguette icon' ></i>
<span class="text nav-text">메뉴 관리</span>
</a>
Expand All @@ -63,7 +63,7 @@
</a>
</li>
<li class="nav-link">
<a href="#">
<a href="/owner/seat/manage">
<i class='icon'><img src="/images/table.png"></i>
<span class="text nav-text">테이블 관리</span>
</a>
Expand Down

0 comments on commit cc1872c

Please sign in to comment.