|
1 | 1 | package com.vaadin.flow.spring.flowsecurity;
|
2 | 2 |
|
| 3 | +import com.vaadin.flow.component.UI; |
| 4 | +import com.vaadin.flow.internal.UrlUtil; |
| 5 | +import com.vaadin.flow.server.HandlerHelper; |
| 6 | +import com.vaadin.flow.spring.RootMappedCondition; |
| 7 | +import com.vaadin.flow.spring.VaadinConfigurationProperties; |
| 8 | +import com.vaadin.flow.spring.flowsecurity.data.UserInfo; |
| 9 | +import com.vaadin.flow.spring.flowsecurity.service.UserInfoService; |
| 10 | +import com.vaadin.flow.spring.flowsecurity.views.LoginView; |
| 11 | +import com.vaadin.flow.spring.security.AuthenticationContext; |
| 12 | +import com.vaadin.flow.spring.security.NavigationAccessControlConfigurer; |
| 13 | +import com.vaadin.flow.spring.security.RequestUtil; |
| 14 | +import com.vaadin.flow.spring.security.SpringAccessPathChecker; |
| 15 | +import com.vaadin.flow.spring.security.UidlRedirectStrategy; |
3 | 16 | import jakarta.servlet.ServletContext;
|
4 |
| - |
5 |
| -import java.util.stream.Collectors; |
6 |
| - |
7 | 17 | import org.springframework.beans.factory.annotation.Autowired;
|
8 | 18 | import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
|
9 | 19 | import org.springframework.context.annotation.Bean;
|
10 | 20 | import org.springframework.context.annotation.Configuration;
|
| 21 | +import org.springframework.context.annotation.Primary; |
11 | 22 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
12 | 23 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
13 | 24 | import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
|
16 | 27 | import org.springframework.security.core.userdetails.UserDetails;
|
17 | 28 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
18 | 29 | import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
| 30 | +import org.springframework.security.web.DefaultSecurityFilterChain; |
19 | 31 | import org.springframework.security.web.SecurityFilterChain;
|
20 |
| -import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 32 | +import org.springframework.security.web.access.AuthorizationManagerWebInvocationPrivilegeEvaluator.HttpServletRequestTransformer; |
| 33 | +import org.springframework.security.web.access.PathPatternRequestTransformer; |
| 34 | +import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler; |
21 | 35 |
|
22 |
| -import com.vaadin.flow.component.UI; |
23 |
| -import com.vaadin.flow.internal.UrlUtil; |
24 |
| -import com.vaadin.flow.spring.RootMappedCondition; |
25 |
| -import com.vaadin.flow.spring.VaadinConfigurationProperties; |
26 |
| -import com.vaadin.flow.spring.flowsecurity.data.UserInfo; |
27 |
| -import com.vaadin.flow.spring.flowsecurity.service.UserInfoService; |
28 |
| -import com.vaadin.flow.spring.flowsecurity.views.LoginView; |
29 |
| -import com.vaadin.flow.spring.security.AuthenticationContext; |
30 |
| -import com.vaadin.flow.spring.security.NavigationAccessControlConfigurer; |
31 |
| -import com.vaadin.flow.spring.security.RequestUtil; |
| 36 | +import java.security.Principal; |
| 37 | +import java.util.stream.Collectors; |
32 | 38 |
|
33 | 39 | import static com.vaadin.flow.spring.flowsecurity.service.UserInfoService.ROLE_ADMIN;
|
34 | 40 | import static com.vaadin.flow.spring.security.RequestUtil.antMatchers;
|
35 |
| -import static com.vaadin.flow.spring.security.VaadinSecurityConfigurer.vaadin; |
36 | 41 |
|
37 | 42 | @EnableWebSecurity
|
38 | 43 | @Configuration
|
@@ -60,51 +65,88 @@ public AuthenticationContext authenticationContext() {
|
60 | 65 | @Bean
|
61 | 66 | static NavigationAccessControlConfigurer navigationAccessControlConfigurer() {
|
62 | 67 | return new NavigationAccessControlConfigurer()
|
63 |
| - .withRoutePathAccessChecker(); |
| 68 | + .withLoginView(LoginView.class).withRoutePathAccessChecker(); |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + @Primary |
| 73 | + static HttpServletRequestTransformer customRequestTransformer() { |
| 74 | + return SpringAccessPathChecker.principalAwareRequestTransformer( |
| 75 | + new PathPatternRequestTransformer()); |
64 | 76 | }
|
65 | 77 |
|
66 | 78 | @Bean
|
67 | 79 | public SecurityFilterChain webFilterChain(HttpSecurity http,
|
68 | 80 | AuthenticationContext authenticationContext) throws Exception {
|
69 | 81 | // Setup
|
70 | 82 | http.csrf(AbstractHttpConfigurer::disable); // simple for testing
|
71 |
| - // purpose |
| 83 | + // purpose |
72 | 84 |
|
73 | 85 | // Homemade security for Vaadin application, not fully functional as the
|
74 | 86 | // configuration provided by VaadinWebSecurity
|
75 | 87 | // @formatter:off
|
76 | 88 | http.authorizeHttpRequests(auth -> auth
|
| 89 | + // Ensures that SpringPathAccessChecker does not fail when matchers get Principal from HTTP request |
| 90 | + .requestMatchers(request -> { |
| 91 | + Principal principal = request.getUserPrincipal(); |
| 92 | + if (principal == null) { |
| 93 | + // Do nothing, just avoid IDE complain about not used variable |
| 94 | + } |
| 95 | + return false; // no need to match rule, we just want to access principal. |
| 96 | + }).denyAll() |
77 | 97 | // Permit access to static resources
|
78 | 98 | .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
|
79 |
| - .permitAll() |
| 99 | + .permitAll() |
| 100 | + // Permit access to vaadin's internal communication |
| 101 | + .requestMatchers(request -> HandlerHelper |
| 102 | + .isFrameworkInternalRequest("/*", request)) |
| 103 | + .permitAll() |
| 104 | + .requestMatchers(requestUtil::isAnonymousRoute) |
| 105 | + .permitAll() |
| 106 | + // Permit technical access to vaadin's static files |
| 107 | + .requestMatchers(antMatchers("/VAADIN/**")).permitAll() |
| 108 | + // custom request matchers. using 'routeAwareAntMatcher' to |
| 109 | + // allow checking route and alias paths against patterns |
80 | 110 | .requestMatchers(antMatchers("/admin-only/**", "/admin"))
|
81 |
| - .hasAnyRole(ROLE_ADMIN) |
| 111 | + .hasAnyRole(ROLE_ADMIN) |
82 | 112 | .requestMatchers(antMatchers("/private"))
|
83 |
| - .authenticated() |
| 113 | + .authenticated() |
84 | 114 | .requestMatchers(antMatchers("/", "/public/**", "/another"))
|
85 |
| - .permitAll() |
86 |
| - .requestMatchers(new AntPathRequestMatcher("/error")) |
87 |
| - .permitAll() |
| 115 | + .permitAll() |
| 116 | + |
| 117 | + .requestMatchers(antMatchers("/error")) |
| 118 | + .permitAll() |
88 | 119 | // routes aliases
|
89 | 120 | .requestMatchers(antMatchers("/alias-for-admin"))
|
90 |
| - .hasAnyRole(ROLE_ADMIN) |
| 121 | + .hasAnyRole(ROLE_ADMIN) |
91 | 122 | .requestMatchers(antMatchers("/home", "/hey/**"))
|
92 |
| - .permitAll() |
93 |
| - ); |
| 123 | + .permitAll() |
| 124 | + .requestMatchers(antMatchers("/all-logged-in/**", "/passthrough/**")) |
| 125 | + .authenticated() |
| 126 | + ); |
94 | 127 | // @formatter:on
|
95 |
| - http.with(vaadin(), |
96 |
| - cfg -> cfg.loginView(LoginView.class, getLogoutSuccessUrl()) |
97 |
| - .addLogoutHandler( |
98 |
| - (request, response, authentication) -> { |
99 |
| - UI ui = UI.getCurrent(); |
100 |
| - ui.accessSynchronously(() -> ui.getPage() |
101 |
| - .setLocation(UrlUtil |
102 |
| - .getServletPathRelative( |
103 |
| - getLogoutSuccessUrl(), |
104 |
| - request))); |
105 |
| - })); |
106 |
| - |
107 |
| - return http.build(); |
| 128 | + http.logout(cfg -> { |
| 129 | + SimpleUrlLogoutSuccessHandler logoutSuccessHandler = new SimpleUrlLogoutSuccessHandler(); |
| 130 | + logoutSuccessHandler.setDefaultTargetUrl(getLogoutSuccessUrl()); |
| 131 | + logoutSuccessHandler |
| 132 | + .setRedirectStrategy(new UidlRedirectStrategy()); |
| 133 | + cfg.logoutSuccessHandler(logoutSuccessHandler); |
| 134 | + cfg.addLogoutHandler((request, response, authentication) -> { |
| 135 | + UI ui = UI.getCurrent(); |
| 136 | + ui.accessSynchronously(() -> ui.getPage().setLocation( |
| 137 | + UrlUtil.getServletPathRelative(getLogoutSuccessUrl(), |
| 138 | + request))); |
| 139 | + }); |
| 140 | + }); |
| 141 | + // Custom login page with form authentication |
| 142 | + http.formLogin(cfg -> cfg.loginPage("/my/login/page").permitAll()); |
| 143 | + DefaultSecurityFilterChain filterChain = http.build(); |
| 144 | + // Test application uses AuthenticationContext, configure it with |
| 145 | + // the logout handlers |
| 146 | + AuthenticationContext.applySecurityConfiguration(http, |
| 147 | + authenticationContext); |
| 148 | + |
| 149 | + return filterChain; |
108 | 150 | }
|
109 | 151 |
|
110 | 152 | public String getLogoutSuccessUrl() {
|
|
0 commit comments