当前位置: 首页 > 知识库问答 >
问题:

Spring Security授权检查不起作用

马朝斑
2023-03-14

我有以下几点。

@EnableWebSecurity
@Configuration
public class RetailerSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                .csrf().ignoringAntMatchers("/login/**")
                .and()
                .authorizeRequests()
                .antMatchers("/login", "/login/reset", "/resources/**", "/balance/getResult/**").permitAll()
                .anyRequest().fullyAuthenticated()
                .antMatchers("/retailers/**", "/balance/**").access("hasRole('ROLE_ADMIN')")
                .antMatchers("/tellers/**", "/tellers").hasAnyAuthority("PRIMARY")
                .antMatchers("/sell/**", "/sell").hasAnyAuthority("TELLER")
                .and()
                .formLogin()
                .loginPage("/login")
                .successForwardUrl("/login/success")
                .permitAll()
                .and()
                .logout()
                .permitAll();

    }
}

我还尝试了一系列其他的排列,似乎都不起作用。我已经检查了这部分代码是否被执行,它执行并没有错误。

编辑

忘了提一下,所有的url都不能被经过身份验证的用户访问。但是任何经过身份验证的用户都可以访问所有URL。例如,以PRIMARY登录,我可以点击/sell/retailers/create,这是不应该发生的。

我没有东西可以尝试了。Emm...我已经用完了。我应该检查什么?

共有1个答案

刘博雅
2023-03-14

很奇怪,但是在权限设置修复它之前移动anyRequest(). fullyAuthentiated()。最后看起来是这样的

@EnableWebSecurity
@Configuration
public class RetailerSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                .csrf().ignoringAntMatchers("/login/**")
                .and()
                .authorizeRequests()
                .antMatchers("/login", "/login/reset", "/resources/**", "/balance/getResult/**").permitAll()
                .antMatchers("/retailers/**", "/balance/**").access("hasRole('ROLE_ADMIN')")
                .antMatchers("/tellers/**", "/tellers").hasAnyAuthority("PRIMARY")
                .antMatchers("/sell/**", "/sell").hasAnyAuthority("TELLER")
                .anyRequest().fullyAuthenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .successForwardUrl("/login/success")
                .permitAll()
                .and()
                .logout()
                .permitAll();

    }
}
 类似资料:
  • EDIT做了一些更多的测试:当我只配置secure-annotations=“enabled”时,基于角色的安全性工作。此外,配置pre-postannotations=“enabled”时,既不安全也不预授权。当我只配置前-后注释时,它仍然不起作用。 编辑2 更多的测试:只有secured_annotations=“enabled”,对channelservice的调用通过Cglib2AopPr

  • 我正试图改变从默认状态到滑入状态的转变。我想保持我的minSdkVersion为3,但是直到API级别5(Android 2.0)才添加overridePendingTransition。 我尝试创建一个整数来表示SDK版本,然后仅在Android 1.6或更低版本中通过一个简单的if语句重写转换。 Eclipse给了我一个LINT警告,声明“调用需要API级别5(当前最小值为3):android

  • 问题内容: 正在尝试在方法级别定义访问规则,但是它从未如此有效。 安全配置 ExampleController 每当我尝试使用user:user它访问/ v2 / home时,它执行得很好,是否由于“用户”没有访问权限而给我“拒绝访问”错误ROLE_ADMIN? 我实际上是在考虑在方法级别放弃访问规则并坚持使用http()ant规则,但是我必须知道为什么它对我不起作用。 问题答案: 在控制器上使用

  • 我正在使用带Spring security的Thymeleaf模板引擎。为了使用sec:authorize功能,我还使用了Thymeleaf-Spring Security集成模块,但由于某些原因,它不起作用。我没有收到任何错误,但是html div块中的所有代码都会被执行,无论用户扮演哪个角色。 例如,当我以员工身份登录时,我也会看到“去领导”和“去系统”按钮,但我不想让员工看到这些按钮。 这是

  • 我正在使用 Artifactory 2.4.0 和 Jenkins 1.438,我有一个包含多个模块的 maven 项目。需要将所有模块(jars和一场由此产生的战争)部署到Jenkins的远程Artifactory服务器中。 我的artifactory用户管理员使用默认密码(password),我尝试在jenkins上执行的所有构建都可以正常工作。因此,当我决定更改de Artifactory管

  • 我正在尝试使用ASP.NETCore 2.0创建一个Web API服务器,它使用azure ad v2endpoint令牌授权。我也有一个Angular 2应用程序,在那里会发生office365登录。我从那里获得令牌,然后向Web API服务器中的授权操作发送一个简单的请求。然而,我的令牌没有通过授权检查,我得到了401未授权响应。提供的描述是: 承载错误=“invalid_token”,err