Created
January 12, 2012 12:46
-
-
Save sody/1600306 to your computer and use it in GitHub Desktop.
Secure pages with tapestry5. Rethinking
This file contains 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 SecuredPageActivator implements PageActivator { | |
private final PageActivator activator; | |
private final SecurityContext securityContext; | |
public SecuredPageActivator(final PageActivator activator, | |
final SecurityContext securityContext) { | |
this.activator = activator; | |
this.securityContext = securityContext; | |
} | |
public boolean activatePage(final ComponentResources pageResources, | |
final EventContext activationContext, | |
final ComponentEventResultProcessor resultProcessor) throws IOException { | |
// get annotation from page class if present | |
final Authority authority = pageResources.getPage().getClass().getAnnotation(Authority.class); | |
if (authority != null) { | |
final Authentication authentication = securityContext.getCurrentUser(); | |
// is anonymous | |
if (authentication == null) { | |
throw new AuthenticationException("Not authenticated"); | |
} | |
// check if current user has defined authority | |
if (!authentication.hasAuthority(authority.value())) { | |
throw new AccessDeniedException(String.format("Access denied. Need authority: '%s'", authority.value())); | |
} | |
} | |
return activator.activatePage(pageResources, activationContext, resultProcessor); | |
} | |
} |
This file contains 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
@Decorate(serviceInterface = PageActivator.class) | |
public PageActivator decoratePageActivator(final PageActivator activator, | |
final SecurityContext securityContext) { | |
return new SecuredPageActivator(activator, securityContext); | |
} |
This file contains 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 SecurityFilter implements ComponentRequestFilter { | |
private final SecurityContext securityContext; | |
private final ComponentSource componentSource; | |
public SecurityFilter(final SecurityContext securityContext, final ComponentSource componentSource) { | |
this.securityContext = securityContext; | |
this.componentSource = componentSource; | |
} | |
public void handleComponentEvent(final ComponentEventRequestParameters parameters, | |
final ComponentRequestHandler handler) throws IOException { | |
checkAccessToPage(parameters.getActivePageName()); | |
handler.handleComponentEvent(parameters); | |
} | |
public void handlePageRender(final PageRenderRequestParameters parameters, | |
final ComponentRequestHandler handler) throws IOException { | |
checkAccessToPage(parameters.getLogicalPageName()); | |
handler.handlePageRender(parameters); | |
} | |
private void checkAccessToPage(final String pageName) { | |
// get annotation from page class if present | |
final Authority authority = componentSource.getPage(pageName).getClass().getAnnotation(Authority.class); | |
if (authority != null) { | |
final Authentication authentication = securityContext.getCurrentUser(); | |
// is anonymous | |
if (authentication == null) { | |
throw new AuthenticationException("Not authenticated"); | |
} | |
// check if current user has defined authority | |
if (!authentication.hasAuthority(authority.value())) { | |
throw new AccessDeniedException(String.format("Access denied. Need authority: '%s'", authority.value())); | |
} | |
} | |
} | |
} |
This file contains 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
@Contribute(ComponentRequestHandler.class) | |
public static void contributeComponentRequestHandler(final OrderedConfiguration<ComponentRequestFilter> configuration) { | |
configuration.addInstance("SecurityFilter", SecurityFilter.class); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment