huliguo
2025-05-06 03e22f45b1b06b68a3ba8b9390e9a5f1ddda752a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.cl.config;
 
 
 
import com.cl.common.json.JacksonObjectMapper;
import com.cl.interceptor.JwtTokenInterceptor;
import com.cl.util.LoginAttemptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 配置类,注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
 
    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;
 
 
 
    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/user/**","/data/**","/institution/**")
                .excludePathPatterns("/user/login","user/loginOut");
    }
 
    /**
     * 通过knife4j生成接口文档
     * @return
     */
    @Bean
    public Docket docket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("残联大屏项目接口文档")
                .version("3.0")
                .description("残联大屏项目接口文档")
                .build();
        // 定义请求头参数
        ParameterBuilder tokenParamBuilder = new ParameterBuilder();
        tokenParamBuilder.name("token") // 参数名
                .description("请求头token") // 描述
                .modelRef(new ModelRef("string"))
                .parameterType("header") // 参数类型为请求头
                .required(false) // 是否必填
                .build();
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(tokenParamBuilder.build());
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
 
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cl.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters)
                .apiInfo(apiInfo); // 全局设置请求头参数;
        return docket;
    }
 
 
    /**
     * 设置静态资源映射
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // For Springfox 3.x
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
 
    }
 
    /**
     * 扩展Spring MVC框架的消息转换器
     * @param converters
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展消息转换器");
        //创建一个消息装换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //需要为消息转换器设置一个对象转换器,对象装换器可以将java对象序列化为json数据
        converter.setObjectMapper(new JacksonObjectMapper());
        //将自己的消息转换器加入到容器中
        converters.add(0,converter);
    }
 
    /**
     * 登录密码错误次数校验
     */
    @Bean
    public LoginAttemptService loginAttemptService() {
        return new LoginAttemptService();
    }
}