Cleanup and fix tests

This commit is contained in:
Oleg Zhurakousky
2023-07-20 16:50:16 +02:00
parent 8486e0bcb9
commit efda7285ad
7 changed files with 70 additions and 28 deletions

View File

@@ -1,11 +1,27 @@
/*
* Copyright 2023-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.adapter.azure.helper; package org.springframework.cloud.function.adapter.azure.helper;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.microsoft.azure.functions.HttpResponseMessage; import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatusType;
import com.microsoft.azure.functions.HttpResponseMessage.Builder; import com.microsoft.azure.functions.HttpResponseMessage.Builder;
import com.microsoft.azure.functions.HttpStatusType;
public class BuilderStub implements Builder { public class BuilderStub implements Builder {
@@ -36,4 +52,4 @@ public class BuilderStub implements Builder {
return new HttpResponseMessageStub(this.status, this.headers, this.body); return new HttpResponseMessageStub(this.status, this.headers, this.body);
} }
} }

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2023-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.adapter.azure.helper; package org.springframework.cloud.function.adapter.azure.helper;
import java.net.URI; import java.net.URI;
@@ -72,5 +88,4 @@ public class HttpRequestMessageStub<I> implements HttpRequestMessage<I> {
public Builder createResponseBuilder(HttpStatus status) { public Builder createResponseBuilder(HttpStatus status) {
return new BuilderStub().status(status); return new BuilderStub().status(status);
} }
}
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2023-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.adapter.azure.helper; package org.springframework.cloud.function.adapter.azure.helper;
import java.util.HashMap; import java.util.HashMap;
@@ -34,4 +50,4 @@ public class HttpResponseMessageStub implements HttpResponseMessage {
return this.body; return this.body;
} }
} }

View File

@@ -20,7 +20,6 @@ import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import com.microsoft.azure.functions.ExecutionContext; import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage; import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.annotation.AuthorizationLevel; import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName; import com.microsoft.azure.functions.annotation.FunctionName;

View File

@@ -20,7 +20,6 @@ import java.util.Iterator;
import java.util.Optional; import java.util.Optional;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.function.Function; import java.util.function.Function;
import java.util.logging.Logger;
import com.microsoft.azure.functions.ExecutionContext; import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod; import com.microsoft.azure.functions.HttpMethod;

View File

@@ -217,7 +217,7 @@ public class ProxyServletContext implements ServletContext {
@Override @Override
public Map<String, ? extends ServletRegistration> getServletRegistrations() { public Map<String, ? extends ServletRegistration> getServletRegistrations() {
throw new UnsupportedOperationException("This ServletContext does not represent a running web container"); return this.registrations;
} }
@Override @Override

View File

@@ -72,27 +72,24 @@ public class PetStoreSpringAppConfig {
@Bean @Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf().disable() // need for POST http
.addFilterBefore(new GenericFilterBean() { .addFilterBefore(new GenericFilterBean() {
@Override @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException { throws IOException, ServletException {
SecurityContext securityContext = SecurityContextHolder.getContext(); SecurityContext securityContext = SecurityContextHolder.getContext();
securityContext.setAuthentication(UsernamePasswordAuthenticationToken.authenticated("user", "password", securityContext.setAuthentication(UsernamePasswordAuthenticationToken.authenticated("user", "password",
Collections.singleton(new SimpleGrantedAuthority("USER")))); Collections.singleton(new SimpleGrantedAuthority("USER"))));
HttpSession session = ((HttpServletRequest) request).getSession(); HttpSession session = ((HttpServletRequest) request).getSession();
session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext); session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
chain.doFilter(request, response); chain.doFilter(request, response);
} }
}, SecurityContextHolderFilter.class) }, SecurityContextHolderFilter.class)
.authorizeHttpRequests((requests) -> requests .securityMatcher("/foo")
.requestMatchers("/", "/pets", "/pets/").hasAnyAuthority("USER") .authorizeHttpRequests(authorize -> authorize
.requestMatchers("/foo").hasAnyAuthority("FOO") .anyRequest().hasRole("FOO")
.anyRequest().authenticated()
) )
.exceptionHandling().accessDeniedHandler(accessDeniedHandler()).and() .exceptionHandling(f -> f.accessDeniedHandler(accessDeniedHandler()));
.logout((logout) -> logout.permitAll());
return http.build(); return http.build();
} }