Login interception and permission interception implementations are similar to first customizing a "Defineadapter "class, this class I was used to put a custom configuration (such as custom request parameters, custom interceptors, etc.), integrated Webmvcconfigureradapter rewrite his addinterceptors method, Put your interceptor's implementation class You can join,The Defineadapter code is as follows:
@Configurationpublic class Defineadapter extends webmvcconfigureradapter { @Autowiredpublic roleinterceptor roleinterceptor;//user role blocker @Autowiredpublic logininterceptor logininterceptor;//user login blocker @Autowiredmeidarequestdatacustomargumentresolver meidarequestdatacustomargumentresolver;//Custom parameter binding //Custom parameter binding@Overridepublic void addargumentresolvers (listargumentresolvers) { Super. Addargumentresolvers (argumentresolvers); argumentresolvers. Add (meidarequestdatacustomargumentresolver); }//Custom Interceptor@Overridepublic void addinterceptors (interceptorregistry registry) { //user role blockerregistry. Addinterceptor (roleinterceptor). addpathpatterns ("/**"); //user login blockerregistry. Addinterceptor (logininterceptor). addpathpatterns ("/**"). addpathpatterns ("/*/login"). addpathpatterns ("/*/h5/login"). excludepathpatterns ("/switch/*"); Super. Addinterceptors (registry); } |
|
( custom parameter bindings can be used without a tube, you just don't have that piece of code!!) ) Addinterceptors: Add the implementation logic class for your custom interceptoraddpathpatterns: Add the request path you want to intercept, and if there are multiple paths, continue addpathpatterns excludepathpatterns: Add a request path that you do not need to intercept My interceptor specific implementation logic is Logininterceptor This class, the code is as follows:
@Servicepublic class Logininterceptor extends Handlerinterceptoradapter {{ protected static final Log logger = LOGFAC Tory.getlog (logininterceptor.class); public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler) { String userId = Request.getparameter ("UserId"); if (Stringutils.isnotblank (userId)) { return false; }}
inherit the Handlerinterceptoradapter abstract class, implement the Prehandle method, and write your interception logic inside. Finish it!
Spring boot uses interceptors for user login interception