Skip to content

Commit 7140015

Browse files
NFC-47 Review findings. Use PathPatternRequestMatcher. Remove POST request if statement. Remove doFilter method. Fix request matchers. Inline filterchain. Remove welcome page from permit all.
1 parent b88af9b commit 7140015

File tree

2 files changed

+5
-25
lines changed

2 files changed

+5
-25
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,19 @@
4545
public class ApplicationConfiguration implements WebMvcConfigurer {
4646

4747
@Bean
48-
SecurityFilterChain filterChain(
49-
HttpSecurity http,
50-
AuthTokenDTOAuthenticationProvider provider,
51-
AuthenticationConfiguration authConfig) throws Exception {
48+
public SecurityFilterChain filterChain(HttpSecurity http, AuthTokenDTOAuthenticationProvider authTokenDTOAuthenticationProvider, AuthenticationConfiguration authConfig) throws Exception {
5249

5350
var filter = new WebEidAjaxLoginProcessingFilter("/auth/login", authConfig.getAuthenticationManager());
5451

5552
return http
5653
.csrf(csrf -> csrf.ignoringRequestMatchers("/auth/login"))
5754
.authorizeHttpRequests(auth -> auth
58-
.requestMatchers("/", "/welcome", "/error").permitAll()
55+
.requestMatchers("/", "/error").permitAll()
5956
.requestMatchers("/auth/challenge", "/auth/mobile/challenge").permitAll()
6057
.requestMatchers(HttpMethod.GET, "/auth/eid/login").permitAll()
6158
.requestMatchers(
6259
"/favicon.ico",
63-
"/css/**", "/js/**", "/images/**", "/webjars/**"
60+
"/css/", "/files/", "/img/", "/js/"
6461
).permitAll()
6562
.anyRequest().authenticated()
6663
)

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy;
4747
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
4848
import org.springframework.security.web.context.SecurityContextRepository;
49+
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
4950

5051
import java.io.IOException;
5152

@@ -58,7 +59,7 @@ public WebEidAjaxLoginProcessingFilter(
5859
String defaultFilterProcessesUrl,
5960
AuthenticationManager authenticationManager
6061
) {
61-
super(defaultFilterProcessesUrl);
62+
super(PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.POST, defaultFilterProcessesUrl));
6263
this.setAuthenticationManager(authenticationManager);
6364
this.setAuthenticationSuccessHandler(new AjaxAuthenticationSuccessHandler());
6465
this.setAuthenticationFailureHandler(new AjaxAuthenticationFailureHandler());
@@ -69,10 +70,6 @@ public WebEidAjaxLoginProcessingFilter(
6970
@Override
7071
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
7172
throws AuthenticationException, IOException {
72-
if (!HttpMethod.POST.name().equals(request.getMethod())) {
73-
LOG.warn("HttpMethod not supported: {}", request.getMethod());
74-
throw new AuthenticationServiceException("HttpMethod not supported: " + request.getMethod());
75-
}
7673
final String contentType = request.getHeader("Content-type");
7774
if (contentType == null || !contentType.startsWith("application/json")) {
7875
LOG.warn("Content type not supported: {}", contentType);
@@ -92,18 +89,4 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
9289
super.successfulAuthentication(request, response, chain, authResult);
9390
securityContextRepository.saveContext(SecurityContextHolder.getContext(), request, response);
9491
}
95-
96-
@Override
97-
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
98-
throws IOException, ServletException {
99-
100-
HttpServletRequest request = (HttpServletRequest) req;
101-
102-
if (!HttpMethod.POST.matches(request.getMethod())) {
103-
chain.doFilter(req, res);
104-
return;
105-
}
106-
107-
super.doFilter(req, res, chain);
108-
}
10992
}

0 commit comments

Comments
 (0)