提交 19565e7f 编写于 作者: E Evan

update:Connected to database

上级 5ef4c72b
......@@ -7,7 +7,9 @@ import com.gm.wj.pojo.User;
import com.gm.wj.pojo.vo.VueLoginInfoVo;
import com.gm.wj.result.Result;
import com.gm.wj.result.ResultFactory;
import com.gm.wj.service.UserService;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
......@@ -15,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;
import java.util.Objects;
......@@ -27,24 +30,39 @@ public class LoginController {
* 给VueLoginInfoVo对象加入@Valid注解,并在参数中加入BindingResult来获取错误信息。
* 在逻辑处理中我们判断BindingResult知否含有错误信息,如果有错误信息,则直接返回错误信息。
*/
@Autowired
UserService userService;
@CrossOrigin
@RequestMapping(value = "/api/login", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
@ResponseBody
public Result login(@Valid @RequestBody VueLoginInfoVo loginInfoVo, BindingResult bindingResult, HttpSession session) {
if (bindingResult.hasErrors()) {
String message = String.format("登陆失败,详细信息[%s]。", bindingResult.getFieldError().getDefaultMessage());
return ResultFactory.buildFailResult(message);
}
if (!Objects.equals("admin", loginInfoVo.getUsername()) || !Objects.equals("123", loginInfoVo.getPassword())) {
String message = String.format("登陆失败,详细信息[用户名、密码信息不正确]。");
// if (bindingResult.hasErrors()) {
// String message = String.format("登陆失败,详细信息[%s]。", bindingResult.getFieldError().getDefaultMessage());
// return ResultFactory.buildFailResult(message);
// }
// if (!Objects.equals("admin", loginInfoVo.getUsername()) || !Objects.equals("123", loginInfoVo.getPassword())) {
// String message = String.format("登陆失败,详细信息[用户名、密码信息不正确]。");
// return ResultFactory.buildFailResult(message);
// }
// User user = new User();
// user.setUsername(loginInfoVo.getUsername());
// user.setPassword(loginInfoVo.getPassword());
// session.setAttribute("user",user);
// return ResultFactory.buildSuccessResult("登陆成功。");
// }
String username = loginInfoVo.getUsername();
username = HtmlUtils.htmlEscape(username);
User user = userService.get(username, loginInfoVo.getPassword());
if (null == user) {
String message = "账号密码错误";
return ResultFactory.buildFailResult(message);
} else {
session.setAttribute("user", user);
return ResultFactory.buildSuccessResult("登录成功");
}
User user = new User();
user.setUsername(loginInfoVo.getUsername());
user.setPassword(loginInfoVo.getPassword());
session.setAttribute("user",user);
return ResultFactory.buildSuccessResult("登陆成功。");
}
}
\ No newline at end of file
......@@ -4,5 +4,7 @@ import com.gm.wj.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserDAO extends JpaRepository<User,Integer> {
User findByUsername(String username);
User getByUsernameAndPassword(String username,String password);
}
......@@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
@Entity
@Table(name = "User")
@Table(name = "user")
@JsonIgnoreProperties({"handler","hibernateLazyInitializer"})
public class User {
......
package com.gm.wj.service;
import com.gm.wj.dao.UserDAO;
import com.gm.wj.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserDAO userDAO;
public boolean isExist(String username) {
User user = getByName(username);
return null!=user;
}
public User getByName(String username) {
return userDAO.findByUsername(username);
}
public User get(String username, String password){
return userDAO.getByUsernameAndPassword(username, password);
}
public void add(User user) {
userDAO.save(user);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册