Skip to content

Commit 56f1e4e

Browse files
NFC-82 Improve auth code, remove ThymeleafWebAppConfiguration.java
Signed-off-by: Sander Kondratjev <[email protected]>
1 parent 0034a82 commit 56f1e4e

File tree

3 files changed

+5
-53
lines changed

3 files changed

+5
-53
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
4141
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;
4242
import org.thymeleaf.ITemplateEngine;
43-
import org.thymeleaf.web.servlet.JakartaServletWebApplication;
4443

4544
@Configuration
4645
@ConfigurationPropertiesScan
@@ -55,7 +54,6 @@ public SecurityFilterChain filterChain(
5554
AuthenticationConfiguration authConfig,
5655
ChallengeNonceGenerator challengeNonceGenerator,
5756
ITemplateEngine templateEngine,
58-
JakartaServletWebApplication webApp,
5957
WebEidMobileProperties webEidMobileProperties
6058
) throws Exception {
6159
return http
@@ -67,7 +65,7 @@ public SecurityFilterChain filterChain(
6765
.authenticationProvider(webEidAuthenticationProvider)
6866
.addFilterBefore(new WebEidMobileAuthInitFilter("/auth/mobile/init", "/auth/mobile/login", challengeNonceGenerator, webEidMobileProperties), UsernamePasswordAuthenticationFilter.class)
6967
.addFilterBefore(new WebEidChallengeNonceFilter("/auth/challenge", challengeNonceGenerator), UsernamePasswordAuthenticationFilter.class)
70-
.addFilterBefore(new WebEidLoginPageGeneratingFilter("/auth/mobile/login", "/auth/login", templateEngine, webApp), UsernamePasswordAuthenticationFilter.class)
68+
.addFilterBefore(new WebEidLoginPageGeneratingFilter("/auth/mobile/login", "/auth/login", templateEngine), UsernamePasswordAuthenticationFilter.class)
7169
.addFilterBefore(new WebEidAjaxLoginProcessingFilter("/auth/login", authConfig.getAuthenticationManager()), UsernamePasswordAuthenticationFilter.class)
7270
.logout(l -> l.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()))
7371
.headers(h -> h.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

example/src/main/java/eu/webeid/example/security/ui/WebEidLoginPageGeneratingFilter.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@
3333
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
3434
import org.springframework.security.web.util.matcher.RequestMatcher;
3535
import org.springframework.web.filter.OncePerRequestFilter;
36-
import org.springframework.web.servlet.support.RequestContextUtils;
3736
import org.thymeleaf.ITemplateEngine;
38-
import org.thymeleaf.context.WebContext;
39-
import org.thymeleaf.web.IWebExchange;
40-
import org.thymeleaf.web.servlet.JakartaServletWebApplication;
4137

4238
import java.io.IOException;
4339
import java.nio.charset.StandardCharsets;
@@ -47,18 +43,15 @@ public final class WebEidLoginPageGeneratingFilter extends OncePerRequestFilter
4743
private final RequestMatcher requestMatcher;
4844
private final String loginProcessingPath;
4945
private final ITemplateEngine templateEngine;
50-
private final JakartaServletWebApplication webApp;
5146

5247
public WebEidLoginPageGeneratingFilter(
5348
String path,
5449
String loginProcessingPath,
55-
ITemplateEngine templateEngine,
56-
JakartaServletWebApplication webApp
50+
ITemplateEngine templateEngine
5751
) {
5852
this.requestMatcher = PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.GET, path);
5953
this.loginProcessingPath = loginProcessingPath;
6054
this.templateEngine = templateEngine;
61-
this.webApp = webApp;
6255
}
6356

6457
@Override
@@ -74,16 +67,14 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull Ht
7467
csrf = (CsrfToken) request.getAttribute("_csrf");
7568
}
7669

77-
String html = renderTemplate(request, response, csrf);
70+
String html = renderTemplate(csrf);
7871
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
7972
response.setContentType(MediaType.TEXT_HTML_VALUE);
8073
response.getWriter().write(html);
8174
}
8275

83-
private String renderTemplate(HttpServletRequest request, HttpServletResponse response, CsrfToken csrf) {
84-
IWebExchange exchange = webApp.buildExchange(request, response);
85-
var locale = RequestContextUtils.getLocale(request);
86-
var ctx = new WebContext(exchange, locale);
76+
private String renderTemplate(CsrfToken csrf) {
77+
var ctx = new org.thymeleaf.context.Context();
8778
ctx.setVariable("loginProcessingPath", loginProcessingPath);
8879
ctx.setVariable("csrfHeaderName", csrf != null ? csrf.getHeaderName() : "X-CSRF-TOKEN");
8980
ctx.setVariable("csrfToken", csrf != null ? csrf.getToken() : "");

0 commit comments

Comments
 (0)