You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by Deepthi Jayaram <de...@Aspiresys.com> on 2012/02/03 11:54:03 UTC
Authorization
Hi,
I am trying to use @RequiresRoles and @RequiresPermission and I am getting the following exceptions respectively:
org.apache.shiro.authz.UnauthorizedException: Subject does not have role [ROLE_ADMIN]
org.apache.shiro.authz.UnauthorizedException: Subject does not have permission [ADD_MEMBER]
Authorization is working for the correct roles and permissions. But if a user does not have a specific role or permission, it throws an exception instead of handling it properly. How do I redirect the Unauthorized access situation to a page (403.jsp) in my project?
My shiro config file (root-context.xml) is as follows:
************************<code>****************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop">
<beans:bean id="propConf"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/spring/jdbc.properties" />
<beans:bean id="ds" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
<beans:property name="serverName" value="<IP addr>" />
<beans:property name="user" value="root" />
<beans:property name="password" value="root" />
<beans:property name="databaseName" value="db1" />
</beans:bean>
<!-- Security Manager -->
<beans:bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<beans:property name="sessionMode" value="native" />
<beans:property name="realm" ref="jdbcRealm" />
<beans:property name="sessionManager" ref="sessionManager"/>
<beans:property name="cacheManager" ref="cacheManager"/>
</beans:bean>
<beans:bean name="ehCacheManagerMBean" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="configLocation">
<beans:value>classpath:hibernate.cfg.xml</beans:value>
</beans:property>
<beans:property name="configurationClass">
<beans:value>org.hibernate.cfg.AnnotationConfiguration</beans:value>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">${jdbc.dialect}</beans:prop>
<!-- Second level caching -->
<beans:prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</beans:prop>
<beans:prop key="hibernate.cache.use_second_level_cache">true</beans:prop>
<beans:prop key="hibernate.cache.use_query_cache">true</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<!-- Caching -->
<beans:bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<beans:property name="cacheManager" ref="ehCacheManagerMBean" />
<beans:property name="cacheManagerConfigFile" value="/ehcache-terracotta.xml" />
</beans:bean>
<beans:bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
<beans:property name="activeSessionsCacheName" value="shiro-activeSessionsCache" />
</beans:bean>
<beans:bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<beans:property name="sessionDAO" ref="sessionDAO" />
</beans:bean>
<beans:bean id="sha1Matcher" class="org.apache.shiro.authc.credential.Sha1CredentialsMatcher" />
<!-- JDBC Realm Settings -->
<beans:bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<beans:property name="name" value="jdbcRealm" />
<beans:property name="credentialsMatcher" ref="sha1Matcher" />
<beans:property name="dataSource" ref="ds" />
<beans:property name="authenticationQuery"
value="SELECT password FROM users WHERE username=? and enabled=1" />
<beans:property name="userRolesQuery"
value="SELECT role_name FROM roles r, users u, users_roles ur WHERE u.id=ur.user_id AND r.id=ur.role_id AND u.username=?" />
<beans:property name="permissionsQuery"
value="SELECT permission FROM roles r, permissions p, roles_permissions rp WHERE r.id=rp.role_id AND p.id=rp.permission_id AND r.role_name=?" />
<beans:property name="permissionsLookupEnabled" value="true" />
</beans:bean>
<!-- Spring Integration -->
<beans:bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after
the lifecycleBeanProcessor has run: -->
<beans:bean id="annotationProxy"
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor" />
<beans:bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<beans:property name="securityManager" ref="securityManager" />
</beans:bean>
<!-- Secure Spring remoting: Ensure any Spring Remoting method invocations
can be associated with a Subject for security checks. -->
<beans:bean id="secureRemoteInvocationExecutor"
class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
<beans:property name="securityManager" ref="securityManager" />
</beans:bean>
<!-- Shiro filter -->
<beans:bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<beans:property name="securityManager" ref="securityManager" />
<beans:property name="loginUrl" value="/login" />
<beans:property name="successUrl" value="/index" />
<beans:property name="unauthorizedUrl" value="/loginfailed" />
******************this is a fix I tried but it did not work*************
<beans:property name="filters">
<util:map>
<beans:entry key="roles">
<beans:bean class="org.apache.shiro.web.filter.authz.RolesAuthorizationFilter">
<beans:property name="unauthorizedUrl" value="/403.jsp"/>
</beans:bean>
</beans:entry>
</util:map>
</beans:property>
************************************************************************
<beans:property name="filterChainDefinitions">
<beans:value>
<!-- !!! Order matters !!! -->
/login = anon
/loginfailed = anon
/** = authc
</beans:value>
</beans:property>
</beans:bean>
</beans:beans>
****************************************</code>*************************************************
My web.xml is:
************************<code>****************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd" version="2.4">
<display-name>PlatformWeb</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Shiro Security -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
******************this is a fix I tried but it did not work*************
<error-page>
<error-code>401</error-code>
<location>/403.jsp</location>
</error-page>
*************************************************************************
</web-app>
****************************************</code>*************************************************
My controller is :
****************************************<code>*************************************************
@RequestMapping(value = "/index", method = RequestMethod.GET)
@RequiresRoles("ROLE_ADMIN")
public String listMembers(ModelMap model) {
logger.info("Listing Platform Team members...");
model.put("member", new TeamMember());
model.put("memberList", memberService.listMembers());
return "member";
}
****************************************</code>*************************************************
Thanks,
Deepthi