【发布时间】:2016-11-03 04:47:05
【问题描述】:
我正在尝试删除 Spring Security 中的“ROLE_”前缀。我尝试的第一件事是:
http.servletApi().rolePrefix("");
这不起作用,所以我尝试按照http://docs.spring.io/spring-security/site/migrate/current/3-to-4/html5/migrate-3-to-4-jc.html#m3to4-role-prefixing-disable 中的建议创建一个BeanPostProcessor。那也没用。
最后,我尝试创建自己的SecurityExpressionHandler:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.expressionHandler(webExpressionHandler())
.antMatchers("/restricted").fullyAuthenticated()
.antMatchers("/foo").hasRole("mycustomrolename")
.antMatchers("/**").permitAll();
}
private SecurityExpressionHandler<FilterInvocation> webExpressionHandler() {
DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler();
defaultWebSecurityExpressionHandler.setDefaultRolePrefix("");
return defaultWebSecurityExpressionHandler;
}
但是,这也不起作用。如果我使用“hasAuthority(roleName)”而不是hasRole,它会按预期工作。
是否可以从 Spring Security 的 hasRole 检查中删除 ROLE_ 前缀?
【问题讨论】:
-
奇怪
BeanPostProcessor对我有用(您确实将其声明为staticbean 方法并包含PriorityOrdered以便它运行得非常早?)对于表达式处理程序也是如此。我们还配置了一个DefaultMethodSecurityExpressionHandlerDefaultMethodSecurityExpressionHandler`,其前缀设置为null。 -
是的,我直接从文档中复制了
BeanPostProcessor的代码。我尝试将@Bean放在我的@ConfigurationSpring Security 类和我的@SpringBootApplication类中。我添加了一个System.out.println以确保它也在 Spring Security 之前进行配置。hasAuthority按预期工作,所以我想我会改用它。 -
我们在非 Spring Boot 应用程序中使用了它。会不会是这样的干扰,或者引导的安全性在某种程度上仍然是较早配置的?
标签: spring spring-security spring-java-config