spring-boot-学习笔记(三)-过滤器

  过滤器有两种配置方式,一种是通过注解来完成,一种是通过自定义配置类来设置

这里假设的场景是,定义一个过滤器,过滤所有请求,如果参数中没有username信息则重定向到login_page登录页面,如果带有username则放行

注解-WebFilter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.example.demo.myFilter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


* @author fengxi
* @date 2019-03-06 10:47
*/


(value = "/*") // WebFilter注解告诉spring这是一个过滤器
@Component // Component注解表示这是一个组件,需要创建出一个实例来
public class implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init,在服务启动的时候被调用");
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getRequestURI();
HttpSession session = req.getSession();
if (req.getParameter("username") != null){
// 放行
chain.doFilter(request, response);
}else {
System.out.println(path);
if (path.equals("/login_page")) {
// 如果本身访问的是登录页面,直接放行
chain.doFilter(request, response);
} else {
// 没有username,访问非登录页面,重定向到登录页面
HttpServletResponse resp = (HttpServletResponse) response;
resp.sendRedirect("login_page");
}
}

}

@Override
public void destroy() {
System.out.println("destroy,在停止服务程序的时候会调用");
}
}

代码配置

1。 在fitler实现类不使用注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
大专栏  spring-boot-学习笔记(三)-过滤器ss="line">25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.example.demo.myFilter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


* @author fengxi
* @date 2019-03-06 15:31
*/
public class secondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init");
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getRequestURI();
HttpSession session = req.getSession();
if (req.getParameter("username") != null){
chain.doFilter(request, response);
}else {
System.out.println(path);
if (path.equals("/login_page")) {
chain.doFilter(request, response);
} else {
HttpServletResponse resp = (HttpServletResponse) response;
resp.sendRedirect("login_page");
}
}
}

@Override
public void destroy() {
System.out.println("destroy");
}
}

2。 使用configuration注解定义过滤器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.example.demo.config;

import com.example.demo.myFilter.secondFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


* @author fengxi
* @date 2019-03-06 15:30
*/

@Configuration
public class myConfig {

@Bean("bean1")
public FilterRegistrationBean registerFilter1(){
// 创建过滤器注册bean对象
FilterRegistrationBean bean1 = new FilterRegistrationBean();
// 设置要配置的过滤器对象
bean1.setFilter(new secondFilter());
// 添加该过滤器要过滤的url规则
bean1.addUrlPatterns("/*");
// 设置过滤器执行的优先级,数字小的先执行
bean1.setOrder(1);
return bean1;
}
}

猜你喜欢

转载自www.cnblogs.com/lijianming180/p/12037951.html