Search code examples
javashiro

Date dependent login with shiro


Is it possible creating date dependent logins with apache shiro? This would mean that I would be able to specify that a specific user can authenticate into an application only between certain dates.


Solution

  • You can extend the realm you are using and override the method doGetAuthenticationInfo(AuthenticationToken token) if the date condition is not met:

    public class DateRealm extends JdbcRealm {
    
      @Override
      protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
          throws AuthenticationException {
        Date date = new Date();
        if ( /* Your dat condition here */ true) {
          return super.doGetAuthenticationInfo(token);
        } else {
          return null;
        }
      }
    }