New posts, please correct me after spraying, thank you 1. King of, Pit pit
Like many others, I wrote a Webconfig class to inherit webmvcconfigureadapter, rewrite addresourcehandlers when Springboo custom configuration login interception, Then cheerfully go to implement Handlerinterceptor, write logic in Prehandle, then visit the login page, then what a fuck, I login page style? How many boxes are there? Black face question?
Project directory:
Static path for properties configuration: spring.mvc.static-path-pattern=/resources/**
The static file configuration and interception configuration in Webconfig is as follows:
@Override Public voidaddresourcehandlers (Resourcehandlerregistry registry) {//Configure template Resource pathRegistry.addresourcehandler ("/templates/**"). Addresourcelocations (resourceutils.classpath_url_prefix+"/templates/"); Registry.addresourcehandler ("/static/**"). Addresourcelocations (resourceutils.classpath_url_prefix+"/static/"); } @Override Public voidaddinterceptors (Interceptorregistry registry) {//interceptorregistration addinterceptor = Registry.addinterceptor (Getsecurityinterceptor ()); //it cannot be manipulated in the following way, if that is not possible. This equates to the creation of multiple interceptor. And not just one interceptor .//addinterceptor.excludepathpatterns ("/login");//addinterceptor.excludepathpatterns ("/login/main");// //Intercept all Paths//addinterceptor.addpathpatterns ("/**"); //addpathpatterns ("/**") intercepts all requests, but excludes interception of/login/main and/login requestsRegistry.addinterceptor (Getsecurityinterceptor ()). Addpathpatterns ("/**"). Excludepathpatterns ("/login","/login/main"); } @Bean Publicmyinterceptor Getsecurityinterceptor () {return NewMyinterceptor (); }
/** * Prior to controller execution*/@Override Publicboolean prehandle (httpservletrequest request, httpservletresponse response, Object handler) throws Excepti On {log.info ("start detecting whether to log in"); Log.info (Request.getrequesturl (). toString ()); //determine if the user has logged in to the session if(Request.getsession (). getattribute (session_key)! =NULL) {Log.info ("user is logged in"); return true; } log.info ("user not logged in"); //Jump to login pageResponse.sendredirect (Request.getcontextpath () +"/login"); return false; } @Override Public voidPosthandle (httpservletrequest httpservletrequest, HttpServletResponse httpservletresponse, Object o, Modelandview Modelandview) throws Exception {} @Override Public voidaftercompletion (httpservletrequest httpservletrequest, HttpServletResponse httpservletresponse, Object o, Exception e) throws Exception {}
The print request address output is found (Log.info (Request.getrequesturl (). toString ())), static pages under static are intercepted, and a face is added to the question mark.
2. Workaround
The online search of a lot of information, did not mention the point, the way later found that the reason is: The version is actually:
The spring boot 2.x has been changed to a minimum of JDK8 versions, and the interface in JDK8 allows for a default implementation, so the Webmvcconfigureradapter adaptation class has been discarded and the Webmvcconfigurer interface is implemented directly.
The above information is from the help of ==>https://my.oschina.net/dengfuwei/blog/1795346.
Then Webconfig implements Webmvcconfigurer, is to realize ah webmvcconfigurer ... Then modify the interception, annotations only left @configuration.
Registry.addinterceptor (Getsecurityinterceptor ()) . Addpathpatterns ("/**" ) . Excludepathpatterns ("/login","/login/main ","/static/**");
Restart Project, visit: Http://localhost:8080/login, perfect
springboot2.x custom intercept put static files to intercept pits