SpringMVC登录、注册、验证码

想要实现以下效果:

SpringMVC登录、注册、验证码_第1张图片

首先jsp前台页面



用户名:
密码:
验证码:
登录 注册
用户名:
密码:
真实姓名:
角色:
手机号:
出生日期:
创建日期:
创建人:
最后修改时间:
最后修改人:
性别:
工资:
履历:

在controller层编写

        @RequestMapping("login")//登录
	@ResponseBody
	public ResJson login(HttpServletRequest request , String user_name , String password , String code , HttpSession session){
		ResJson resJson = new ResJson();
		String codeValue = (String) request.getSession().getAttribute(Const.SESSION_CODE);
		if (code.equalsIgnoreCase(codeValue)) {
			User user = userService.getUserName(user_name);
			System.out.println(user);
			if (user==null) {
				resJson.setSuccess(false);
				resJson.setMsg("用户名密码错误");
			}else {
				password=Encoding.md5Encoding(password);
				System.out.println("--------------"+password.toString());
				if (password.equals(user.getPassword())) {
					request.getSession().setAttribute(Const.SESSION_USER, user);
					request.getSession().setAttribute(Const.SESSION_USER_NAME, user.getUser_name());
					resJson.setSuccess(true);
					session.setAttribute("user", user);
				}else {
					resJson.setMsg("用户名密码错误");
				}
			}
		}else {
			resJson.setMsg("验证码错误");
		}
		System.out.println(resJson);
		return resJson;
	}
	@RequestMapping("logout")//退出登录,调回登录页面
	public ModelAndView logout(HttpServletRequest request){
		HttpSession session = request.getSession();
		Object object = session.getAttribute(Const.SESSION_USER);
		if (object!=null) {
			session.invalidate();
		}
		return new ModelAndView("login/login");
	}
	/**
	 * 获取验证码
	 */
	@RequestMapping("getCode")
	@ResponseBody
	public String getCode(HttpServletRequest request , HttpServletResponse response){
		try {
			ValidCodeUtils.getImage(request, response);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}

在dao.xml文件中编写


  

在spring-mvc.xml配置文件中加入拦截器地址


        
               
        

配置SystemInterceptor拦截器

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class Systemlnterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		//获取请求地址
		String requestURI = request.getRequestURI();
		//不需要拦截的请求
		String No=".*/((tologin)|(loginOpt)).*";
		//判断请求
		if (requestURI.matches(No)) {
			return true;
		}
		String id=(String)request.getSession().getAttribute("id");
		if (id!=null) {
			return true;
		}
		request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
		return false;
	}

}

你可能感兴趣的:(SpringMVC登录、注册、验证码)