1.发送请求,浏览器console报如下错误
Failed to load resource: the server responded with a status of 401 ()
2.发现是后端代码的问题,注释掉上面一行,改为下面返回成功
public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res) {
try {
SysUser user = new ObjectMapper().readValue(req.getInputStream(), SysUser.class);
return authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
user.getUsername(),
user.getPassword()
)
);
} catch (Exception e) {
e.printStackTrace();
try {
res.setContentType("application/json;charset=utf-8");
res.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = res.getWriter();
Result result = new Result();
result.setSuccess(false);
result.setMessage("账号或密码错误!");
result.setCode(401);
out.write(new ObjectMapper().writeValueAsString(result));
out.flush();
out.close();
} catch (Exception e1) {
e1.printStackTrace();
}
throw new RuntimeException(e);
}
}