首页 > 代码库 > springmvc权限过滤器

springmvc权限过滤器

package com.zbb.cn.filter;

import java.io.PrintWriter;

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

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

public class frontInterceptor implements HandlerInterceptor {
// @Autowired
// private VariableService variableService;
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
// 后台session控制
Object user=request.getSession().getAttribute("user");
/* String returnUrl = request.getRequestURI(); */
String requestUrl = request.getRequestURI();
if(null==user){
boolean date=false;
if(requestUrl.contains("login.htmls") || requestUrl.contains("login.jsp") || requestUrl.contains("/baVerify/verifyCode.htmls")){//登入的时候放行
date=true;
}else{
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
StringBuilder builder = new StringBuilder();
builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
builder.append("alert(\"请求超时,请重新登陆!\");");
/*builder.append("window.location.href=http://www.mamicode.com/""+variableService.selectVariable("HTML_URL")+"/login.jsp\";");//之前的写法 */
builder.append("window.location.href=http://www.mamicode.com/"/login.jsp\";");
builder.append("</script>");
out.print(builder.toString());
out.close();
}
/* request.getRequestDispatcher("/login.jsp").forward(request, response); */
return date;
} else {
return true;
}
}
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse response, Object o, Exception e) throws Exception {
}
}

 

 

 

 

2、springmvc.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<context:component-scan base-package="com.zbb.cn.controller" />
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<mvc:interceptors>
<!-- 后台拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/user/*.htmls"/>
<mvc:mapping path="/ztemp/*.htmls"/>
<mvc:mapping path="/form/*.htmls"/>
<mvc:mapping path="/config/*.htmls"/>
<mvc:mapping path="/active/*.htmls"/>
<mvc:mapping path="/role/*.htmls"/>
<mvc:mapping path="/custom/*.htmls"/>
<mvc:mapping path="/log/*.htmls"/>
<mvc:mapping path="/auth/*.htmls"/>
<mvc:mapping path="/baVerify/*.htmls"/>
<bean class="com.zbb.cn.filter.frontInterceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!-- json转换器 -->
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>

<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" /> -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="http://www.mamicode.com/WEB-INF/jsp"></property>
<property name="suffix" value="http://www.mamicode.com/.jsp"></property>
</bean>

<!-- 配置多文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<!-- 上传文件大小限制为31M,31*1024*1024 -->
<value>32505856</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>

<!--
首先是配置你要定时加载的目标类
<bean id="myTimer" class="com.timer.MyTimer"></bean>
定时器配置
<bean id="timeDitail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myTimer"></property>指定任务类
<property name="targetMethod" value="http://www.mamicode.com/doit"></property>指定任务方法
</bean>
-->
</beans>

springmvc权限过滤器