springmvc+mybatis登录验证2.0(更新了使用session来进行保存用户值,注销后清除session),并进行了拦截器的设定

本次在之前的文章实现登录注册的基础上又进行了新的升级,主要是采用了session和拦截器来对功能进行完善

新的Controller层

package lyb.com.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import lyb.com.dao.UserMapper;
import lyb.com.model.User;

@Controller
public class UserController {
	@Autowired
	private UserMapper mapper;
	
	@RequestMapping("index")
	public String index(){
		return "login";
	}
	
	@RequestMapping("login")
	public String login(User u,HttpSession session){
		String username = u.getUsername();
		String password = u.getPassword();
		User user = mapper.selectUser(username);
		session.setAttribute("users", user);
		if(user!=null){
			if(password.equals(user.getPassword())){
				return "redirect:list";
			}else{
				return "error";
			}
		}else{
			return "error";
		}
	}
	
	@RequestMapping("list")
	public ModelAndView userList(ModelAndView m){
		List list = new ArrayList();
		list = mapper.selectList();
		m.addObject("list",list);
		m.setViewName("list");
		return m;
	}
	
	@RequestMapping("edit")
	public ModelAndView edit(ModelAndView m,Integer id){
		User user = mapper.selectByPrimaryKey(id);
		//编辑方法
			m.setViewName("edit");
			m.addObject("user",user);
			return m;
	}
	
	@RequestMapping("save")
	public String save(ModelAndView m,User user){
		mapper.updateByPrimaryKeySelective(user);
		return "redirect:list";
	}
	
	@RequestMapping("add1")
	public ModelAndView add1(ModelAndView m){
		m.setViewName("add");
		return m;
	}
	@RequestMapping("add")
	public String add(User user){
		mapper.insert(user);
		return "redirect:list";
	}
	@RequestMapping("delete")
	public String delete(Integer id){
		mapper.deleteByPrimaryKey(id);
		return "redirect:list";
	}
	@RequestMapping("exit")
	public ModelAndView exit(HttpSession session,ModelAndView m){
		session.invalidate();
		m.setViewName("redirect:index");
		return m;
	}
}

在controller层的login方法中设置了一个session来保存用户实体

在exit方法中设置了一个invalidate来销毁session

使用拦截器需要进行两个地方的配置

1.创建一个UserInterceptor的拦截器类

package lyb.com.interceptor;

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

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



public class UserInterceptor implements HandlerInterceptor  {  
    
	  private final String users = "users";  
	  //private  Test t;
	  //拦截前处理  
	  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {  
	    Object sessionObj = request.getSession().getAttribute(users);  
	    if(sessionObj!=null) {   
	      return true;  
	    }   
	    response.sendRedirect("index");  
	    return false;  
	  }  
	  //拦截后处理  
	  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mav) throws Exception { }  
	  //全部完成后处理  
	  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) throws Exception { }  
	}  
2.在springmvc的配置文件中增加拦截器的属性

  
              
              
                 
                 
                    
                  
              
         
这里除了index和login外其他的请求在没有session的情况下都被拦截


下一步实现首页的默认访问页面



你可能感兴趣的:(java)