访问我设置一个spring-boot-1.5.3
应用与spring-security-4.2.2
和基于当局配置限制的路径。这些限制类似于此:检查URL是通过身份验证的用户
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/accounts/**", "/roles/**")
.hasAuthority(ADMINISTRATOR)
.and()
.antMatchers("/**")
.hasAuthority(USER)
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.csrf()
.disable();
}
}
总之 - 我希望用户有权力USER
到能够访问我的整个Web应用程序,除了/accounts/**
和/roles/**
路径。此配置工作正常,如果我尝试访问这些页面而没有相应的权限,则会收到预期的错误(403)。
对于具有USER
权限的用户,我想隐藏一些URL,以便它们不发出这些请求并获得403
错误。 我如何检查是否允许用户访问的URL?
到目前为止,我已经尝试注入WebInvocationPrivilegeEvaluator
并调用它的isAllowed
方法,虽然这似乎并不奏效。例如:
// Helper class that returns authentication instance.
Authentication auth = AuthUtils.getAuthentication();
// Don't have permission to access /users, expect to get false result.
boolean res = webInvocationPrivilegeEvaluator.isAllowed("/users", auth);
assert res == false; // fails
可能的好主意,但不适用于@PreAuthorize。我所得到的FilterSecurityInterceptor确实只返回securityMetaDataSource和“authenticated”过滤器,而有问题的URL有@PreAuthorize(“hasRole('ADMIN')”)注释。看起来我必须得到不同的过滤器,才能提供PreAuthorize中列出的所有条件 – tequilacat