Skip to content

Commit 8b4bf6d

Browse files
committed
Use method injection to provide AuthTokenDTOAuthenticationProvider and AuthenticationConfiguration to filterChain(), move HttpSessionSecurityContextRepository creation into WebEidAjaxLoginProcessingFilter constructor, update README
WE2-860 Signed-off-by: Mart Somermaa <[email protected]>
1 parent 33faef1 commit 8b4bf6d

File tree

4 files changed

+10
-29
lines changed

4 files changed

+10
-29
lines changed

example/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Web eID only works over a HTTPS connection with a trusted HTTPS certificate.
1717
You can either setup a reverse HTTPS proxy during development or, alternatively, configure
1818
HTTPS support directly in the bundled web server. HTTPS configuration is described in more detail in section _[HTTPS support](#https-support)_ below.
1919

20-
You can use, for example, [_ngrok_](https://ngrok.com/) to get a reverse HTTPS proxy. Download _ngrok_ and run it in a terminal window by providing the protocol and Spring Boot application port arguments as follows:
20+
You can use, for example, [_ngrok_](https://ngrok.com/) or [_localtunnel_](https://theboroer.github.io/localtunnel-www/) to get a reverse HTTPS proxy. Download _ngrok_ and run it in a terminal window by providing the protocol and Spring Boot application port arguments as follows:
2121

2222
ngrok http 8080
2323

@@ -35,7 +35,7 @@ web-eid-auth-token:
3535
3636
### 3. Configure the trusted certificate authority certificates
3737
38-
The algorithm, which performs the validation of the Web eID authentication token, needs to know which intermediate certificate authorities (CA) are trusted to issue the eID authentication certificates. CA certificates are loaded either from `.cer` files in the profile-specific subdirectory of the [`certs`resource directory](src/main/resources/certs) or the [truststore file](src/main/resources/certs/prod/trusted_certificates.jks). By default, Estonian eID test CA certificates are included in the `dev` profile and production CA certificates in the `prod` profile.
38+
The algorithm, which performs the validation of the Web eID authentication token, needs to know which intermediate certificate authorities (CA) are trusted to issue the eID authentication certificates. CA certificates are loaded either from `.cer` files in the profile-specific subdirectory of the [`certs` resource directory](src/main/resources/certs) or the [truststore file](src/main/resources/certs/prod/trusted_certificates.jks). By default, Estonian eID test CA certificates are included in the `dev` profile and production CA certificates in the `prod` profile.
3939

4040
In case you need to provide your own CA certificates, either add the `.cer` files to the `src/main/resources/certs/{dev,prod}` profile-specific directory or add the certificates to the truststore file.
4141

example/src/main/java/eu/webeid/example/config/ApplicationConfiguration.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import eu.webeid.example.security.WebEidAjaxLoginProcessingFilter;
2727
import org.springframework.context.annotation.Bean;
2828
import org.springframework.context.annotation.Configuration;
29-
import org.springframework.security.authentication.AuthenticationManager;
3029
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
3130
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
3231
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -35,35 +34,19 @@
3534
import org.springframework.security.web.SecurityFilterChain;
3635
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
3736
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;
38-
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
39-
import org.springframework.security.web.context.SecurityContextRepository;
4037
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
4138
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
4239

4340
@Configuration
4441
@EnableWebSecurity
4542
@EnableMethodSecurity(securedEnabled = true)
4643
public class ApplicationConfiguration implements WebMvcConfigurer {
47-
final AuthTokenDTOAuthenticationProvider authTokenDTOAuthenticationProvider;
48-
final SecurityContextRepository securityContextRepository;
49-
50-
public ApplicationConfiguration(AuthTokenDTOAuthenticationProvider authTokenDTOAuthenticationProvider) {
51-
this.authTokenDTOAuthenticationProvider = authTokenDTOAuthenticationProvider;
52-
this.securityContextRepository = new HttpSessionSecurityContextRepository();
53-
}
54-
55-
@Bean
56-
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
57-
return authenticationConfiguration.getAuthenticationManager();
58-
}
5944

6045
@Bean
61-
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
62-
AuthenticationManager manager = authenticationManager(http.getSharedObject(AuthenticationConfiguration.class));
63-
46+
public SecurityFilterChain filterChain(HttpSecurity http, AuthTokenDTOAuthenticationProvider authTokenDTOAuthenticationProvider, AuthenticationConfiguration authConfig) throws Exception {
6447
return http
6548
.authenticationProvider(authTokenDTOAuthenticationProvider)
66-
.addFilterBefore(new WebEidAjaxLoginProcessingFilter("/auth/login", manager, securityContextRepository),
49+
.addFilterBefore(new WebEidAjaxLoginProcessingFilter("/auth/login", authConfig.getAuthenticationManager()),
6750
UsernamePasswordAuthenticationFilter.class)
6851
.logout(logout -> logout.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()))
6952
.headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))

example/src/main/java/eu/webeid/example/security/WebEidAjaxLoginProcessingFilter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
4343
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
4444
import org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy;
45+
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
4546
import org.springframework.security.web.context.SecurityContextRepository;
4647

4748
import java.io.IOException;
@@ -53,15 +54,14 @@ public class WebEidAjaxLoginProcessingFilter extends AbstractAuthenticationProce
5354

5455
public WebEidAjaxLoginProcessingFilter(
5556
String defaultFilterProcessesUrl,
56-
AuthenticationManager authenticationManager,
57-
SecurityContextRepository securityContextRepository
57+
AuthenticationManager authenticationManager
5858
) {
5959
super(defaultFilterProcessesUrl);
6060
this.setAuthenticationManager(authenticationManager);
6161
this.setAuthenticationSuccessHandler(new AjaxAuthenticationSuccessHandler());
6262
this.setAuthenticationFailureHandler(new AjaxAuthenticationFailureHandler());
6363
setSessionAuthenticationStrategy(new SessionFixationProtectionStrategy());
64-
this.securityContextRepository = securityContextRepository;
64+
this.securityContextRepository = new HttpSessionSecurityContextRepository();
6565
}
6666

6767
@Override

example/src/test/java/eu/webeid/example/security/WebEidAjaxLoginProcessingFilterTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package eu.webeid.example.security;
22

3+
import jakarta.servlet.http.HttpServletRequest;
4+
import jakarta.servlet.http.HttpServletResponse;
35
import org.junit.jupiter.api.Test;
46
import org.springframework.http.HttpMethod;
57
import org.springframework.security.authentication.AuthenticationManager;
68

7-
import jakarta.servlet.http.HttpServletRequest;
8-
import jakarta.servlet.http.HttpServletResponse;
99
import java.io.BufferedReader;
1010
import java.io.StringReader;
1111

1212
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1313
import static org.mockito.Mockito.mock;
1414
import static org.mockito.Mockito.when;
15-
import org.springframework.security.web.context.SecurityContextRepository;
1615

1716
class WebEidAjaxLoginProcessingFilterTest {
1817

@@ -32,10 +31,9 @@ void testAttemptAuthentication() throws Exception {
3231
when(request.getReader()).thenReturn(new BufferedReader(new StringReader(AUTH_TOKEN)));
3332

3433
final AuthenticationManager authenticationManager = mock(AuthenticationManager.class);
35-
final SecurityContextRepository securityContextRepository = mock(SecurityContextRepository.class);
3634

3735
assertDoesNotThrow(() ->
38-
new WebEidAjaxLoginProcessingFilter("/auth/login", authenticationManager, securityContextRepository)
36+
new WebEidAjaxLoginProcessingFilter("/auth/login", authenticationManager)
3937
.attemptAuthentication(request, response));
4038
}
4139
}

0 commit comments

Comments
 (0)