Created
April 15, 2011 15:57
-
-
Save maggandalf/921931 to your computer and use it in GitHub Desktop.
SpringSecurity Voter granting only access when it is night.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NightVoter implements AccessDecisionVoter { | |
private String rolePrefix = "ROLE_"; | |
private static final int SUNRISE_HOUR = 9; | |
private static final int SUNSET_HOUR = 21; | |
public String getRolePrefix() { | |
return rolePrefix; | |
} | |
/** | |
* Allows the default role prefix of <code>ROLE_</code> to be overridden. | |
* May be set to an empty value, although this is usually not desirable. | |
* | |
* @param rolePrefix | |
* the new prefix | |
*/ | |
public void setRolePrefix(String rolePrefix) { | |
this.rolePrefix = rolePrefix; | |
} | |
public boolean supports(ConfigAttribute attribute) { | |
if ((attribute.getAttribute() != null) | |
&& attribute.getAttribute().startsWith(getRolePrefix())) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public boolean supports(Class<?> arg0) { | |
return true; | |
} | |
public int vote(Authentication authentication, Object object, | |
Collection<ConfigAttribute> attributes) { | |
Calendar calendar = Calendar.getInstance(); | |
int currentHour = calendar.get(Calendar.HOUR_OF_DAY); | |
if(!isDaylight(currentHour)) { | |
return ACCESS_GRANTED; | |
} | |
return ACCESS_DENIED; | |
} | |
private boolean isDaylight(int currentHour) { | |
return currentHour>=SUNRISE_HOUR && currentHour<=SUNSET_HOUR; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment