【发布时间】:2017-01-01 16:17:40
【问题描述】:
我有一些自定义角色,例如:
<span sec:authentication="principal.authorities">[MENU_USER, BUTTON_ADD_USER,ROLE_USER, MENU_PRIVILEGE, BUTTON_EDIT_USER]</span>
<div sec:authorize="hasRole('MENU_USER')">
<span>This content is only shown to administrators.</span>
</div>
使用'ROLE_USER'时,“span”中的文字可以正常显示,但使用其他角色时,文字无法显示。然后我在我的自定义角色中添加了'ROLE_'前缀,它又变得正常了。
我尝试像这样删除“ROLE_”前缀约束:
@Bean
AccessDecisionManager accessDecisionManager() {
RoleVoter voter = new RoleVoter();
voter.setRolePrefix("");
List<AccessDecisionVoter<? extends Object>> voters= new ArrayList<>();
voters.add(new WebExpressionVoter());
voters.add(voter);
voters.add(new AuthenticatedVoter());
AffirmativeBased decisionManger = new AffirmativeBased(voters);
return decisionManger;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.accessDecisionManager(accessDecisionManager())
.antMatchers("/webjars/**", "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.loginProcessingUrl("/j_spring_security_check")
.usernameParameter("j_username")
.passwordParameter("j_password")
.defaultSuccessUrl("/home", true)
.failureUrl("/test")
.and()
//logout is
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout")
.permitAll();
}
它也不起作用。知道如何删除强制性的“ROLE_”前缀吗?
【问题讨论】:
标签: spring-mvc spring-security thymeleaf