001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.web.cors;
017
018import com.jfinal.ext.cors.EnableCORS;
019import io.jboot.utils.AnnotationUtil;
020
021import java.util.Map;
022import java.util.Objects;
023import java.util.concurrent.ConcurrentHashMap;
024
025public class CORSConfig {
026
027    private static final CORSConfig DEFAULT_CONFIG = new CORSConfig();
028    private static final Map<Integer, CORSConfig> cache = new ConcurrentHashMap<>();
029
030    private String allowOrigin = "*";
031    private String allowCredentials = "true";
032    private String allowHeaders = "Origin,X-Requested-With,Content-Type,Accept,Authorization,Jwt";
033    private String allowMethods = "GET,PUT,POST,DELETE,PATCH,OPTIONS";
034    private String exposeHeaders = "";
035    private String requestHeaders = "";
036    private String requestMethod = "";
037    private String origin = "";
038    private String maxAge = "3600";
039
040    public static CORSConfig getDefaultConfig() {
041        return DEFAULT_CONFIG;
042    }
043
044    public static CORSConfig fromAnnotation(EnableCORS enableCORS) {
045        int identityHashCode = System.identityHashCode(enableCORS);
046        CORSConfig corsConfig = cache.get(identityHashCode);
047        if (corsConfig == null) {
048            corsConfig = new CORSConfig(enableCORS);
049            if (corsConfig.equals(DEFAULT_CONFIG)) {
050                corsConfig = DEFAULT_CONFIG;
051            }
052            cache.put(identityHashCode, corsConfig);
053        }
054        return corsConfig;
055    }
056
057
058    public CORSConfig() {
059    }
060
061    public CORSConfig(String allowOrigin) {
062        this.allowOrigin = allowOrigin;
063    }
064
065    public CORSConfig(String allowOrigin, String allowHeaders) {
066        this.allowOrigin = allowOrigin;
067        this.allowHeaders = allowHeaders;
068    }
069
070    public CORSConfig(String allowOrigin
071            , String allowCredentials
072            , String allowHeaders
073            , String allowMethods
074            , String exposeHeaders
075            , String requestHeaders
076            , String requestMethod
077            , String origin
078            , String maxAge) {
079        this.allowOrigin = allowOrigin;
080        this.allowCredentials = allowCredentials;
081        this.allowHeaders = allowHeaders;
082        this.allowMethods = allowMethods;
083        this.exposeHeaders = exposeHeaders;
084        this.requestHeaders = requestHeaders;
085        this.requestMethod = requestMethod;
086        this.origin = origin;
087        this.maxAge = maxAge;
088    }
089
090    public CORSConfig(EnableCORS enableCORS) {
091        this.allowOrigin = AnnotationUtil.get(enableCORS.allowOrigin());
092        this.allowCredentials = AnnotationUtil.get(enableCORS.allowCredentials());
093        this.allowHeaders = AnnotationUtil.get(enableCORS.allowHeaders());
094        this.allowMethods = AnnotationUtil.get(enableCORS.allowMethods());
095        this.exposeHeaders = AnnotationUtil.get(enableCORS.exposeHeaders());
096        this.requestHeaders = AnnotationUtil.get(enableCORS.requestHeaders());
097        this.requestMethod = AnnotationUtil.get(enableCORS.requestMethod());
098        this.origin = AnnotationUtil.get(enableCORS.origin());
099        this.maxAge = AnnotationUtil.get(enableCORS.maxAge());
100    }
101
102
103    public String getAllowOrigin() {
104        return allowOrigin;
105    }
106
107    public void setAllowOrigin(String allowOrigin) {
108        this.allowOrigin = allowOrigin;
109    }
110
111    public String getAllowCredentials() {
112        return allowCredentials;
113    }
114
115    public void setAllowCredentials(String allowCredentials) {
116        this.allowCredentials = allowCredentials;
117    }
118
119    public String getAllowHeaders() {
120        return allowHeaders;
121    }
122
123    public void setAllowHeaders(String allowHeaders) {
124        this.allowHeaders = allowHeaders;
125    }
126
127    public String getAllowMethods() {
128        return allowMethods;
129    }
130
131    public void setAllowMethods(String allowMethods) {
132        this.allowMethods = allowMethods;
133    }
134
135    public String getExposeHeaders() {
136        return exposeHeaders;
137    }
138
139    public void setExposeHeaders(String exposeHeaders) {
140        this.exposeHeaders = exposeHeaders;
141    }
142
143    public String getRequestHeaders() {
144        return requestHeaders;
145    }
146
147    public void setRequestHeaders(String requestHeaders) {
148        this.requestHeaders = requestHeaders;
149    }
150
151    public String getRequestMethod() {
152        return requestMethod;
153    }
154
155    public void setRequestMethod(String requestMethod) {
156        this.requestMethod = requestMethod;
157    }
158
159    public String getOrigin() {
160        return origin;
161    }
162
163    public void setOrigin(String origin) {
164        this.origin = origin;
165    }
166
167    public String getMaxAge() {
168        return maxAge;
169    }
170
171    public void setMaxAge(String maxAge) {
172        this.maxAge = maxAge;
173    }
174
175    @Override
176    public boolean equals(Object o) {
177        if (this == o) {
178            return true;
179        }
180        if (o == null || getClass() != o.getClass()) {
181            return false;
182        }
183        CORSConfig that = (CORSConfig) o;
184        return Objects.equals(allowOrigin, that.allowOrigin)
185                && Objects.equals(allowCredentials, that.allowCredentials)
186                && Objects.equals(allowHeaders, that.allowHeaders)
187                && Objects.equals(allowMethods, that.allowMethods)
188                && Objects.equals(exposeHeaders, that.exposeHeaders)
189                && Objects.equals(requestHeaders, that.requestHeaders)
190                && Objects.equals(requestMethod, that.requestMethod)
191                && Objects.equals(origin, that.origin)
192                && Objects.equals(maxAge, that.maxAge);
193    }
194
195    @Override
196    public int hashCode() {
197        return Objects.hash(allowOrigin, allowCredentials, allowHeaders, allowMethods, exposeHeaders, requestHeaders, requestMethod, origin, maxAge);
198    }
199}