Skip to content

Commit 7749cca

Browse files
authored
Merge pull request #438 from FlowCI/feature/1610
Feature/1610
2 parents e86a81b + bb40423 commit 7749cca

File tree

5 files changed

+89
-63
lines changed

5 files changed

+89
-63
lines changed

core/src/main/java/com/flowci/core/auth/controller/WebAuth.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.flowci.exception.AuthenticationException;
2626
import com.flowci.util.StringHelper;
2727
import com.google.common.base.Strings;
28+
import lombok.AllArgsConstructor;
2829
import org.springframework.beans.factory.annotation.Autowired;
2930
import org.springframework.messaging.MessageHeaders;
3031
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
@@ -43,17 +44,16 @@
4344
* @author yang
4445
*/
4546
@Component("webAuth")
47+
@AllArgsConstructor
4648
public class WebAuth implements HandlerInterceptor {
4749

4850
private static final String HeaderToken = "Token";
4951

5052
private static final String ParameterToken = "token";
5153

52-
@Autowired
53-
private AuthService authService;
54+
private final AuthService authService;
5455

55-
@Autowired
56-
private SessionManager sessionManager;
56+
private final SessionManager sessionManager;
5757

5858
/**
5959
* Get user object from ws message header
@@ -73,7 +73,7 @@ public User validate(MessageHeaders headers) {
7373
}
7474

7575
Optional<User> user = authService.get(token);
76-
if (!user.isPresent()) {
76+
if (user.isEmpty()) {
7777
throw new AuthenticationException("Invalid token");
7878
}
7979

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2018 flow.ci
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.flowci.core.common.adviser;
18+
19+
import org.springframework.web.bind.annotation.RequestMethod;
20+
21+
import javax.servlet.*;
22+
import javax.servlet.annotation.WebFilter;
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
import java.io.IOException;
26+
27+
/**
28+
* @author yang
29+
*/
30+
@WebFilter(urlPatterns = "/*", filterName = "corsFilter")
31+
public class CorsFilter implements Filter {
32+
33+
private static final String AllowedHeaders =
34+
"Origin, X-Requested-With, Content-Disposition, Content-Type, Accept, Token, Authorization";
35+
36+
private static final String AllowedMethods = "GET, POST, PATCH, OPTIONS, DELETE";
37+
38+
@Override
39+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
40+
if (!isHttpRequest(request, response)) {
41+
chain.doFilter(request, response);
42+
return;
43+
}
44+
45+
var httpRequest = (HttpServletRequest) request;
46+
var httpResponse = (HttpServletResponse) response;
47+
48+
httpResponse.setHeader("Access-Control-Allow-Origin", "*");
49+
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
50+
httpResponse.setHeader("Access-Control-Allow-Methods", AllowedMethods);
51+
httpResponse.setHeader("Access-Control-Max-Age", "1800");
52+
httpResponse.setHeader("Access-Control-Allow-Headers", AllowedHeaders);
53+
54+
if (httpRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
55+
httpResponse.setStatus(HttpServletResponse.SC_OK);
56+
return;
57+
}
58+
59+
chain.doFilter(request, response);
60+
}
61+
62+
private static boolean isHttpRequest(ServletRequest request, ServletResponse response) {
63+
return (request instanceof HttpServletRequest) && (response instanceof HttpServletResponse);
64+
}
65+
}

core/src/main/java/com/flowci/core/common/adviser/CrosInterceptor.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

core/src/main/java/com/flowci/core/common/config/WebConfig.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,61 @@
1717
package com.flowci.core.common.config;
1818

1919
import com.fasterxml.jackson.databind.ObjectMapper;
20-
import com.flowci.core.common.adviser.CrosInterceptor;
20+
import com.flowci.core.common.adviser.CorsFilter;
2121
import com.flowci.core.common.helper.JacksonHelper;
2222
import com.flowci.core.plugin.domain.Plugin;
2323
import com.flowci.domain.Vars;
2424
import com.google.common.collect.ImmutableList;
25+
import lombok.AllArgsConstructor;
2526
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2628
import org.springframework.context.annotation.Bean;
2729
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.core.Ordered;
31+
import org.springframework.core.annotation.Order;
2832
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
2933
import org.springframework.http.converter.HttpMessageConverter;
3034
import org.springframework.http.converter.ResourceHttpMessageConverter;
3135
import org.springframework.http.converter.StringHttpMessageConverter;
3236
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
3337
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
3438
import org.springframework.web.servlet.HandlerInterceptor;
35-
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
36-
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
37-
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
38-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
39+
import org.springframework.web.servlet.config.annotation.*;
3940

41+
import javax.servlet.Filter;
4042
import java.nio.file.Path;
4143
import java.util.HashMap;
4244
import java.util.List;
4345
import java.util.Map;
4446

4547
@EnableWebMvc
4648
@Configuration
49+
@AllArgsConstructor
4750
public class WebConfig {
4851

49-
@Autowired
50-
private HandlerInterceptor apiAuth;
52+
private final HandlerInterceptor apiAuth;
5153

52-
@Autowired
53-
private HandlerInterceptor webAuth;
54+
private final HandlerInterceptor webAuth;
5455

55-
@Autowired
56-
private AppProperties appProperties;
56+
private final AppProperties appProperties;
5757

5858
@Bean("staticResourceDir")
5959
public Path staticResourceDir() {
6060
return appProperties.getSiteDir();
6161
}
6262

63+
@Bean
64+
@Order(Ordered.HIGHEST_PRECEDENCE)
65+
@ConditionalOnProperty(prefix = "app", name = "cors", havingValue = "true")
66+
public Filter corsFilter() {
67+
return new CorsFilter();
68+
}
69+
6370
@Bean
6471
public WebMvcConfigurer webMvcConfigurer() {
6572
return new WebMvcConfigurer() {
6673
@Override
6774
public void addInterceptors(InterceptorRegistry registry) {
68-
registry.addInterceptor(new CrosInterceptor());
69-
7075
registry.addInterceptor(webAuth)
7176
.addPathPatterns("/users/**")
7277
.excludePathPatterns("/users/default")

core/src/main/resources/flow.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ app.default-smtp-config=true
88
app.socket-container=true
99
app.core-pool-size=100
1010
app.max-pool-size=200
11+
app.cors=true
1112

1213
app.auth.enabled=true
1314
app.auth.expire-seconds=7200

0 commit comments

Comments
 (0)