To allow users with authority ZN_W to access all resources, including those restricted to ZN_BRIGAD, you need to adjust the order and specificity of your authorize rules in the SecurityFilterChain configuration.
The key principle here is that Spring Security checks the rules in the order they are defined. As soon as a rule matches, it applies the access control decision and moves on, without evaluating the rest of the rules. Therefore, if you want ZN_W users to have access to all resources, you should place that rule before the more specific rules.
Here's how you can adjust your configuration:
public SecurityFilterChain clientSecurityFilterChain(
HttpSecurity http,
ClientRegistrationRepository clientRegistrationRepository) throws Exception {
http.oauth2Login(Customizer.withDefaults());
http.logout((logout) -> {
var logoutSuccessHandler =
new OidcClientInitiatedLogoutSuccessHandler(clientRegistrationRepository);
logoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}/");
logout.logoutSuccessHandler(logoutSuccessHandler);
});
// Define your request matchers as before
RequestMatcher otkAdaptersDataTable = (request) ->
AntPathRequestMatcher.antMatcher("/fill1").matches(request) &&
"otkAdaptersData".equals(request.getParameter("func"));
RequestMatcher otkAdapterSaveBtn = (request) ->
AntPathRequestMatcher.antMatcher("/fill1").matches(request) &&
"otkAdapterSave".equals(request.getParameter("func"));
RequestMatcher otkAdapterDelBtn = (request) ->
AntPathRequestMatcher.antMatcher("/fill1").matches(request) &&
"otkAdapterDel".equals(request.getParameter("func")) && "POST".equals(request.getMethod());
RequestMatcher otkAdaptersDropDown = (request) ->
AntPathRequestMatcher.antMatcher("/fillFarbe").matches(request) &&
"otkAdapters".equals(request.getParameter("get"));
http.authorizeHttpRequests(authorize -> {
// Permit all for these
authorize.requestMatchers("/resources/**", "/scr.js", "/index.jsp").permitAll();
// Users with ZN_W can access any resource
authorize.requestMatchers("/**").hasAuthority("ZN_W"); // This will override more specific rules below
// Specific rules for ZN_BRIGAD, these will not be evaluated if /** rule above matches
// If you want to keep these, ensure users with ZN_W don't match these by using a different path or parameter
// Only Adapters for Brigadiers
authorize.requestMatchers(otkAdaptersDataTable, otkAdapterSaveBtn, otkAdapterDelBtn, otkAdaptersDropDown).hasAuthority("ZN_BRIGAD");
// You might not need these if /** rule for ZN_W users is correctly in place
//authorize.requestMatchers("/fillFarbe", "/fill1","/helper/**").hasAuthority("ZN_W");
// Deny all other requests
authorize.anyRequest().denyAll();
})
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.sessionFixation().migrateSession()
)
.csrf(csrf -> csrf.ignoringRequestMatchers(
otkAdapterDelBtn,
AntPathRequestMatcher.antMatcher("/resources/**"),
AntPathRequestMatcher.antMatcher("/scr.js"),
AntPathRequestMatcher.antMatcher("/index.jsp")
));
return http.build();
}