MvcConfig.java 1.53 KB
package com.xkl.config;

import com.xkl.authorization.interceptor.AuthorizationInterceptor;
import com.xkl.authorization.resolvers.CurrentAdminMethodArgumentResolver;
import com.xkl.authorization.resolvers.CurrentUserMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * 配置类,增加自定义拦截器和解析器
 * @see com.xkl.authorization.resolvers.CurrentUserMethodArgumentResolver
 * @see com.xkl.authorization.interceptor.AuthorizationInterceptor
 */
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;

    @Autowired
    private CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver;

    @Autowired
    private CurrentAdminMethodArgumentResolver currentAdminMethodArgumentResolver;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizationInterceptor);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentUserMethodArgumentResolver);
        argumentResolvers.add(currentAdminMethodArgumentResolver);
    }
}