如何在GrailsSpringSecurity中为RememberMecookie设置自定义超时?
我在如何在GrailsSpringSecurity中为RememberMecookie设置自定义超时?,grails,spring-security,remember-me,Grails,Spring Security,Remember Me,我在Config.groovy中设置了tokenValiditySeconds作为 grails.plugins.springsecurity.rememberMe.tokenValiditySeconds=31*24*60*60 但是,我想为来自(比如子域)的所有请求设置不同的有效性。我可以从请求对象中识别域信息,但无法从CustomMemberService类重写令牌有效性秒 默认情况下,代币的有效期为14天,从最后一个起 验证尝试成功。这可以使用 AbstractMemberServic
Config.groovy
中设置了tokenValiditySeconds
作为
grails.plugins.springsecurity.rememberMe.tokenValiditySeconds=31*24*60*60
但是,我想为来自(比如子域)的所有请求设置不同的有效性。我可以从请求
对象中识别域信息,但无法从CustomMemberService
类重写令牌有效性秒
默认情况下,代币的有效期为14天,从最后一个起
验证尝试成功。这可以使用
AbstractMemberServices.setTokenValiditySeconds(int)。如果该值
如果小于零,则过期时间将保持在14天,但
cookie的maxAge属性将使用负值,
这意味着当浏览器关闭时,它将不会被存储
根据文档,我应该能够使用setTokenValiditySeconds(int)
方法更改有效性,但它没有任何效果
那么,如何覆盖配置文件中设置的值呢
谢谢
编辑:
CustomRemeberMEService.groovy
// ..
class CustomRememberMeServicesFilter extends RememberMeAuthenticationFilter {
def customRememberMeService;
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
if (SecurityContextHolder.getContext().getAuthentication() == null) {
LoggedInUserDetails rememberMeAuth = customRememberMeService.customAutoLogin(request, response);
}
chain.doFilter(request, response);
}
}
覆盖方法
calculateLoginLifetime
,默认情况下,该方法将返回配置中设置的值(它调用getTokenValiditySeconds()
。通过覆盖该方法,您可以确定(基于请求)应通过正常超时还是自定义超时
protected int calculateLoginLifetime(HttpServletRequest request, Authentication authentication) {
if (request.getRemoteAddr().startsWith("subdomain") {
return 15; // Or whatever you want, you could also make it configurable.
}
return getTokenValiditySeconds();
}
你能展示一下你的CustomMembermyService以及你是如何注册它的吗(例如你的conf/spring/resources.groovy)user2264997:好的。我会更新它。目前我远离代码。更新问题以包含当前代码。
setTokenValiditySeconds
是一个配置选项,在创建cookie后在代码中调用它基本上不会做任何事情(至少对于已经存在的cookie不会).M.Deinum:那么,我怎样才能自定义cookie设置部分呢?我认为memberme cookie是从AbstractMemberService.groovy
类()中设置的。感谢您的回复。我将进行调查,但这将花费大量时间。之后将进行响应。这不起作用。它仍然使用配置文件中指定的值。请确保您仅覆盖calculateLoginLifetime
,并且您的实例也是正在使用的实际实例。
// ..
class CustomRememberMeServicesFilter extends RememberMeAuthenticationFilter {
def customRememberMeService;
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
if (SecurityContextHolder.getContext().getAuthentication() == null) {
LoggedInUserDetails rememberMeAuth = customRememberMeService.customAutoLogin(request, response);
}
chain.doFilter(request, response);
}
}
protected int calculateLoginLifetime(HttpServletRequest request, Authentication authentication) {
if (request.getRemoteAddr().startsWith("subdomain") {
return 15; // Or whatever you want, you could also make it configurable.
}
return getTokenValiditySeconds();
}