如何在ssm框架下实现拦截器功能,只用登录成功才能访问其他网页?

springmvc文件

	
	
                
                    
                    
                    
                
            

建立intercepter包

包:cn.baidu.intercepter

 

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

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.kgc.pojo.User;
import cn.kgc.tools.Constants;

public class SysInterceptor extends HandlerInterceptorAdapter{
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception{
	   
	    HttpSession session = request.getSession();

	    User user = (User)session.getAttribute(Constants.USER_SESSION);

	    if(null == user){
	    response.sendRedirect(request.getContextPath()+"/401.jsp");
	    return false;
	    }
	    return true;
	    }
}

controller类

package cn.kgc.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;



import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.kgc.pojo.User;
import cn.kgc.service.UserService;
import cn.kgc.tools.Constants;

@Controller
public class LoginController {

	 @Resource
	 private UserService userService;
	 
	 @RequestMapping(value="/login.html")
	    public String login(){
	    
	    return "login";
	    }
	 
	 @RequestMapping(value="/dologin.html",method=RequestMethod.POST)
	    public String doLogin(@RequestParam String userCode,@RequestParam String userPassword,HttpServletRequest request,HttpSession session) throws Exception{
	   
	    //调用service方法,进行用户匹配
	    User user = userService.login(userCode,userPassword);
	    if(null != user){//登录成功
	    //放入session
	    session.setAttribute(Constants.USER_SESSION, user);
	    //页面跳转(frame.jsp)
	    return "redirect:/sys/main.html";
	    }else{
	    //页面跳转(login.jsp)带出提示信息--转发
	    request.setAttribute("error", "用户名或密码不正确");
	    return "login";
	    }
	    }

	    @RequestMapping(value="/logout.html")
	    public String logout(HttpSession session){
	    //清除session
	    session.removeAttribute(Constants.USER_SESSION);
	    return "login";
	    }

	    @RequestMapping(value="/sys/main.html")
	    public String main(){
	    return "frame";
	    }
}

service层

package cn.kgc.service;



import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.kgc.dao.UserMapper;
import cn.kgc.pojo.User;
@Service
public class UserServiceImpl implements UserService{

	 @Resource
     private UserMapper userMapper;
	
	@Override
	public User login(String userCode, String userPassword) throws Exception {
		// TODO Auto-generated method stub
		 User user = null;
         user = userMapper.getLoginUser(userCode);
         //匹配密码
         if(null != user){
	         if(!user.getUserPassword().equals(userPassword)){
	        	 user = null;
	         }
	         
	      }
         return user;
         
	}

}

dao层




       

 

你可能感兴趣的:(课堂,SpringMVC)