一:定义拦截器
1.1:创建java类继承自WebMvcConfigurerAdapetr,并通过@Configruation注解进行标注
@Configuration
public class MyInterception extends WebMvcConfigurerAdapter {
1.2、重写父类addInterceptors方法,创建HandlerInterceptor对象
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
/**
* 拦截请求的方法
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("****************");
return true; //true=放行;false=拦截(直接大白页)
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
};
1.3、在addInterceptors方法中,将HandlerInterceptor对象放到InterceptorRegistry对象中,并通过addPathPatterns方法定义拦截规则
registry.addInterceptor(handlerInterceptor).addPathPatterns("/**"); //拦截一切请求
二、全部源代码
package demo.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Configuration
public class MyInterceptor extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
/**
* 拦截请求的方法
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("****************");
return true; //true=放行;false=拦截(直接大白页)
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
};
registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
}
}
二、window环境下redis安装
redis的发行版应用于Linux操作系统,并没有提供windows操作系统的版本,网上流传的windows版本的redis属于linux的移植版本,该版本在windows操作系统上可以直接双击运行,无需配置任何环境
部分电脑出现双击之后无法运行的情况,请使用命令行运行:
redis-server.exe redis.windows.conf
三、使用jedis操作redis
1.1、创建web项目并在pom.xml中添加jedis依赖
<!-- jedis依赖,访问redis数据库-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- jedis依赖,访问redis数据库-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
3.2、创建Jedis对象向Redis数据库中保存数据
Redis是以键值对的形式保存数据的,这点和Java的HashMap很相似。
//创建Jedis对象用于操作redis数据库
Jedis jedis = new Jedis("127.0.0.1", 6379); //访问到redis服务器
//jedis.auth("Aa123123."); //设置范围redis服务器的密码
jedis.set("hello", "hello Redis"); //向redis中保存数据,以键值对的形式,思路和HashMap一致
//释放资源
jedis.close();
//创建Jedis对象用于操作redis数据库
Jedis jedis = new Jedis("127.0.0.1", 6379); //访问到redis服务器
//jedis.auth("Aa123123."); //设置范围redis服务器的密码
jedis.set("hello", "hello Redis"); //向redis中保存数据,以键值对的形式,思路和HashMap一致
//释放资源
jedis.close();
3.3、创建Jedis对象从Redis数据库中获取数据
//创建Jedis对象,访问Redis服务器(数据库)
Jedis jedis = new Jedis("127.0.0.1", 6379);
//通过key从reids中读取数据
String str = jedis.get("hello");
//释放资源
jedis.close();