【问题标题】:Problem with permitAll in Vaadin and WebSecurity - not workingVaadin 和 WebSecurity 中的 permitAll 问题 - 无法正常工作
【发布时间】:2019-12-24 12:47:39
【问题描述】:

@Route 在 Vaadin 中创建的视图很少,现在我想添加安全性和一些登录。在我的SecurityConfiguration 类中,我将antMatchers.permitAll() 设置为仅用于2 个视图,其余设置为角色ADMIN。但它并没有像我认为的那样工作。它需要登录才能访问每个视图,并且在登录后,无论用户具有什么角色,我都可以访问所有视图。

我希望本教程对我有所帮助,但是没有登录就无法访问视图。

Securing Your App With Spring Security

我的配置类:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    private UserService userService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    public SecurityConfiguration(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    private void configureAuth(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService);
        auth.inMemoryAuthentication()
                .withUser("user")
                .password(passwordEncoder().encode("user"))
                .roles("USER");
    }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http.httpBasic().and()
              .anonymous()
              .and()
              .authorizeRequests()
              .antMatchers("/", "/login").permitAll()
              .antMatchers("/recipe-manager", "/ingredient-manager").hasAnyRole("ADMIN")
              .and()
              .formLogin().loginPage("/login").permitAll()
              .and()
              .logout().logoutSuccessUrl("/")
              .and()
              .csrf().disable().cors().disable().headers().disable();
  }
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(
                "/VAADIN/**",
                "/favicon.ico",
                "/robots.txt",
                "/manifest.webmanifest",
                "/sw.js",
                "/offline-page.html",
                "/icons/**",
                "/images/**",
                "/frontend/**",
                "/webjars/**",
                "/h2-console/**",
                "/frontend-es5/**", "/frontend-es6/**");
    }
}

我的视图有如下注释:

@Route("recipe-manager")
public class RecipeManagerView extends VerticalLayout
@Route("")
public class RecipeBrowserView extends VerticalLayout 
@Route("login")
public class LoginView extends VerticalLayout 
@Route("ingredient-manager")
public class IngredientManagerView extends VerticalLayout 

我希望任何人都可以访问RecipeBrowserViewLoginView,但只有登录用户才能访问RecipeManagerViewIngredientMangerView

【问题讨论】:

  • 当您尝试访问 LoginView 时发生了什么?
  • 嗯,当我登录时,我被重定向到 LoginView,当我没有登录时,什么都没有 - 我在任何地方

标签: java spring-security vaadin


【解决方案1】:

据我了解antMatchers 只接受单个参数。你应该改变你的配置类:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    private UserService userService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    public SecurityConfiguration(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    private void configureAuth(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService);
        auth.inMemoryAuthentication()
                .withUser("user")
                .password(passwordEncoder().encode("user"))
                .roles("USER");
    }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http.httpBasic().and()
              .anonymous()
              .and()
              .authorizeRequests()
              .antMatchers("/").permitAll()
              .antMatchers("/login").permitAll()
              .antMatchers("/recipe-manager", "/ingredient-manager").hasAnyRole("ADMIN")
              .and()
              .formLogin().loginPage("/login").permitAll()
              .and()
              .logout().logoutSuccessUrl("/")
              .and()
              .csrf().disable().cors().disable().headers().disable();
  }
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(
                "/VAADIN/**",
                "/favicon.ico",
                "/robots.txt",
                "/manifest.webmanifest",
                "/sw.js",
                "/offline-page.html",
                "/icons/**",
                "/images/**",
                "/frontend/**",
                "/webjars/**",
                "/h2-console/**",
                "/frontend-es5/**", "/frontend-es6/**");
    }
}

【讨论】:

  • 我已经改变了,但事实并非如此。 andMatchers 采用 String 的 Varargs,因此可以有多种模式。
【解决方案2】:

您不能将 Spring Security 中的基于路径的匹配用于 Vaadin 路由。 Spring Security 基于请求路径进行匹配,而在 Vaadin 中从一个视图导航到另一个视图是作为内部请求中的元数据发送的,该内部请求始终进入相同的硬编码路径。

相反,您可以在 Vaadin 提供的拦截器中实现访问控制逻辑。您可以查看https://vaadin.com/tutorials/securing-your-app-with-spring-security 了解更多信息。

【讨论】:

  • 好的,谢谢您的建议。我再看一遍。现在知道解决方案应该在那里。但我真的不知道该怎么做。我应该在方法 beforeEnter(BeforeEnterEvent event) 中的 ConfigureUISeriveInitListener.class 中以某种方式做到这一点吗?
  • 看来我生活在未来。教程系列仍在进行中,有关路由器集成的部分尚未完成。您对使用从服务初始化侦听器注册的 UI 级别 beforeEnter 侦听器的想法是正确的。如果您从 vaadin.com/start 下载完整的堆栈(“面包店”)启动程序,您现在可以看到如何完成此操作的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多