Skip to content

Commit bca11cd

Browse files
authored
refactor(security): extract WebAuthn ObjectPostProcessor into helper method (#261)
* refactor(security): extract WebAuthn ObjectPostProcessor into helper method Replace inline anonymous ObjectPostProcessor in setupWebAuthn() with an extracted private method webAuthnSuccessHandlerPostProcessor(). This removes fully-qualified type names, reduces nesting, and aligns with the concise method-extraction style used elsewhere in WebSecurityConfig. Closes #255 * docs(security): improve JavaDoc return description on webAuthnSuccessHandlerPostProcessor Address PR review feedback: the previous @return tag added no information beyond the method name. Updated to describe what the post processor does.
1 parent 5ad6f28 commit bca11cd

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

src/main/java/com/digitalsanctuary/spring/user/security/WebSecurityConfig.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.security.authentication.AuthenticationEventPublisher;
1818
import org.springframework.security.authentication.DefaultAuthenticationEventPublisher;
1919
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
20+
import org.springframework.security.config.ObjectPostProcessor;
2021
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2122
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
2223
import org.springframework.security.core.session.SessionRegistry;
@@ -26,6 +27,7 @@
2627
import org.springframework.security.crypto.password.PasswordEncoder;
2728
import org.springframework.security.web.SecurityFilterChain;
2829
import org.springframework.security.web.session.HttpSessionEventPublisher;
30+
import org.springframework.security.web.webauthn.authentication.WebAuthnAuthenticationFilter;
2931
import com.digitalsanctuary.spring.user.roles.RolesAndPrivilegesConfig;
3032
import com.digitalsanctuary.spring.user.service.DSOAuth2UserService;
3133
import com.digitalsanctuary.spring.user.service.DSOidcUserService;
@@ -224,15 +226,22 @@ private void setupWebAuthn(HttpSecurity http) throws Exception {
224226

225227
http.webAuthn(webAuthn -> webAuthn.rpName(webAuthnConfigProperties.getRpName()).rpId(webAuthnConfigProperties.getRpId())
226228
.allowedOrigins(normalizedAllowedOrigins)
227-
.withObjectPostProcessor(
228-
new org.springframework.security.config.ObjectPostProcessor<org.springframework.security.web.webauthn.authentication.WebAuthnAuthenticationFilter>() {
229-
@Override
230-
public <O extends org.springframework.security.web.webauthn.authentication.WebAuthnAuthenticationFilter> O postProcess(
231-
O filter) {
232-
filter.setAuthenticationSuccessHandler(new WebAuthnAuthenticationSuccessHandler(userDetailsService));
233-
return filter;
234-
}
235-
}));
229+
.withObjectPostProcessor(webAuthnSuccessHandlerPostProcessor()));
230+
}
231+
232+
/**
233+
* Creates an ObjectPostProcessor that sets our custom WebAuthn success handler on the WebAuthnAuthenticationFilter.
234+
*
235+
* @return an ObjectPostProcessor that injects a custom authentication success handler
236+
*/
237+
private ObjectPostProcessor<WebAuthnAuthenticationFilter> webAuthnSuccessHandlerPostProcessor() {
238+
return new ObjectPostProcessor<WebAuthnAuthenticationFilter>() {
239+
@Override
240+
public <O extends WebAuthnAuthenticationFilter> O postProcess(O filter) {
241+
filter.setAuthenticationSuccessHandler(new WebAuthnAuthenticationSuccessHandler(userDetailsService));
242+
return filter;
243+
}
244+
};
236245
}
237246

238247
// Commenting this out to try adding /error to the unprotected URIs list instead

0 commit comments

Comments
 (0)