jpablog : login basic
This commit is contained in:
@@ -11,17 +11,29 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api")
|
||||
public class UserApiController {
|
||||
|
||||
private final UserService userService;
|
||||
private final HttpSession session;
|
||||
|
||||
@PostMapping("/user")
|
||||
public ResponseDto<Integer> save(@RequestBody User user) {
|
||||
user.setRole(RoleType.USER);
|
||||
int result = userService.회원가입(user);
|
||||
return new ResponseDto<>(result, HttpStatus.OK);
|
||||
return new ResponseDto<>(result, HttpStatus.OK.value());
|
||||
}
|
||||
|
||||
@PostMapping("/user/login")
|
||||
public ResponseDto<Integer> login(@RequestBody User user) {
|
||||
User principal = userService.로그인(user);
|
||||
if (principal != null) {
|
||||
session.setAttribute("principal", principal);
|
||||
}
|
||||
return new ResponseDto<>(1, HttpStatus.OK.value());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,5 +14,6 @@ import org.springframework.http.HttpStatus;
|
||||
public class ResponseDto<T> {
|
||||
|
||||
T data;
|
||||
HttpStatus status;
|
||||
int statusCode;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.example.jpablog.handler;
|
||||
|
||||
import com.example.jpablog.dto.ResponseDto;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -9,7 +11,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public String handleArgumentException(Exception e) {
|
||||
return "<h1>" + e.getMessage() + "</h1>";
|
||||
public ResponseDto<String> handleArgumentException(Exception e) {
|
||||
return new ResponseDto<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,15 @@ package com.example.jpablog.repository;
|
||||
|
||||
import com.example.jpablog.model.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Repository // 생략가능
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
// select * from user where username =? and password =?;
|
||||
User findByUsernameAndPassword(String username, String password);
|
||||
|
||||
// @Query(value = "select * from user where username=?1 and password=?2", nativeQuery = true)
|
||||
// User login(String username, String password);
|
||||
}
|
||||
|
||||
@@ -6,9 +6,11 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(readOnly = true)
|
||||
@Transactional(readOnly = true) // select 할 때 트랜잭션 시작, 서비스 종료시에 트랜잭션 종료 (정합성)
|
||||
public class UserService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
@@ -18,4 +20,8 @@ public class UserService {
|
||||
userRepository.save(user);
|
||||
return 1;
|
||||
}
|
||||
|
||||
public User 로그인(User user) {
|
||||
return userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ let index = {
|
||||
init : function () {
|
||||
$("#btn-save").on("click", () => {
|
||||
this.save();
|
||||
});
|
||||
$("#btn-login").on("click", () => {
|
||||
this.login();
|
||||
})
|
||||
},
|
||||
save : function () {
|
||||
@@ -23,6 +26,23 @@ let index = {
|
||||
}).fail(function (error){
|
||||
alert(JSON.stringify(error));
|
||||
});
|
||||
},
|
||||
login : function () {
|
||||
let data = {
|
||||
username: $("#username").val(),
|
||||
password: $("#pwd").val(),
|
||||
};
|
||||
$.ajax({
|
||||
type: "post",
|
||||
url: "/jpablog/api/user/login",
|
||||
data: JSON.stringify(data),
|
||||
contentType: "application/json; charset=utf-8",
|
||||
dataType: "json"
|
||||
}).done(function (resp){
|
||||
location.href = "/jpablog";
|
||||
}).fail(function (error){
|
||||
alert(JSON.stringify(error));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
|
||||
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
@@ -19,14 +20,34 @@
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="collapsibleNavbar">
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="${pageContext.request.contextPath}/user/loginForm">로그인</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="${pageContext.request.contextPath}/user/joinForm">회원가입</a>
|
||||
</li>
|
||||
</ul>
|
||||
<c:choose>
|
||||
<c:when test="${empty sessionScope.principal}">
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="${pageContext.request.contextPath}/user/loginForm">로그인</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="${pageContext.request.contextPath}/user/joinForm">회원가입</a>
|
||||
</li>
|
||||
</ul>
|
||||
</c:when>
|
||||
<c:otherwise>
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="${pageContext.request.contextPath}/user/writeForm">글쓰기</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="${pageContext.request.contextPath}/user/userForm">회원정보</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="${pageContext.request.contextPath}/user/logout">로그아웃</a>
|
||||
</li>
|
||||
</ul>
|
||||
</c:otherwise>
|
||||
</c:choose>
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
</nav>
|
||||
<br/>
|
||||
@@ -3,7 +3,7 @@
|
||||
<%@include file="../layout/header.jsp"%>
|
||||
|
||||
<div class="container">
|
||||
<form action="/action_page.php">
|
||||
<form>
|
||||
<div class="form-group">
|
||||
<label for="username">Username:</label>
|
||||
<input type="text" class="form-control" placeholder="Enter username" id="username">
|
||||
@@ -17,8 +17,9 @@
|
||||
<input class="form-check-input" type="checkbox"> Remember me
|
||||
</label>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">로그인</button>
|
||||
<button id="btn-login" type="button" class="btn btn-primary">로그인</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script src="${pageContext.request.contextPath}/js/user.js"></script>
|
||||
<%@include file="../layout/footer.jsp"%>
|
||||
Reference in New Issue
Block a user