You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2022/10/12 17:13:09 UTC
[cxf] branch main updated: Fixed DigestAuthSupplierSpringTest test case
This is an automated email from the ASF dual-hosted git repository.
reta pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new 683e2b1bbd Fixed DigestAuthSupplierSpringTest test case
683e2b1bbd is described below
commit 683e2b1bbd5f629d2d6d5726849f6f8d16298202
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Wed Oct 12 13:12:51 2022 -0400
Fixed DigestAuthSupplierSpringTest test case
---
systests/transport-hc5/pom.xml | 31 +++++
.../http/auth/DigestAuthSupplierSpringTest.java | 144 +++++++++++++++++++++
systests/transports/pom.xml | 5 +
3 files changed, 180 insertions(+)
diff --git a/systests/transport-hc5/pom.xml b/systests/transport-hc5/pom.xml
index ed504a078d..386d025562 100644
--- a/systests/transport-hc5/pom.xml
+++ b/systests/transport-hc5/pom.xml
@@ -169,5 +169,36 @@
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.security</groupId>
+ <artifactId>spring-security-config</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.security</groupId>
+ <artifactId>spring-security-web</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-test</artifactId>
+ <version>${cxf.spring.boot.version}</version>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-logging</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-web</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/systests/transport-hc5/src/test/java/org/apache/cxf/systest/http/auth/DigestAuthSupplierSpringTest.java b/systests/transport-hc5/src/test/java/org/apache/cxf/systest/http/auth/DigestAuthSupplierSpringTest.java
new file mode 100644
index 0000000000..7f23b54bc3
--- /dev/null
+++ b/systests/transport-hc5/src/test/java/org/apache/cxf/systest/http/auth/DigestAuthSupplierSpringTest.java
@@ -0,0 +1,144 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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.apache.cxf.systest.http.auth;
+
+import jakarta.ws.rs.NotAuthorizedException;
+import jakarta.ws.rs.core.MediaType;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.transport.http.HTTPConduit;
+import org.apache.cxf.transport.http.auth.DigestAuthSupplier;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.boot.test.web.server.LocalServerPort;
+import org.springframework.context.annotation.Bean;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint;
+import org.springframework.security.web.authentication.www.DigestAuthenticationFilter;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(
+ classes = {
+ DigestAuthSupplierSpringTest.SecurityConfig.class,
+ DigestAuthSupplierSpringTest.Controller.class
+ },
+ webEnvironment = WebEnvironment.RANDOM_PORT
+)
+@SpringBootApplication
+public class DigestAuthSupplierSpringTest {
+
+ private static final String USER = "alice";
+ private static final String PWD = "ecila";
+
+ @LocalServerPort
+ private int port;
+
+ @Test
+ public void test() {
+ WebClient client = WebClient.create("http://localhost:" + port, (String) null);
+
+ assertThrows(NotAuthorizedException.class, () -> client.get(String.class));
+
+ HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
+ conduit.setAuthSupplier(new DigestAuthSupplier());
+ conduit.getAuthorization().setUserName(USER);
+ conduit.getAuthorization().setPassword(PWD);
+
+ assertEquals(Controller.RESPONSE, client.get(String.class));
+ }
+
+ @RestController
+ static class Controller {
+
+ static final String RESPONSE = "Hi!";
+
+ @GetMapping(produces = MediaType.TEXT_PLAIN, value = "/")
+ public String get() {
+ return "Hi!";
+ }
+
+ }
+
+ static class SecurityConfig extends WebSecurityConfigurerAdapter {
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ DigestAuthenticationEntryPoint authenticationEntryPoint = digestAuthenticationEntryPoint();
+ http
+ .authorizeRequests().anyRequest().authenticated()
+ .and()
+ .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
+ .and()
+ .addFilter(digestAuthenticationFilter(authenticationEntryPoint));
+ }
+
+ private DigestAuthenticationFilter digestAuthenticationFilter(
+ DigestAuthenticationEntryPoint authenticationEntryPoint) {
+ DigestAuthenticationFilter digestAuthenticationFilter = new DigestAuthenticationFilter();
+ digestAuthenticationFilter.setUserDetailsService(userDetailsService());
+ digestAuthenticationFilter.setAuthenticationEntryPoint(authenticationEntryPoint);
+ return digestAuthenticationFilter;
+ }
+
+ private static DigestAuthenticationEntryPoint digestAuthenticationEntryPoint() {
+ DigestAuthenticationEntryPoint digestAuthenticationEntryPoint = new DigestAuthenticationEntryPoint();
+ digestAuthenticationEntryPoint.setKey("acegi");
+ digestAuthenticationEntryPoint.setRealmName("Digest Realm");
+ return digestAuthenticationEntryPoint;
+ }
+
+ @Override
+ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
+ auth.inMemoryAuthentication()
+ .withUser(USER).password(PWD).roles("");
+ }
+
+ @Bean
+ public static PasswordEncoder passwordEncoder() {
+ return new PasswordEncoder() {
+ @Override
+ public String encode(CharSequence rawPassword) {
+ return rawPassword.toString();
+ }
+ @Override
+ public boolean matches(CharSequence rawPassword, String encodedPassword) {
+ return rawPassword.toString().equals(encodedPassword);
+ }
+ };
+ }
+ }
+
+ public static void main(String[] args) {
+ SpringApplication.run(DigestAuthSupplierSpringTest.class, args);
+ }
+
+}
diff --git a/systests/transports/pom.xml b/systests/transports/pom.xml
index f96c4aec49..1115f457db 100644
--- a/systests/transports/pom.xml
+++ b/systests/transports/pom.xml
@@ -274,6 +274,11 @@
<version>${cxf.spring.boot.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents.client5</groupId>
+ <artifactId>httpclient5</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>