LoginController.java 2.31 KB
package com.daeucna.board.security.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daeucna.board.security.domain.UserDto;
import com.daeucna.board.security.domain.UserForm;
import com.daeucna.board.security.service.LoginService;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/")
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

	@GetMapping("/login")
	public String getlogin(Model model) {
	  log.info("로그인 Page");
	  return "page/login/login";
	}
    
  @GetMapping("/info")
  @ResponseBody
  public String getCurrentUserInfo(HttpSession httpSession) {
	  try {
		  Authentication authentication = (Authentication) httpSession.getAttribute("authentication");
		  UserDto userDto = (UserDto) authentication.getPrincipal();
	      log.info("user : " + userDto );
	      return userDto.getUsername() + "은 로그인중입니다";
	  } catch (Exception e) {
	      return "먼저 로그인을 하세요";
	  }
  }

  @GetMapping("/join")
  public String getJoin(Model model) {
    log.info("회원가입 Page");
    return "page/login/join";
  }

  @SuppressWarnings({ "rawtypes", "unchecked" })
  @PostMapping("/join_proc")
  @ResponseBody
  public ResponseEntity procJoin(HttpServletRequest request, HttpServletResponse response, UserForm userForm) {
	  HttpStatus httpStatus = null;
	  UserForm resultUserForm = null;
	  try {
	      resultUserForm = loginService.procJoin(userForm);
	      	      
		  httpStatus = HttpStatus.OK;
	  } catch (Exception e) {
		  httpStatus = HttpStatus.BAD_REQUEST;
	  }
	  return new ResponseEntity(resultUserForm, httpStatus);
  }
  
}