Ra在Grails应用程序中验证已更改的用户
我想立即在我的Grails应用程序中传播用户更改(用户角色的更改)(我使用的是Spring安全插件) 我发现:Ra在Grails应用程序中验证已更改的用户,grails,spring-security,Grails,Spring Security,我想立即在我的Grails应用程序中传播用户更改(用户角色的更改)(我使用的是Spring安全插件) 我发现: springSecurityService.reauthenticate(userName) 但这适用于当前登录的用户,而不是更改用户 是否有任何简单的解决方案(即使强制注销已更改的用户也足够了) 这种情况的用例是管理员更改其他用户角色时。如果更改的用户已登录,则不会立即在Spring Security的上下文中看到角色更改。我认为您必须声明Spring Security Sessi
springSecurityService.reauthenticate(userName)
但这适用于当前登录的用户,而不是更改用户
是否有任何简单的解决方案(即使强制注销已更改的用户也足够了)
这种情况的用例是管理员更改其他用户角色时。如果更改的用户已登录,则不会立即在Spring Security的上下文中看到角色更改。我认为您必须声明Spring Security SessionRegistry。看看这里和这里
然后,您可以列出和访问经过身份验证的用户并对其进行修改。多亏了Fabiano,我提供了以下有效的解决方案: 资源.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,并根据需要使用不同的方式将concurrentSessionFilter添加到Config.groovy
web.xml
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
org.springframework.security.web.session.HttpSessionEventPublisher
花了很长时间浏览互联网上的同一个问题。提议的解决方案不起作用(也许我没那么幸运:)。这就是我的方法
首先,我们创建一个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
}
}
就这些。每次在代码中更新用户权限时,我们称之为服务方法
securityHelper.markUserUpdate('username')
当在线用户下次访问页面时,会自动检查并重新加载其权限。不需要手动注销
我们可以选择在新登录时清除以前的用户更新,以避免过滤器中不必要的重定向
希望这有帮助
securityHelper.markUserUpdate('username')