SpringBoot配置参数解析器(WebArgumentResolver)

##SpingBoot下的WebArgumentResolver

参数解析器
需要集成类型WebArgumentResolver
并在@Configuration配置类型中配置参数解析器

import javax.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;

public class ArgumentResolver implements WebArgumentResolver {
	@Override
	public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
		Class parameterType = methodParameter.getParameterType();
		if (parameterType != null) {
			HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
			if (parameterType.equals(User.class)) {
				return request.getAttribute(Constant.CURRENT_WX_USER_LOGIN);
			}
		}
		return UNRESOLVED;
	}
}
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Override
    public void addArgumentResolvers(List argumentResolvers) {
        argumentResolvers.add(new ServletWebArgumentResolverAdapter(new ArgumentResolver()));
    }
}

添加拦截器,并在配置文件中配置拦截器

import com.qf.common.constant.Constants;
import com.qf.entity.dto.Admin;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PermissionInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
                Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
                if (admin != null) {
                    request.setAttribute(Constants.LOGIN_ADMIN, admin);
                }
        }
        return super.preHandle(request, response, handler);
    }
}
import com.qf.admin.interceptor.PermissionInterceptor;
import com.qf.admin.resolver.ArgumentResolver;
import org.springframework.context.annotation.Bean;
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.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter;
import java.util.List;

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Bean
    public PermissionInterceptor permissionInterceptor() {
        return new PermissionInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(permissionInterceptor()) // 权限拦截器
                .addPathPatterns("/**") // 拦截所有
                .excludePathPatterns("/") // 排除拦截默认
                .excludePathPatterns("/css/**") // 排除拦截静态资源
                .excludePathPatterns("/js/**") // 排除拦截静态资源
                .excludePathPatterns("/favicon.ico") // 排除拦截静态资源
                .excludePathPatterns("/login"); // 排除拦截登录逻辑
        super.addInterceptors(registry);
    }
}

你可能感兴趣的:(微服务,-,教学)