【发布时间】:2014-06-26 14:05:32
【问题描述】:
我有一些 Spring Controller 方法,起初我使用 @Secured 表示法进行保护
@Secured("Role_Admin")
public void dummyMethod(HttpServletRequest request) ...
如果用户没有该角色(如预期的那样),它将很好地识别角色,或者拒绝。但是,我尝试切换到使用 @PreAuthorize 注释,现在一切都被拒绝了。
@PreAuthorize("hasRole('Role_Admin')")
public void dummyMethod(HttpServletRequest request) ...
如果我删除 @PreAuthorize 注释,然后做某事,说
request.isUserInRole("Role_Admin")
这将返回 true。如果我留在注释中,然后检查用户的角色 在拒绝访问处理程序中,我可以看到授予用户“Role_Admin”权限。
所以我不确定@PreAuthorize 背后的代码正在检查哪些角色/权限。有人知道我可以检查哪些春季安全课程吗?
编辑
我正在使用 Java 来配置所有内容。所以它看起来像这样:
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class SecurityConfigurator extends GlobalMethodSecurityConfiguration { ...
在我对下面答案的回复中,我从 @Secured 切换的原因是因为它没有启动我的 AccessDeniedHandler。如果我可以强迫它这样做,我会坚持下去。 @PreAuthorize 确实启动了处理程序。
EDIT2
正如 Andrei 所指出的,我尝试在我的配置中定义它。但这并没有什么不同。
@Bean
public SimpleMappingExceptionResolver resolver() {
SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
resolver.setExcludedExceptions(AccessDeniedException.class);
Properties mappings = new Properties();
mappings.put("org.springframework.web.servlet.PageNotFound", "p404");
mappings.put("org.springframework.dao.DataAccessException", "dataAccessFailure");
mappings.put("org.springframework.transaction.TransactionException", "dataAccessFailure");
resolver.setExceptionMappings(mappings);
return resolver;
}
【问题讨论】:
-
您能发布您的安全配置 xml 吗?
-
很可能,您已经看过this SO post,但我想我会分享它。
-
看到了。我确实尝试在我的配置中添加一些东西,但是 ExceptionHandler 仍然会获取异常而不是 AccessDeniedHandler。将配置添加到问题中。
标签: java spring spring-security