11package org .cbioportal .application .security .config ;
22
3+ import java .util .ArrayList ;
4+ import java .util .List ;
35import org .cbioportal .application .security .token .RestAuthenticationEntryPoint ;
46import org .cbioportal .application .security .token .TokenAuthenticationFilter ;
57import org .cbioportal .application .security .token .TokenAuthenticationSuccessHandler ;
68import org .cbioportal .legacy .service .DataAccessTokenService ;
79import org .cbioportal .legacy .utils .config .annotation .ConditionalOnProperty ;
810import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .beans .factory .annotation .Value ;
912import org .springframework .context .annotation .Bean ;
1013import org .springframework .context .annotation .Configuration ;
1114import org .springframework .core .Ordered ;
1720import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
1821import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1922import 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 ;
2723import org .springframework .security .web .SecurityFilterChain ;
2824import org .springframework .security .web .authentication .HttpStatusEntryPoint ;
2925import org .springframework .security .web .context .SecurityContextHolderFilter ;
26+ import org .springframework .security .web .util .matcher .AndRequestMatcher ;
3027import 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 (
3636 isNot = true )
3737public 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
13396class 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 }
0 commit comments