基于 ROLES 的 Spring 安全授权不起作用

Jay*_*Jay 1 java spring spring-security angularjs spring-boot

在 Spring boot / spring security / Angular JS 应用程序中..

仅当用户具有指定角色时,我才尝试允许以下页面。

.antMatchers("/1_reportingEntities/*.html").hasAnyRole(roles)
Run Code Online (Sandbox Code Playgroud)

但是即使没有为用户设置该角色,它也允许页面。我错过了什么?

Java代码

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        String roles [] = new String[] {"ROLE_EDITOR", "ROLE_AUTHORISER" };

        http.httpBasic()
                .and()
                .authorizeRequests()

                // Permit these resources
                .antMatchers("/login", "/4_security/login.html", "/bower_components/**", "/0_common/*.html", "/1_reportingEntities/*.js",
                        "/2_dataCollections/*.js", "/3_calendar/*.js", "/4_security/*.js", "/")
                .permitAll()
                // All other requests needs authentication
                .anyRequest().authenticated()

                // Allow only if has certain roles  
                .antMatchers("/1_reportingEntities/*.html").hasAnyRole(roles)

                .and()
                // CSRF protection
                .csrf().csrfTokenRepository(csrfTokenRepository()).and().addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);

        // Enable https
        http.requiresChannel().anyRequest().requiresSecure();

    }
Run Code Online (Sandbox Code Playgroud)

小智 5

我想问题出在 Spring 上。您需要为 hasAnyAuthority() 更改 hasAnyRole()。