系统登录增加密码错误等错误信息弹窗

UserLoginController.java:

@Controller
@RequestMapping("admin")
public class UserLoginController {
     

	static Logger logger = LoggerFactory.getLogger(UserLoginController.class);
	private Map<String, Object> resultMap = new LinkedHashMap<>();

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request) {
     

		String username = request.getParameter("username");
		String pwd = request.getParameter("password");
		resultMap.put("status", 500);
		if(StringUtils.isEmpty(username) || StringUtils.isEmpty(pwd)){
     
			resultMap.put("message", "用户名或密码为空!");
			return resultMap;
		}
		UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
		token.setRememberMe(false);
		// 获取当前的Subject
		Subject currentUser = SecurityUtils.getSubject();
		try {
     
			AuthManager authManager = AppContext.getBean(AuthManager.class);
			// 校验用户,密码错误则返回user为空
			User user = authManager.verifyAuth(username, pwd);
			if (user == null) {
     
				resultMap.put("message", "密码错误!");
				return resultMap;
			}
			currentUser.login(token);
			resultMap.put("status", 200);
			HttpSession sess = request.getSession();
			sess.setAttribute(AuthUtils.SEC_USER, user);
		} catch (Exception e) {
     
			logger.error("登录异常!", e.getMessage());
		}
		// 验证是否登录成功
		if (!currentUser.isAuthenticated()) {
     
			token.clear();
		}
		//jsp显示“欢迎XXX登录“
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(SEC_USER);
		session.setAttribute("userName",user.getUserName());
		return resultMap;
	}

	/*
	 * 退出登录
	 */
	@RequestMapping(value = "/logout", method =RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> logout(HttpServletRequest request) {
     
		try {
     
			Subject subject = SecurityUtils.getSubject();
			subject.logout();
			// 销毁和用户关联的session对象
			request.getSession().invalidate();
			resultMap.put("status", 200);
		} catch (Exception e) {
     
			resultMap.put("status", 500);
			logger.error("logout error:" + e.getMessage());
		}
		return resultMap;
	}
}

login.jsp:

+

退出登录:
index.jsp:

···
	
···

你可能感兴趣的:(Java)