The previous post defined some required roles for an endpoint in Spring, and passed them through to a ShiroUTValidator class which checks that the authenticated subject has all of the defined roles:
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
<util:list id="requiredRolesList"> | |
<value>boss</value> | |
</util:list> | |
<bean class="org.apache.coheigea.cxf.shiro.authentication.ShiroUTValidator" id="utValidator"> | |
<constructor-arg value="src/test/resources/securityconfig.ini"/> | |
<property name="requiredRoles" ref="requiredRolesList"/> | |
</bean> |
An alternative is instead to use Shiro's annotation support. Here we can add annotations to the service endpoint implementation to require that the authenticated user has the correct role (@RequiresRoles) or permissions (@RequiresPermissions). Note that these annotations are specific to Shiro, support is not yet added to support the standard javax.annotation.security annotations (see here).
So to change our test-case to use annotations, instead of defining the roles in Spring, we instead define the following annotation in the service implementation:
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
@RequiresRoles("boss") | |
public int doubleIt(int numberToDouble) { | |
... | |
} |
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
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> | |
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" | |
depends-on="lifecycleBeanPostProcessor"/> | |
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor" /> |