我想立即在我的Grails应用程序(我正在使用Spring Security插件)中传播用户更改(用户角色的更改)。
我发现了这个:
springSecurityService.reauthenticate(userName)但这适用于当前登录的用户,而不适用于更改的用户!
对此有什么简单的解决方案(即使是强制注销已更改的用户也能满足我的要求)。
这种情况的用例是管理员更改了某个其他用户角色。如果更改的用户已登录,则在Spring Security的上下文中不会立即看到角色更改。
发布于 2012-06-29 06:16:59
我认为你必须声明一个spring安全SessionRegistry。看看这里的concurrent-sessions和这里的list-authenticated-principals。
然后,您可以列出和访问经过身份验证的用户并对其进行修改。
发布于 2012-06-29 21:49:17
多亏了Fabiano,我提供了以下有效的解决方案:
resources.groovy
import org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy
import org.springframework.security.web.session.ConcurrentSessionFilter
import org.springframework.security.core.session.SessionRegistryImpl
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy
// Place your Spring DSL code here
beans = {
// bind session registry
sessionRegistry(SessionRegistryImpl)
sessionAuthenticationStrategy(ConcurrentSessionControlStrategy, sessionRegistry) {
maximumSessions = -1
}
concurrentSessionFilter(ConcurrentSessionFilter){
sessionRegistry = sessionRegistry
expiredUrl = '/login/concurrentSession'
}
}MyService.groovy
def sessionRegistry
def expireSession(User user) {
def userSessions = sessionRegistry.getAllSessions(user, false)
// expire all registered sessions
userSessions.each {
log.debug "Expire session [$it] of the user [$user]"
it.expireNow()
}
}非常简单:-)
更新:
此外,不要忘记注册HttpSessionEventPublisher,并根据Config.groovy使用不同的方式将concurrentSessionFilter添加到Filter Documentations。
web.xml
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>发布于 2013-03-29 15:55:07
我花了很长时间在网上浏览同样的问题。提出的解决方案不起作用(也许我没那么幸运:)。所以这是我的方法
首先,我们创建一个Grails服务,如下所示:
class SecurityHelperService {
final Set<String> userUpdates = new HashSet<String>()
public void markUserUpdate(String username) {
synchronized (userUpdates) {
userUpdates.add(username)
}
}
public boolean clearUserUpdate(String username) {
synchronized (userUpdates) {
return userUpdates.remove(username) != null
}
}
public boolean checkUserUpdate() {
def principal = springSecurityService.principal
if (principal instanceof org.springframework.security.core.userdetails.User) {
synchronized (userUpdates) {
if (!userUpdates.remove(principal.username)) {
return true
}
}
springSecurityService.reauthenticate(principal.username)
return false
}
return true
}
}在grails-app/conf目录中,我们创建一个Grails过滤器来检查当前用户权限是否已更改,例如
class MyFilters {
SecurityHelperService securityHelper
def filters = {
userUpdateCheck(controller: '*', action: '*') {
before = {
if (!securityHelper.checkUserUpdate()) {
redirect url: '/'
return false
}
return true
}
}
}
}就这样。每次在代码中更新用户权限时,我们都会调用服务方法
securityHelper.markUserUpdate('username')当在线用户下一次访问页面时,会自动检查并重新加载其权限。不需要手动注销。
我们还可以在新登录时清除以前的用户更新,以避免过滤器中不必要的重定向
希望这能有所帮助
https://stackoverflow.com/questions/11247495
复制相似问题