나는 grails의 초보자입니다. 최근에는 관리자 권한으로 사용자를 변경하는 강제 작업이있었습니다. 그래서 몇 가지 연구가 끝나면 내 솔루션입니다. 나는 사용자 세션을 추적하고 있으며 일단 세션이 변경되면 간단히 활성 세션이 만료됩니다.
web.xml 파일에서
,이 청취자 컨트롤러에서 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
beans = {
// bind session registry
sessionRegistry(SessionRegistryImpl)
sessionAuthenticationStrategy(ConcurrentSessionControlStrategy,sessionRegistry){
maximumSessions = -1 }
concurrentSessionFilter(ConcurrentSessionFilter){
sessionRegistry = sessionRegistry
expiredUrl = '/login/auth?f=true'
}
}
에서
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
def expireSession(User user) {
log.info("Process to expire session begins")
def orginalUser = springSecurityService?.principal.username
log.info("session infos for all principals: ${sessionRegistry.getAllPrincipals()}")
sessionRegistry.getAllPrincipals()?.each { princ ->
def allSessions = sessionRegistry.getAllSessions(princ, true);
log.info("all sessions: ${allSessions}")
log.info("principal: $princ; email: ${user?.email}; username: ${princ?.username}")
if(princ?.username?.equals(user?.email)) { //killing sessions only for user ([email protected])
sessionRegistry.getAllSessions(princ, true)?.each { sess ->
log.info("session: ${sess}; expiring it")
if(sess.expireNow())
log.info("----session expired----")
springSecurityService?.reauthenticate(user?.email)
springSecurityService?.reauthenticate(orginalUser)
}
}
}
}
RequestFilters.groovy에서를 추가 할 경우 각 요청 우리에 세션이 유효하거나 만료되었는지 테스트하십시오.
class RequestFilters {
def springSecurityService
def sessionRegistry
def filters = {
all(controller:'*', action:'*') {
before = {
log.info(controllerName + '/' + actionName + " : " + params)
log.info("request ${request}; session: ${request?.session}")
def sessInfo = sessionRegistry.getSessionInformation(request?.session?.id)
log.info("sessionRegistry: ${sessionRegistry}")
log.info("Session Id: ${request?.session?.id}")
log.info("session info: ${sessInfo}; is expired: ${sessInfo?.expired}")
if(sessInfo?.expired==true)
response.sendRedirect(grailsApplication.config.grails.serverURL+"/j_spring_security_logout");
}
after = { Map model ->
}
afterView = { Exception e ->
}
}
}
일부 답변을 살펴보면 다음과 같이 안내됩니다. http://stackoverflow.com/questions/11247495/ra-authenticate-changed-user-in-grails-application –
@ JoshuaMoore 감사합니다,하지만 불행히도 도움이되지 않습니다 : (( –
왜 안돼? 세션 레지스트리를 설정하고, 지정된 사용자에 대한 세션을 찾고, 사용자를 무효화 (예 : 로그 아웃)하는 방법을 명확하게 보여줍니다. 누락 된 것이 무엇입니까? –