I have configured OAuth Authentication with EntraID, but I’m having problems getting the view-based authentication to work, with the @RolesAllowed annotation etc.
I’m on Vaadin 24.9.6 and VaadinSecurityConfigurer is not available as described in the documentation. Am I missing something?
Also injecting the AuthenticationContext produces a “no such bean” exception.
Here is my SecurityConfiguration - this one works and handles all the frontend/backend communication etc. How can I get the view based annotations to work and how can I access the roles through code to show or hide components based on the user role?
@Configuration
@EnableWebSecurity
@Import(VaadinAwareSecurityContextHolderStrategyConfiguration.class)
public class SecurityConfiguration {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http, RouteUtil routeUtil, RequestUtil requestUtil)
throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers(routeUtil::isRouteAllowed).permitAll()
.requestMatchers(requestUtil::isFrameworkInternalRequest).permitAll()
.anyRequest().authenticated()
)
.oauth2Login(oauth -> oauth
.userInfoEndpoint(userInfo -> userInfo
.oidcUserService(oidcUserService()))
.defaultSuccessUrl("/"))
.logout(logout -> logout
.logoutSuccessUrl("/")
)
.csrf(csrf -> csrf.ignoringRequestMatchers(requestUtil::isFrameworkInternalRequest))
.headers(headers -> headers.frameOptions(frame -> frame.sameOrigin()));
return http.build();
}
@Bean
public OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
return userRequest -> {
OidcUserService delegate = new OidcUserService();
OidcUser oidcUser = delegate.loadUser(userRequest);
List<GrantedAuthority> mappedAuthorities = oidcUser.getAuthorities().stream()
.collect(Collectors.toList());
List<String> groupIds = oidcUser.getClaimAsStringList("groups");
if (groupIds != null) {
for (String groupId : groupIds) {
switch (groupId) {
case "11111111-1111-1111-1111-111111111111":
mappedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
break;
case "22222222-2222-2222-2222-222222222222":
mappedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
break;
}
}
}
return new DefaultOidcUser(mappedAuthorities, oidcUser.getIdToken(), oidcUser.getUserInfo());
};
}
}