spring-boot- study notes (c) - Filter

  Filters There are two configurations, one kind of annotation is done by a custom configuration class by setting

assumed here that the scene is, define a filter, filter all requests, if there is no username parameter information to redirect login_page to the login page, if it is released with a username

Notes -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 annotation tells the spring it is a filter
@Component // the Component annotation indicates that this is a component, you need to create an instance of a
public class the implements the Filter { @Override public void the init (FilterConfig FilterConfig ) throws ServletException { System.out.println ( "the init, the service starts is called" ); }





@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")) {
// 如果本身访问的是登录页面,直接放行
the chain.doFilter (Request, Response);
} the else { // No username, a non-access login page redirected to a login page the HttpServletResponse RESP = (the HttpServletResponse) Response; resp.sendRedirect ( "login_page" ); } }






}

@Override public void the destroy () { System.out.println ( "the destroy, in time to stop the service program will be called" ); } }





Code configuration

1. Do not use annotations in fitler implementation class

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 () { // create a filter register bean objects FilterRegistrationBean Bean1 = new new FilterRegistrationBean (); // set the object to configure a filter bean1.setFilter ( new new secondFilter ()); // add the filter rules to filter url bean1.addUrlPatterns ( "/ *" ); // set the priority level, a number of the first filter performs execution bean1.setOrder ( . 1 ); return Bean1; } }













Guess you like

Origin www.cnblogs.com/lijianming180/p/12037951.html
Recommended