Apache Shiro的配置主要分为四部分:
- 对象和属性的定义与配置
- URL的过滤器配置
- 静态用户配置
- 静态角色配置
Apache Shiro的大多数组件是基于POJO的,因此我们可以使用POJO兼容的任何配置机制进行配置,例如:Java代码、Sping XML、YAML、JSON、ini文件等等。下面,以Spring XML的配置方式为例,并且对其中的一些配置参数进行一些简单说明。
一、在web.xml中添加shiro过滤器
Shiro过滤器的配置
Shiro主要是通过URL过滤来进行安全管理,这里的配置便是指定具体授权规则定义。
- <!-- Shiro filter-->
- <filter>
- <filter-name>shiroFilter</filter-name>
- <filter-class>
- org.springframework.web.filter.DelegatingFilterProxy
- </filter-class>
- </filter>
- <filter-mapping>
- <filter-name>shiroFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
二、在Spring的applicationContext.xml中添加shiro配置
1、添加shiroFilter定义
- <!-- Shiro Filter -->
- <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
- <property name="securityManager" ref="securityManager" />
- <property name="loginUrl" value="/login" />
- <property name="successUrl" value="/user/list" />
- <property name="unauthorizedUrl" value="/login" />
- <property name="filterChainDefinitions">
- <value>
- /login = anon
- /user/** = authc
- /role/edit/* = perms[role:edit]
- /role/save = perms[role:edit]
- /role/list = perms[role:view]
- /** = authc
- </value>
- </property>
- </bean>
2、添加securityManager定义
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
- <property name="realm" ref="myRealm" />
- <property name="sessionManager" ref="sessionManager"/>
- <property name="cacheManager" ref="cacheManager"/>
- <property name="sessionMode" value="native"/>
-
- </bean>
3、添加realm定义
- <bean id=" myRealm" class="com...MyRealm" />
URL过滤器配置说明:
Shiro可以通过配置文件实现基于URL的授权验证。FilterChain定义格式:
URL_Ant_Path_Expression = Path_Specific_Filter_Chain
每个URL配置,表示匹配该URL的应用程序请求将由对应的过滤器进行验证。
例如:
[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]
URL表达式说明
1、URL目录是基于HttpServletRequest.getContextPath()此目录设置
2、URL可使用通配符,**代表任意子目录
3、Shiro验证URL时,URL匹配成功便不再继续匹配查找。所以要注意配置文件中的URL顺序,尤其在使用通配符时。
Filter Chain定义说明
1、一个URL可以配置多个Filter,使用逗号分隔
2、当设置多个过滤器时,全部验证通过,才视为通过
3、部分过滤器可指定参数,如perms,roles
Shiro内置的FilterChain
Filter Name | Class |
anon | org.apache.shiro.web.filter.authc.AnonymousFilter |
authc | org.apache.shiro.web.filter.authc.FormAuthenticationFilter |
authcBasic | org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter |
perms | org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter |
port | org.apache.shiro.web.filter.authz.PortFilter |
rest | org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter |
roles | org.apache.shiro.web.filter.authz.RolesAuthorizationFilter |
ssl | org.apache.shiro.web.filter.authz.SslFilter |
user | org.apache.shiro.web.filter.authc.UserFilter |
URL过滤器配置说明:
Shiro可以通过配置文件实现基于URL的授权验证。FilterChain定义格式:
URL_Ant_Path_Expression = Path_Specific_Filter_Chain
每个URL配置,表示匹配该URL的应用程序请求将由对应的过滤器进行验证。
例如:
[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]
URL表达式说明
1、URL目录是基于HttpServletRequest.getContextPath()此目录设置
2、URL可使用通配符,**代表任意子目录
3、Shiro验证URL时,URL匹配成功便不再继续匹配查找。所以要注意配置文件中的URL顺序,尤其在使用通配符时。
Filter Chain定义说明
1、一个URL可以配置多个Filter,使用逗号分隔
2、当设置多个过滤器时,全部验证通过,才视为通过
3、部分过滤器可指定参数,如perms,roles
Shiro内置的FilterChain
Filter Name | Class |
anon | org.apache.shiro.web.filter.authc.AnonymousFilter |
authc | org.apache.shiro.web.filter.authc.FormAuthenticationFilter |
authcBasic | org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter |
perms | org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter |
port | org.apache.shiro.web.filter.authz.PortFilter |
rest | org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter |
roles | org.apache.shiro.web.filter.authz.RolesAuthorizationFilter |
ssl | org.apache.shiro.web.filter.authz.SslFilter |
user | org.apache.shiro.web.filter.authc.UserFilter |
三、实现MyRealm:继承AuthorizingRealm,并重写认证授权方法
- public class MyRealm extends AuthorizingRealm{
- private AccountManager accountManager;
- public void setAccountManager(AccountManager accountManager) {
- this.accountManager = accountManager;
- }
- /**
- * 授权信息
- */
- protected AuthorizationInfo doGetAuthorizationInfo(
- PrincipalCollection principals) {
- String username=(String)principals.fromRealm(getName()).iterator().next();
- if( username != null ){
- User user = accountManager.get( username );
- if( user != null && user.getRoles() != null ){
- SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
- for( SecurityRole each: user.getRoles() ){
- info.addRole(each.getName());
- info.addStringPermissions(each.getPermissionsAsString());
- }
- return info;
- }
- }
- return null;
- }
- /**
- * 认证信息
- */
- protected AuthenticationInfo doGetAuthenticationInfo(
- AuthenticationToken authcToken ) throws AuthenticationException {
- UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
- String userName = token.getUsername();
- if( userName != null && !"".equals(userName) ){
- User user = accountManager.login(token.getUsername(),
- String.valueOf(token.getPassword()));
- if( user != null )
- return new SimpleAuthenticationInfo(
- user.getLoginName(),user.getPassword(), getName());
- }
- return null;
- }
- }