【发布时间】:2016-08-04 12:53:53
【问题描述】:
在 Spring boot / spring security / Angular JS 应用程序中..
我试图仅在用户具有指定角色时才允许以下页面。
.antMatchers("/1_reportingEntities/*.html").hasAnyRole(roles)
但即使没有为用户设置该角色,它也允许该页面。 我错过了什么?
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();
}
【问题讨论】:
-
我认为 spring 会自动为角色添加 ROLE_,尝试使用数组 {"EDITOR", "AUTHORISER"}
-
无论你是否在它前面加上 ROLE 都没关系,Spring 可以检测到。
-
试试把 antMatchers.hasAnyRole 放在 anyRequest.authenticated 之前,看看是否有效?
标签: java angularjs spring spring-security spring-boot