Problem: springboot returns login failure

How does springboot return login failure?

UserController

This is the login in the original UserController:

@RequestMapping("login")
	public class UserController extends BaseController {
    
    

	@Autowired
	private IUserService userService;
	
	@RequestMapping("reg")
	public JsonResult<Void> reg(User user) {
    
    
		userService.reg(user);
		return new JsonResult<>(OK);
	}
	public JsonResult<User> login(String username, String password, HttpSession session) {
    
    
		// 调用业务层方法执行登录
		User data = userService.login(username, password);
		// 将uid、username存入到session中
		session.setAttribute("uid", data.getUid());
		session.setAttribute("username", data.getUsername());
		// 向客户端响应
		return new JsonResult<>(OK, data);
	}

method:

This is the login in the modified UserController:

@RequestMapping("login")
	public JsonResult<User> login(String username, String password, HttpSession session) {
    
    
		// 调用业务层方法执行登录
		try {
    
    
			User data = userService.login(username, password);
			session.setAttribute("uid", data.getUid());
			session.setAttribute("username", data.getUsername());
			// 向客户端响应若成功 
			return new JsonResult<>(OK, data);
		}catch (Exception e) {
    
    
			// 向客户端响应若失败 
			return new JsonResult<>(ERROR,"账号或密码错误");
		}
	}

Insert picture description here
What is ERROR?
Because
UserController inherits BaseController, add it to BaseController

protected static final int ERROR= 500;

BaseController

public class BaseController {
    
    
	/**
	 * 响应状态:成功
	 */
	//reg.html "dataType" : "json", "success" : function(json) { if (json.state == 2000) { alert("登录成功!"); } else {alert("登录失败!");}
	protected static final int OK =2000;
	
	protected static final int ERROR= 500;

After the background is normal, configure jQuery data in the foreground

This is before configuration:

<script type="text/javascript">
	$("#reg").click(function() {
    
    
		$.ajax({
    
    
			"url" : "/users/login",
			"data" : $("#form-login").serialize(),
			"type" : "POST",
			"dataType" : "json",
			"success" : function(json) {
    
    
				if (json.state == 2000) {
    
    
    				alert("登录成功!");
    			} else {
    
    
    				alert("登录失败!");
    			}
				}
			}
		});
	});

This is after configuration:

<script type="text/javascript">
	$("#reg").click(function() {
    
    
		$.ajax({
    
    
			"url" : "/users/login",
			"data" : $("#form-login").serialize(),
			"type" : "POST",
			"dataType" : "json",
			"success" : function(json) {
    
    
				if (json.state == 2000) {
    
    
					alert("登录成功!");
				} else {
    
    
					if(json.state== 500){
    
    
						alert(json.message);
					}
				}
			}
		});
	});

in:

alert(json.message);

What is message?

Insert picture description here
Handle exceptions
//Create a JsonResult class that encapsulates the response result

JsonResult

//处理异常
//创建封装响应结果的JsonResult类:
public class JsonResult<T> {
    
    
	private Integer state;
	private String message;
	private T data;
	//this,get.set方法

This way to get the message in login.HTML

Source code

Guess you like

Origin blog.csdn.net/weixin_44182157/article/details/109131219