Spring 尽管已授予权限,安全性仍拒绝访问“@Secured”方法
Spring Security denies access to `@Secured` method in spite of granted authorities
所以我有一个 Vaadin (8) 视图,我已经 @Secure
d Spring 安全:
@Secured(SUPERADMIN_ROLE)
@SpringView(name = AdminHomeView.NAME)
class AdminHomeView : DemoViewWithLabel(){
companion object {
const val NAME = "admin/home"
}
override val labelContent = "This is the protected admin section. You are authenticated and authorized."
}
其中 DemoViewWithLabel
只是一个非常简单的摘要 class 显示
VerticalLayout(Label(labelContent))
因此,如果我以具有 Superadmin
角色的身份登录,我可以很好地访问此视图。
但是,让我们做一个小改动并覆盖一个方法...
@Secured(SUPERADMIN_ROLE)
@SpringView(name = AdminHomeView.NAME)
class AdminHomeView : DemoViewWithLabel(){
companion object {
const val NAME = "admin/home"
}
override val labelContent = "This is the protected admin section. You are authenticated and authorized."
override fun enter(event: ViewChangeListener.ViewChangeEvent?) {
super.enter(event)
}
}
这让我得到了 AccessDeniedException
... 我不明白为什么。
所以我为 Spring 安全打开了调试日志,这就是它必须说的:
Secure object: ReflectiveMethodInvocation: public void ch.cypherk.myapp.ui.views.admin.AdminHomeView.enter(com.vaadin.navigator.ViewChangeListener$ViewChangeEvent);
target is of class [ch.cypherk.myapp.ui.views.admin.AdminHomeView];
Attributes: [Superadmin]
Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@a6801701:
Principal: ch.cypherk.myapp.model.auth.MyUserDetails@6e4c5c5b;
Credentials: [PROTECTED];
Authenticated: true; Details: null;
Granted Authorities: RIGHT_MANAGER, Superadmin
到目前为止,这似乎没问题。它需要 Superadmin
权限,并且它有一个经过身份验证的用户具有该 Superadmin
权限。
然而...
Voter: org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter@7ddc2dd1, returned: 0
Voter: org.springframework.security.access.vote.RoleVoter@75d3fbb7, returned: 0
Voter: org.springframework.security.access.vote.AuthenticatedVoter@391740fc, returned: 0
因此,
org.springframework.security.access.AccessDeniedException: Access is denied
at org.springframework.security.access.vote.AbstractAccessDecisionManager.checkAllowIfAllAbstainDecisions(AbstractAccessDecisionManager.java:70)
at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:89)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:233)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:65)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at ch.cypherk.myapp.ui.views.admin.AdminHomeView$$EnhancerBySpringCGLIB$$ae16a97c_4.enter(<generated>)
at com.vaadin.navigator.Navigator.performNavigateTo(Navigator.java:778)
at com.vaadin.navigator.Navigator.lambda$navigateToa874efd(Navigator.java:702)
at com.vaadin.navigator.ViewBeforeLeaveEvent.navigate(ViewBeforeLeaveEvent.java:54)
at com.vaadin.navigator.View.beforeLeave(View.java:79)
at com.vaadin.navigator.Navigator.runAfterLeaveConfirmation(Navigator.java:730)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:701)
at ...
为什么?!!
希望能帮助解决这个问题。
一言以蔽之
授予的权限没有显示 ROLE_
前缀,因此被 RoleVoter
忽略。
将 RoleVoter
替换为具有空前缀的自定义实现解决了该问题。
全文
现在剩下的问题是为什么我们可以访问视图。解释很简单,但需要更多上下文。
我们正在努力将 Thorntail 应用程序迁移到 Spring Boot。
我们正在使用 Vaadin 8(因为我们有遗留的 Vaadin 7 东西,我们还没有设法摆脱它,需要支持)。
然后呢?
Vaadin 是一个 single-page 框架,Vaadin 8 有这种讨厌的引用视图的方式,即它在根 url 上使用 #!
(例如 https://<host>:<port>/#!foo/bar/baz/...
).
在 #
之后没有任何内容被发送到服务器,这意味着我们无法区分对 /
的访问和对 /#!foo/bar/baz/...
的访问
因此,我们不能使用 Spring 安全来保护对视图的访问。
并且我们有一个 Vaadin 视图,我们需要允许未经身份验证的访问。
因此,我们被迫允许未经身份验证的访问 /
。 MainUI
(处理所有传入请求)将检查用户是否已通过身份验证,如果未通过身份验证,则重定向到登录页面。
对视图本身的访问由 Vaadin 保护,而不是 Spring。 SpringViewProvider
将找不到用户不应访问的 View
(因此,用户无法导航到那里)。
但是,一旦我们调用 那个视图的方法,Spring 安全性就会启动并执行访问检查。 这些 检查失败,因为授予的权限没有预期的 "ROLE_"-前缀 Spring。 (我原以为这只是一个惯例,但事实并非如此;RoleVoter
实际上会忽略不显示前缀的权限,因此您必须那样做,或者您必须提供自己的 RoleVoter
.)
解决方案相对简单...
@Configuration
@EnableGlobalMethodSecurity(
securedEnabled = true,
prePostEnabled = true,
proxyTargetClass = true
)
class GlobalSecurityConfiguration : GlobalMethodSecurityConfiguration(){
override fun accessDecisionManager(): AccessDecisionManager {
return (super.accessDecisionManager() as AffirmativeBased).apply {
decisionVoters.replaceAll {
when(it){
is RoleVoter -> MyRoleVoter()
else -> it
}
}
}
}
}
哪里
class MyRoleVoter : RoleVoter(){
init {
rolePrefix = ""
}
}
所以我有一个 Vaadin (8) 视图,我已经 @Secure
d Spring 安全:
@Secured(SUPERADMIN_ROLE)
@SpringView(name = AdminHomeView.NAME)
class AdminHomeView : DemoViewWithLabel(){
companion object {
const val NAME = "admin/home"
}
override val labelContent = "This is the protected admin section. You are authenticated and authorized."
}
其中 DemoViewWithLabel
只是一个非常简单的摘要 class 显示
VerticalLayout(Label(labelContent))
因此,如果我以具有 Superadmin
角色的身份登录,我可以很好地访问此视图。
但是,让我们做一个小改动并覆盖一个方法...
@Secured(SUPERADMIN_ROLE)
@SpringView(name = AdminHomeView.NAME)
class AdminHomeView : DemoViewWithLabel(){
companion object {
const val NAME = "admin/home"
}
override val labelContent = "This is the protected admin section. You are authenticated and authorized."
override fun enter(event: ViewChangeListener.ViewChangeEvent?) {
super.enter(event)
}
}
这让我得到了 AccessDeniedException
... 我不明白为什么。
所以我为 Spring 安全打开了调试日志,这就是它必须说的:
Secure object: ReflectiveMethodInvocation: public void ch.cypherk.myapp.ui.views.admin.AdminHomeView.enter(com.vaadin.navigator.ViewChangeListener$ViewChangeEvent);
target is of class [ch.cypherk.myapp.ui.views.admin.AdminHomeView];
Attributes: [Superadmin]
Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@a6801701:
Principal: ch.cypherk.myapp.model.auth.MyUserDetails@6e4c5c5b;
Credentials: [PROTECTED];
Authenticated: true; Details: null;
Granted Authorities: RIGHT_MANAGER, Superadmin
到目前为止,这似乎没问题。它需要 Superadmin
权限,并且它有一个经过身份验证的用户具有该 Superadmin
权限。
然而...
Voter: org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter@7ddc2dd1, returned: 0
Voter: org.springframework.security.access.vote.RoleVoter@75d3fbb7, returned: 0
Voter: org.springframework.security.access.vote.AuthenticatedVoter@391740fc, returned: 0
因此,
org.springframework.security.access.AccessDeniedException: Access is denied
at org.springframework.security.access.vote.AbstractAccessDecisionManager.checkAllowIfAllAbstainDecisions(AbstractAccessDecisionManager.java:70)
at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:89)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:233)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:65)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at ch.cypherk.myapp.ui.views.admin.AdminHomeView$$EnhancerBySpringCGLIB$$ae16a97c_4.enter(<generated>)
at com.vaadin.navigator.Navigator.performNavigateTo(Navigator.java:778)
at com.vaadin.navigator.Navigator.lambda$navigateToa874efd(Navigator.java:702)
at com.vaadin.navigator.ViewBeforeLeaveEvent.navigate(ViewBeforeLeaveEvent.java:54)
at com.vaadin.navigator.View.beforeLeave(View.java:79)
at com.vaadin.navigator.Navigator.runAfterLeaveConfirmation(Navigator.java:730)
at com.vaadin.navigator.Navigator.navigateTo(Navigator.java:701)
at ...
为什么?!!
希望能帮助解决这个问题。
一言以蔽之
授予的权限没有显示 ROLE_
前缀,因此被 RoleVoter
忽略。
将 RoleVoter
替换为具有空前缀的自定义实现解决了该问题。
全文
现在剩下的问题是为什么我们可以访问视图。解释很简单,但需要更多上下文。
我们正在努力将 Thorntail 应用程序迁移到 Spring Boot。
我们正在使用 Vaadin 8(因为我们有遗留的 Vaadin 7 东西,我们还没有设法摆脱它,需要支持)。
然后呢?
Vaadin 是一个 single-page 框架,Vaadin 8 有这种讨厌的引用视图的方式,即它在根 url 上使用 #!
(例如 https://<host>:<port>/#!foo/bar/baz/...
).
在 #
之后没有任何内容被发送到服务器,这意味着我们无法区分对 /
的访问和对 /#!foo/bar/baz/...
因此,我们不能使用 Spring 安全来保护对视图的访问。
并且我们有一个 Vaadin 视图,我们需要允许未经身份验证的访问。
因此,我们被迫允许未经身份验证的访问 /
。 MainUI
(处理所有传入请求)将检查用户是否已通过身份验证,如果未通过身份验证,则重定向到登录页面。
对视图本身的访问由 Vaadin 保护,而不是 Spring。 SpringViewProvider
将找不到用户不应访问的 View
(因此,用户无法导航到那里)。
但是,一旦我们调用 那个视图的方法,Spring 安全性就会启动并执行访问检查。 这些 检查失败,因为授予的权限没有预期的 "ROLE_"-前缀 Spring。 (我原以为这只是一个惯例,但事实并非如此;RoleVoter
实际上会忽略不显示前缀的权限,因此您必须那样做,或者您必须提供自己的 RoleVoter
.)
解决方案相对简单...
@Configuration
@EnableGlobalMethodSecurity(
securedEnabled = true,
prePostEnabled = true,
proxyTargetClass = true
)
class GlobalSecurityConfiguration : GlobalMethodSecurityConfiguration(){
override fun accessDecisionManager(): AccessDecisionManager {
return (super.accessDecisionManager() as AffirmativeBased).apply {
decisionVoters.replaceAll {
when(it){
is RoleVoter -> MyRoleVoter()
else -> it
}
}
}
}
}
哪里
class MyRoleVoter : RoleVoter(){
init {
rolePrefix = ""
}
}