springmvc+shiro自定义过滤器

实现需求:

1.用户未登录,跳转到登录页,登录完成后会跳到初始访问页。

2.用户自定义处理(如需要激活),跳转到激活页面,激活完成后会跳到初始访问页。

使用到的框架

springmvc 的拦截器

shiro 自定义过滤器

实现:

1.编写拦截器通过session保存初始访问的页面地址,便于后面回跳这个页面做准备。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * 用户登录以后跳转回之前页面的拦截器 拦截对象: 除登录,注册之外的所有跳转页面的请求 因为用户随时可能进行登录操作
 *
 * @version 1.0.0
 * @date 2018 -10-19
 */
public class ForwardBeforeUrlInteceptor implements HandlerInterceptor {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {

		// 过滤掉ajax请求
		if (request.getHeader("x-requested-with") != null
				&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
			return true;
		}

		// 获取当前会话
		HttpSession session = request.getSession(true);
		// 拿到上一个页面地址
		String uri = request.getRequestURI();
		// 去掉项目地址长度的字符(因为我的默认项目地址是给出的)
		String path = uri.substring(request.getContextPath().length());
		// 得到参数
		String query = request.getQueryString();
		if (query == null) {
			query = "";
		} else {
			query = "?" + query;
		}
		String beforePath = path + query;
		session.setAttribute("beforePath", beforePath);
		session.setAttribute("method", request.getMethod());
		logger.debug("beforePath :{},  method:{}", beforePath, request.getMethod());
		return true;
	}
}

2.在spring的xml配置文件中配置拦截器,例如application.xml

 
        

        
        
            
            
            
            
            
            
            
        
    

注意: 此处是界面可以直接进入激活页面,此处是排除拦截激活页面,防止页面出现不停的回跳到自己页面。

3.自定义过滤器。



import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 课程拦截器,当会员过期或未激活时自动跳转到激活页面
 * 
 * @version 1.0.0
 * @date 2018 -10-19
 */
public class MemberFilter extends com.bwjf.framework.shiro.filter.UserFilter {

	@Override
	protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		if (httpServletRequest.getRequestURI().indexOf("activation") > 0) {
			return true;
		}

		MyShiroUser myShiroUser = MyUserUtil.getCurrentShiroUser();
		if (!CheckEmptyUtil.isEmpty(myShiroUser) && CheckEmptyUtil.isEmpty(myShiroUser.getActiveDate())) {
			try {
            // 浏览器跳转到激活页面
				httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/activation");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;

	}
}

4.shiro.xml配置自定义过滤器

springmvc+shiro自定义过滤器_第1张图片

5.controller激活处理后跳转到初始页面

springmvc+shiro自定义过滤器_第2张图片

你可能感兴趣的:(springmvc,shiro,自定义拦截)