实现登录,编写错误码,实现错误码返回json

This commit is contained in:
F嘉阳
2018-01-30 16:58:12 +08:00
parent 91aba6a592
commit bc720e6270
10 changed files with 155 additions and 12 deletions

View File

@@ -6,7 +6,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class LoginController {
@@ -14,16 +13,16 @@ public class LoginController {
@Autowired
private UserService userService;
@GetMapping("/index")
@GetMapping(value = {"index",""})
public String toLoginPage(){
return "login";
}
@PostMapping("/login/dologin")
public boolean doLogin(TbUser tbUser){
public String doLogin(TbUser tbUser)throws Exception{
if (userService.doLoginService(tbUser.getColname(),tbUser.getColpassword())){
return true;
return "home";
}
return false;
return "login";
}
}

View File

@@ -0,0 +1,34 @@
package com.fjy.spring.domain;
/**
* Http请求返回的最外层对象
*/
public class Result<T> {
private Integer code;
private String message;
private T data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}

View File

@@ -0,0 +1,28 @@
package com.fjy.spring.enums;
public enum ResultEnum {
UNKOWN_ERROR(-1,"未知错误"),
SUCCESS(0,"请求成功"),
USER_NOTEXIST(101,"用户不存在"),
UPDATE_ERROR(102,"更新失败"),
DELETE_ERROR(103,"删除失败"),
ADD_ERROR(104,"添加失败"),
WRONGPASS(105,"用户名或密码错误"),
ILLEGAL_ACCESS(106,"非法访问")
;
private Integer code;
private String msg;
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
ResultEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}

View File

@@ -0,0 +1,17 @@
package com.fjy.spring.exception;
import com.fjy.spring.enums.ResultEnum;
public class UserException extends RuntimeException{
private Integer code;
public UserException(ResultEnum resultEnum) {
super(resultEnum.getMsg());
this.code = resultEnum.getCode();
}
public Integer getCode() {
return code;
}
}

View File

@@ -0,0 +1,28 @@
package com.fjy.spring.handle;
import com.fjy.spring.domain.Result;
import com.fjy.spring.enums.ResultEnum;
import com.fjy.spring.exception.UserException;
import com.fjy.spring.untils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class ExceptionHandle {
private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result handle(Exception e){
if (e instanceof UserException){
UserException userException = (UserException)e;
return ResultUtil.error(userException.getCode(),userException.getMessage());
}else{
logger.error("系统异常",e);
return ResultUtil.error(ResultEnum.UNKOWN_ERROR.getCode(),ResultEnum.UNKOWN_ERROR.getMsg());
}
}
}

View File

@@ -1,6 +1,8 @@
package com.fjy.spring.service;
import com.fjy.spring.domain.TbUser;
import com.fjy.spring.enums.ResultEnum;
import com.fjy.spring.exception.UserException;
import com.fjy.spring.repository.TbUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -17,6 +19,6 @@ public class UserService {
if (password.equals(user.getColpassword())){
return true;
}
return false;
throw new UserException(ResultEnum.USER_NOTEXIST);
}
}

View File

@@ -0,0 +1,12 @@
package com.fjy.spring.untils;
import com.fjy.spring.domain.Result;
public class ResultUtil {
public static Result error(Integer code,String msg){
Result result = new Result();
result.setCode(code);
result.setMessage(msg);
return result;
}
}

View File

@@ -2,9 +2,11 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<title>主页</title>
</head>
<body>
<h1>登录系统</h1>
<center>
<h1>欢迎访问主页</h1>
</center>
</body>
</html>

View File

@@ -5,6 +5,15 @@
<title>登录</title>
</head>
<body>
<center>
<h1>登录系统</h1>
<form action="/cms/login/dologin" method="post">
<p>用户名:
<input type="text" name="colname"></p>
<p>密码:
<input type="password" name="colpassword"></p>
<input type="submit" value="登录">
</center>
</form>
</body>
</html>

View File

@@ -11,6 +11,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@@ -22,7 +23,18 @@ public class LoginControllerTest {
@Test
public void toLoginPage() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/index"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("/login"));
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void doLogin() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/login/dologin").param("colname", "root").param("colpassword", "root"))
.andExpect(MockMvcResultMatchers.status().isOk());
mvc.perform(MockMvcRequestBuilders.post("/login/dologin").param("colname", "root").param("colpassword", "123"))
.andExpect(MockMvcResultMatchers.content().json("{\n" +
" \"code\": 101,\n" +
" \"message\": \"用户不存在\",\n" +
" \"data\": null\n" +
"}"));
}
}