01 shiro与spring集成

shiro与spring集成,本案例中没有配置缓存和单点登陆的实现。单点登陆需要导入shiro-cas-1.3.2.jar。Spring相关的JAR包这里不作列举。
shiro的JAR以及依赖包:

     <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-all</artifactId>
      <version>1.3.2</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.6.4</version>
    </dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.4</version>
  </dependency>


配置过滤器:
web.xml配置:

 <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配置:

	<bean id="shiroAuthenticationRealm" class="com.spec.shiro.ShiroAuthenticationRealm"></bean>
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="shiroAuthenticationRealm"/>
	</bean>
	<!---->
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<property name="securityManager" ref="securityManager"/>
		<property name="loginUrl" value="/"/>
		<!--未授权-->
		<property name="unauthorizedUrl" value="/index/goLogin.do"/>
		<property name="filterChainDefinitions">
			<value>
				/plug/** = anon
				/index.jsp = anon
				/login.jsp = anon
				/index/goLogin.do = anon
				/index/login.do = anon
				/** = authc
			</value>
		</property>
	</bean>

代码说明:
1、ShiroAuthenticationRealm自己实现的Realm类,代码见下面的“Realm实现”
2、securityManager中的realm属性,配置为我们自己实现的Realm。
3、shiroFilter的名称必须和web.xml中的filter-name相同。 DelegatingFilterProxy作用是自动到Spring容器查找名字为shiroFilter(filter-name)的bean并把所有Filter的操作委托给它。如果名称不一致,会抛出NoSuchBeanDefinitionException异常。因为Shiro会来IOC容器中查找<filter-name>一样的bean。
4、shiroFilter中loginUrl为登录页面的地址。
5、shiroFilter中successUrl为登录成功页面的地址。
6、shiroFilter中unauthorizedUrl认证未通过访问页面。(未经过授权页面)
7、shiroFilter中filterChainDefinitions属性,anon表示匿名访问(不需要认证与授权),authc表示需要认证


Realm实现:
ShiroAuthenticationRealm类:

public class ShiroAuthenticationRealm extends AuthorizingRealm {
    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 登陆信息和用户验证信息验证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        return null;
    }
}

代码说明:
Realm实现:一般继承AuthorizingRealm(授权)即可,其继承了AuthenticatingRealm(即身份验证),而且也间接继承了CachingRealm(带有缓存实现)。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值