上面两个篇幅解决了注解形式的自定义前缀,那么再http这边配置的能通用吗?
通过之前的调试结果,很明显应该是不行的。权限自动拼接了 ROLE_前缀。
http.authorizeRequests()
.antMatchers("/data/**").hasRole("data:data")
.anyRequest()
.authenticated();
authorities.add(new SimpleGrantedAuthority("data:data"));
那么这种形式的权限前缀又怎么自定义呢?
private static String hasRole(String role) {
Assert.notNull(role, "role cannot be null");
if (role.startsWith("ROLE_")) {
throw new IllegalArgumentException(
"role should not start with 'ROLE_' since it is automatically inserted. Got '"
+ role + "'");
}
return "hasRole('ROLE_" + role + "')";
}
hasRole点进去可以发现也就是如果你固定使用.hasRole的形式就是默认又这个前缀,这个貌似无法更改,那么就可以用
.hasAuthority("data:data")来代替。