【问题标题】:User able to access Admin role page - Spring Security [duplicate]用户能够访问管理员角色页面 - Spring Security [重复]
【发布时间】:2019-09-15 09:58:23
【问题描述】:

我今天一直在使用 Spring Security 进行注册/登录,但仍在掌握它。

我的配置是:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/**").permitAll()
            .antMatchers("/login").permitAll()
            .antMatchers("/registration").permitAll()
            .antMatchers("/viewGamers").permitAll()
            .antMatchers("/gameLibrary").permitAll()
            .antMatchers("/admin/**").hasAuthority("ADMIN").anyRequest()
            .authenticated().and().csrf().disable().formLogin()
            .loginPage("/login").failureUrl("/login?error=true")
            .defaultSuccessUrl("/gamer/home")
            .usernameParameter("email")
            .passwordParameter("password")
            .and().logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/").and().exceptionHandling()
            .accessDeniedPage("/access-denied");

}

从这里我读到如果你去/admin/anything,你必须有一个ADMIN 分配给你的角色。对吗?

我在处理它的控制器中没有逻辑检查,当一个通用的非管理员角色用户能够访问 /admin/home 页面时,我有点惊讶。

我在数据库中添加了一个新角色,确保该用户具有与之关联的角色,但它仍然可以访问管理页面,是否正确?

我已经运行了一些 printlns 并且可以确认角色是正确的。我是否在配置中犯了错误,或者我是否需要 admin/home 控制器中if (user.role != "Admin") { send elsewhere } 的附加逻辑?

提前致谢。

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

    问题出在您的配置中,您应该从更受限制的安全配置开始到更少限制的配置。 所以将顺序更改为:

                .antMatchers("/login").permitAll()
                .antMatchers("/registration").permitAll()
                .antMatchers("/viewGamers").permitAll()
                .antMatchers("/gameLibrary").permitAll()
                .antMatchers("/admin/**").hasAuthority("ADMIN")
                .antMatchers("/**").permitAll() // this will allow everything that passes all above cnofigs 
    

    请注意,我删除了 .anyRequest().authenticated(),因为包含了 .antMatchers("/**").permitAll(),如果您希望对所有请求进行身份验证,可以替换它。

    【讨论】:

      猜你喜欢
      • 2013-12-08
      • 2016-05-20
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 2013-04-29
      • 1970-01-01
      • 2012-08-16
      相关资源
      最近更新 更多