Filter

Introduction

  • Filter is used to do user authorization , logging, ....

  • If the condition doesn't meet, the chain can be broken, so that logic of controller will not triggered

  • Add the filter

Implementation

  • The aim is to filter the controller

@GetMapping("/hello")
public String hello(){
    logger.info("coach" + coachConfig.getName()+ "  " + coachConfig.getUserId());
    return "hello: " + coachName;
}
  • Add the servlet scanning in main class, so as to scan the filter component

@SpringBootApplication
@ServletComponentScan
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

Flow

  • when user do a request -> entering first filter -> chain do filter -> entering second filter -> chain do filter -> entering controller method -> after finished controller method - > go back to first filter -> then second filter

The result

Last updated

Was this helpful?