广州市综治平台后端
xusd
3 days ago f4e5da604604308b9f754de7e863c180fcedd8c9
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cn.huge.base.config;
 
import cn.huge.base.common.utils.SpringContextUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
import java.util.List;
 
/**
 * @title: MVC配置
 * @description: MVC配置
 * @company: hugeinfo
 * @author: liyj
 * @time: 2021-11-05 16:51:48
 * @version: 1.0.0
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
 
    }
 
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedHeaders("*")
//                .allowCredentials(true)
//                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
//                .maxAge(3600);
//    }
 
    /**
     * 静态资源访问
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("static/**").addResourceLocations("classpath:/static/");
    }
 
    /**
     * 参数解析
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new HandlerMethodArgumentResolver() {
 
            @Override
            public boolean supportsParameter(MethodParameter parameter) {
                if (parameter.hasParameterAnnotation(CurrentUser.class)) {
                    return true;
                }
                return false;
            }
            @Nullable
            @Override
            public Object resolveArgument(MethodParameter parameter,
                    @Nullable ModelAndViewContainer mavContainer,
                    NativeWebRequest webRequest,
                    @Nullable WebDataBinderFactory binderFactory) throws Exception {
                String userId = webRequest.getHeader("Authorization-userId");
                if(userId == null){
                    throw new IllegalArgumentException("userId不能为空");
//                    //开发环境不校验
//                    if (SpringContextUtil.checkDev()){
//                        userId = "2204221720151007";
//                    }else {
//                        throw new IllegalArgumentException("userId不能为空");
//                    }
                }
                return userId;
            }
        });
        resolvers.add(new HandlerMethodArgumentResolver() {
 
            @Override
            public boolean supportsParameter(MethodParameter parameter) {
                if (parameter.hasParameterAnnotation(CurrentCust.class)) {
                    return true;
                }
                return false;
            }
            @Nullable
            @Override
            public Object resolveArgument(MethodParameter parameter,
                                          @Nullable ModelAndViewContainer mavContainer,
                                          NativeWebRequest webRequest,
                                          @Nullable WebDataBinderFactory binderFactory) throws Exception {
                String custId = webRequest.getHeader("Authorization-custId");
                if(custId == null){
                    throw new IllegalArgumentException("custId不能为空");
//                    //开发环境不校验
//                    if (SpringContextUtil.checkDev()){
//                        custId = "2204121717531081";
//                    }else {
//                        throw new IllegalArgumentException("custId不能为空");
//                    }
                }
                return custId;
            }
        });
    }
}
/**
 * -------------------_ooOoo_-------------------
 * ------------------o8888888o------------------
 * ------------------88" . "88------------------
 * ------------------(| -_- |)------------------
 * ------------------O\  =  /O------------------
 * ---------------____/`---'\____---------------
 * -------------.'  \\|     |//  `.-------------
 * ------------/  \\|||  :  |||//  \------------
 * -----------/  _||||| -:- |||||-  \-----------
 * -----------|   | \\\  -  /// |   |-----------
 * -----------| \_|  ''\---/''  |   |-----------
 * -----------\  .-\__  `-`  ___/-. /-----------
 * ---------___`. .'  /--.--\  `. . __----------
 * ------."" '<  `.___\_<|>_/___.'  >'"".-------
 * -----| | :  `- \`.;`\ _ /`;.`/ - ` : | |-----
 * -----\  \ `-.   \_ __\ /__ _/   .-` /  /-----
 * ======`-.____`-.___\_____/___.-`____.-'======
 * -------------------`=---='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * ---------佛祖保佑---hugeinfo---永无BUG----------
 */