Spring 安全性在未登录的情况下泄露 url
Spring Security revealing urls without logging in
在我的 Vaadin 应用程序中,我想使用 spring 安全性来保护一些页面。登录和注销功能都可以正常工作,但我想要的是,当 vaadin 应用程序启动时,他应该仍然能够访问“注册视图 class”。为此,我使用以下安全配置 class.
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private static final String LOGIN_PROCESSING_URL = "/login";
private static final String LOGIN_FAILURE_URL = "/login?error";
private static final String LOGOUT_SUCCESS_URL = "/login";
/**
* Require login to access internal pages and configure login form.
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
// Not using Spring CSRF here to be able to use plain HTML for the login page
http.csrf().disable()
// Register our CustomRequestCache, that saves unauthorized access attempts, so
// the user is redirected after login.
.requestCache().requestCache(new CustomRequestCache())
// Restrict access to our application.
.and().authorizeRequests()
.antMatchers("/", "/VAADIN/**", "/HEARTBEAT/**", "/UIDL/**", "/resources/**"
, "/registration", "registration", "/login/**", "/manifest.json", "/icons/**", "/images/**",
// (development mode) static resources
"/frontend/**",
// (production mode) static resources
"/frontend-es5/**", "/frontend-es6/**").anonymous()
// Allow all flow internal requests.
.requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()
// Allow all requests by logged in users.
.anyRequest().authenticated()
// Configure the login page.
.and().formLogin().loginPage(LOGIN_PROCESSING_URL).permitAll().loginProcessingUrl(LOGIN_PROCESSING_URL)
.failureUrl(LOGIN_FAILURE_URL)
// Configure logout
.and().logout().logoutSuccessUrl(LOGOUT_SUCCESS_URL);
}
我的 RegistrationView class 如下所示
@Route(value = "registration", layout = MainView.class)
@RouteAlias(value = "registration", layout = MainView.class)
@CssImport("./styles/views/login/registration.css")
public class Registration extends Div {
private static final long serialVersionUID = -1223086666624645746L;
private TextField username;
private TextField email;
private PasswordField password;
private PasswordField password2;
private Button userRegistrationSubmitButton;
在当前模式下,无论我做什么,只要我启动我的应用程序,我就会被重定向到登录页面。我该如何解决这个问题?提前致谢..
这不完全是重复的,但它的答案与 相同。
其核心是您不能使用 Spring 安全性使用的基于路径的检查来保护作为 single-page 应用程序加载的 Vaadin 视图。
对于那些将来会来这里的人。别担心,我支持你,兄弟。我找到了解决办法(笑)。基本上,无论谁在处理这个 vaadin 和 spring 安全应用程序,都会知道除了这些安全配置和自定义请求缓存 class 之外,您还将拥有 ConfigureUIServerInitListener class,基于在您遵循的教程上。有一个小技巧,我们可以使用身份验证检查来限制访问。所以如果你还没有,请添加以下内容class。
@Component
public class ConfigureUIServiceInitListener implements VaadinServiceInitListener {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void serviceInit(ServiceInitEvent event) {
event.getSource().addUIInitListener(uiEvent -> {
final UI ui = uiEvent.getUI();
ui.addBeforeEnterListener(this::beforeEnter);
});
}
/**
* Reroutes the user if (s)he is not authorized to access the view.
*
* @param event
* before navigation event with event details
*/
private void beforeEnter(BeforeEnterEvent event) {
if (AboutView.class.equals(event.getNavigationTarget())
&& !SecurityUtils.isUserLoggedIn()) {
event.rerouteTo(Login.class);
}
}
}
从之前的Enter方法可以看出,我希望AboutView class受到spring安全保护。然后我进行相应的检查。基于此,您可以添加其他视图classes。这样它会阻止您在未登录的情况下访问某些视图。
在我的 Vaadin 应用程序中,我想使用 spring 安全性来保护一些页面。登录和注销功能都可以正常工作,但我想要的是,当 vaadin 应用程序启动时,他应该仍然能够访问“注册视图 class”。为此,我使用以下安全配置 class.
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private static final String LOGIN_PROCESSING_URL = "/login";
private static final String LOGIN_FAILURE_URL = "/login?error";
private static final String LOGOUT_SUCCESS_URL = "/login";
/**
* Require login to access internal pages and configure login form.
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
// Not using Spring CSRF here to be able to use plain HTML for the login page
http.csrf().disable()
// Register our CustomRequestCache, that saves unauthorized access attempts, so
// the user is redirected after login.
.requestCache().requestCache(new CustomRequestCache())
// Restrict access to our application.
.and().authorizeRequests()
.antMatchers("/", "/VAADIN/**", "/HEARTBEAT/**", "/UIDL/**", "/resources/**"
, "/registration", "registration", "/login/**", "/manifest.json", "/icons/**", "/images/**",
// (development mode) static resources
"/frontend/**",
// (production mode) static resources
"/frontend-es5/**", "/frontend-es6/**").anonymous()
// Allow all flow internal requests.
.requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()
// Allow all requests by logged in users.
.anyRequest().authenticated()
// Configure the login page.
.and().formLogin().loginPage(LOGIN_PROCESSING_URL).permitAll().loginProcessingUrl(LOGIN_PROCESSING_URL)
.failureUrl(LOGIN_FAILURE_URL)
// Configure logout
.and().logout().logoutSuccessUrl(LOGOUT_SUCCESS_URL);
}
我的 RegistrationView class 如下所示
@Route(value = "registration", layout = MainView.class)
@RouteAlias(value = "registration", layout = MainView.class)
@CssImport("./styles/views/login/registration.css")
public class Registration extends Div {
private static final long serialVersionUID = -1223086666624645746L;
private TextField username;
private TextField email;
private PasswordField password;
private PasswordField password2;
private Button userRegistrationSubmitButton;
在当前模式下,无论我做什么,只要我启动我的应用程序,我就会被重定向到登录页面。我该如何解决这个问题?提前致谢..
这不完全是重复的,但它的答案与
其核心是您不能使用 Spring 安全性使用的基于路径的检查来保护作为 single-page 应用程序加载的 Vaadin 视图。
对于那些将来会来这里的人。别担心,我支持你,兄弟。我找到了解决办法(笑)。基本上,无论谁在处理这个 vaadin 和 spring 安全应用程序,都会知道除了这些安全配置和自定义请求缓存 class 之外,您还将拥有 ConfigureUIServerInitListener class,基于在您遵循的教程上。有一个小技巧,我们可以使用身份验证检查来限制访问。所以如果你还没有,请添加以下内容class。
@Component
public class ConfigureUIServiceInitListener implements VaadinServiceInitListener {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void serviceInit(ServiceInitEvent event) {
event.getSource().addUIInitListener(uiEvent -> {
final UI ui = uiEvent.getUI();
ui.addBeforeEnterListener(this::beforeEnter);
});
}
/**
* Reroutes the user if (s)he is not authorized to access the view.
*
* @param event
* before navigation event with event details
*/
private void beforeEnter(BeforeEnterEvent event) {
if (AboutView.class.equals(event.getNavigationTarget())
&& !SecurityUtils.isUserLoggedIn()) {
event.rerouteTo(Login.class);
}
}
}
从之前的Enter方法可以看出,我希望AboutView class受到spring安全保护。然后我进行相应的检查。基于此,您可以添加其他视图classes。这样它会阻止您在未登录的情况下访问某些视图。