Skip to content

Commit a705ee0

Browse files
committed
style: Fix Spotless trailing whitespace violations
1 parent 399f1ce commit a705ee0

2 files changed

Lines changed: 30 additions & 52 deletions

File tree

src/main/java/org/cbioportal/application/security/config/ApiSecurityConfig.java

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.cbioportal.application.security.config;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
35
import org.cbioportal.application.security.token.RestAuthenticationEntryPoint;
46
import org.cbioportal.application.security.token.TokenAuthenticationFilter;
57
import org.cbioportal.application.security.token.TokenAuthenticationSuccessHandler;
68
import org.cbioportal.legacy.service.DataAccessTokenService;
79
import org.cbioportal.legacy.utils.config.annotation.ConditionalOnProperty;
810
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.beans.factory.annotation.Value;
912
import org.springframework.context.annotation.Bean;
1013
import org.springframework.context.annotation.Configuration;
1114
import org.springframework.core.Ordered;
@@ -17,17 +20,14 @@
1720
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
1821
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1922
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
20-
import org.springframework.beans.factory.annotation.Value;
21-
import java.util.ArrayList;
22-
import java.util.List;
23-
import org.springframework.security.web.util.matcher.AndRequestMatcher;
24-
import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
25-
import org.springframework.security.web.util.matcher.OrRequestMatcher;
26-
import org.springframework.security.web.util.matcher.RequestMatcher;
2723
import org.springframework.security.web.SecurityFilterChain;
2824
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
2925
import org.springframework.security.web.context.SecurityContextHolderFilter;
26+
import org.springframework.security.web.util.matcher.AndRequestMatcher;
3027
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
28+
import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
29+
import org.springframework.security.web.util.matcher.OrRequestMatcher;
30+
import org.springframework.security.web.util.matcher.RequestMatcher;
3131

3232
@Configuration
3333
@ConditionalOnProperty(
@@ -36,13 +36,10 @@
3636
isNot = true)
3737
public class ApiSecurityConfig {
3838

39-
// Add security filter chains that handle calls to the API endpoints.
40-
// Different chains are added for the '/api' and legacy '/webservice.do' paths.
41-
// Both are able to handle API tokens provided in the request.
42-
// see: "Creating and Customizing Filter Chains" @
43-
// https://spring.io/guides/topicals/spring-security-architecture
39+
@Value("${api.access.token.required:false}")
40+
private boolean accessTokenRequired;
4441

45-
private static final String[] PUBLIC_API_Matchers = {
42+
static final String[] PUBLIC_API_Matchers = {
4643
"/api/swagger-resources/**",
4744
"/api/swagger-ui.html",
4845
"/api/health",
@@ -79,40 +76,6 @@ public SecurityFilterChain securityFilterChain(
7976
return http.build();
8077
}
8178

82-
// ... (rest of class)
83-
84-
class ApiTokenFilterDsl extends AbstractHttpConfigurer<ApiTokenFilterDsl, HttpSecurity> {
85-
// ... (fields)
86-
87-
@Override
88-
public void configure(HttpSecurity http) {
89-
AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
90-
TokenAuthenticationSuccessHandler tokenAuthenticationSuccessHandler =
91-
new TokenAuthenticationSuccessHandler();
92-
TokenAuthenticationFilter filter =
93-
new TokenAuthenticationFilter(
94-
"/**", authenticationManager, tokenService, accessTokenRequired);
95-
96-
// Explicitly set the request matcher to exclude public paths if enforcement is enabled
97-
if (accessTokenRequired) {
98-
// Filter applies to /api/** BUT NOT the public paths
99-
List<RequestMatcher> matchers = new ArrayList<>();
100-
matchers.add(new AntPathRequestMatcher("/api/**"));
101-
102-
List<RequestMatcher> publicMatchers = new ArrayList<>();
103-
for (String pattern : ApiSecurityConfig.PUBLIC_API_Matchers) {
104-
publicMatchers.add(new AntPathRequestMatcher(pattern));
105-
}
106-
matchers.add(new NegatedRequestMatcher(new OrRequestMatcher(publicMatchers)));
107-
108-
filter.setRequiresAuthenticationRequestMatcher(new AndRequestMatcher(matchers));
109-
}
110-
111-
filter.setAuthenticationSuccessHandler(tokenAuthenticationSuccessHandler);
112-
http.addFilterAfter(filter, SecurityContextHolderFilter.class);
113-
}
114-
}
115-
11679
@Autowired
11780
public void buildAuthenticationManager(
11881
AuthenticationManagerBuilder authenticationManagerBuilder,
@@ -132,11 +95,10 @@ public RestAuthenticationEntryPoint restAuthenticationEntryPoint() {
13295

13396
class ApiTokenFilterDsl extends AbstractHttpConfigurer<ApiTokenFilterDsl, HttpSecurity> {
13497

135-
private boolean accessTokenRequired;
136-
98+
private final boolean accessTokenRequired;
13799
private final DataAccessTokenService tokenService;
138100

139-
public ApiTokenFilterDsl(DataAccessTokenService tokenService, boolean accessTokenRequired) {
101+
private ApiTokenFilterDsl(DataAccessTokenService tokenService, boolean accessTokenRequired) {
140102
this.tokenService = tokenService;
141103
this.accessTokenRequired = accessTokenRequired;
142104
}
@@ -149,6 +111,22 @@ public void configure(HttpSecurity http) {
149111
TokenAuthenticationFilter filter =
150112
new TokenAuthenticationFilter(
151113
"/**", authenticationManager, tokenService, accessTokenRequired);
114+
115+
// Explicitly set the request matcher to exclude public paths if enforcement is enabled
116+
if (accessTokenRequired) {
117+
// Filter applies to /api/** BUT NOT the public paths
118+
List<RequestMatcher> matchers = new ArrayList<>();
119+
matchers.add(new AntPathRequestMatcher("/api/**"));
120+
121+
List<RequestMatcher> publicMatchers = new ArrayList<>();
122+
for (String pattern : ApiSecurityConfig.PUBLIC_API_Matchers) {
123+
publicMatchers.add(new AntPathRequestMatcher(pattern));
124+
}
125+
matchers.add(new NegatedRequestMatcher(new OrRequestMatcher(publicMatchers)));
126+
127+
filter.setRequiresAuthenticationRequestMatcher(new AndRequestMatcher(matchers));
128+
}
129+
152130
filter.setAuthenticationSuccessHandler(tokenAuthenticationSuccessHandler);
153131
http.addFilterAfter(filter, SecurityContextHolderFilter.class);
154132
}

src/main/java/org/cbioportal/application/security/token/TokenAuthenticationFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ protected void successfulAuthentication(
128128
Authentication authResult)
129129
throws IOException, ServletException {
130130
super.successfulAuthentication(request, response, chain, authResult);
131-
131+
132132
boolean mdcSet = false;
133133
if (authResult != null && authResult.getName() != null) {
134134
MDC.put("user", authResult.getName());
135135
MDC.put("auth_method", "token");
136136
mdcSet = true;
137137
}
138-
138+
139139
try {
140140
chain.doFilter(request, response);
141141
} finally {

0 commit comments

Comments
 (0)