shiro,

最近老项目整合,抽时间研究了一下,Apache Shiro。
先Apache Shiro在spring项目中基本配置。

1、spring-config-*.xml中配置

<!-- 安全认证过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
 <!-- 没有权限或者失败后跳转的页面 -->  
<property name="loginUrl" value="/index.jsp" />  
<property name="successUrl" value="/loginSuccess.jsp" />  
<property name="unauthorizedUrl" value="" />  
<property name="filterChainDefinitions">
    <value>  
        /logout.do = logout  
        /user/** = authc  
        /admin/** = authc,roles[admin]  
    </value>  
</property>
</bean>
<!-- 定义Shiro安全管理配置 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="systemAuthorizingRealm" />
<property name="sessionManager" ref="sessionManager" />
<property name="cacheManager" ref="shiroCacheManager" />
</bean>
2、web.xml中的配置

<!-- Apache Shiro -->
	<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>
**注意 filter-name 和 beanid必须相同。DelegatingFilterProxy是通过这个filter-name查找beanid的。

项目启动时:
首先,在 DelegatingFilterProxy类中初始化,将Filter delegate的变量初始为beanId= shiroFilter的安全认证 SpringShiroFilter 
又时间请详细看看 org.apache.shiro.spring.web.ShiroFilterFactoryBean是如何实现的。

执行url时,顺序为: org.springframework.web.filter.DelegatingFilterProxy.doFilter --->( SpringShiroFilter   delegate.doFilter-----> doFilterInternal

shiro多登陆配置
实现从多个登陆页面配置:
实现方式是,自定义filterchain
具体方式如下:
<!-- 安全认证过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
 <!-- 没有权限或者失败后跳转的页面 -->  
<property name="loginUrl" value="/index.jsp" />  
<property name="successUrl" value="/loginSuccess.jsp" />  
<property name="unauthorizedUrl" value="" />  
<property name="filterChainDefinitions">
    <value>  
        /logout.do = logout  
        /user/** = authc  
        /admin/** = authc,roles[admin]
        /index/** = shopauthc
    </value>  
</property>
<!--多登陆配置-->
	<bean id="shopformauthenticationfilter" class="org.mia.ontelshop.modules.sys.security.ShopFormAuthenticationFilter">
		<property name="loginUrl" value="index/login" />
		<property name="successUrl" value="index?login" />
	</bean>
原理: 当spring初始化shiroFilter,他会进行如下操作:

private void applyLoginUrlIfNecessary(Filter filter) {
        String loginUrl = getLoginUrl();
        if (StringUtils.hasText(loginUrl) && (filter instanceof AccessControlFilter)) {
            AccessControlFilter acFilter = (AccessControlFilter) filter;
            //only apply the login url if they haven't explicitly configured one already:
            String existingLoginUrl = acFilter.getLoginUrl();
            if (AccessControlFilter.DEFAULT_LOGIN_URL.equals(existingLoginUrl)) {
                acFilter.setLoginUrl(loginUrl);
            }
        }
    }
    private void applySuccessUrlIfNecessary(Filter filter) {
        String successUrl = getSuccessUrl();
        if (StringUtils.hasText(successUrl) && (filter instanceof AuthenticationFilter)) {
            AuthenticationFilter authcFilter = (AuthenticationFilter) filter;
            //only apply the successUrl if they haven't explicitly configured one already:
            String existingSuccessUrl = authcFilter.getSuccessUrl();
            if (AuthenticationFilter.DEFAULT_SUCCESS_URL.equals(existingSuccessUrl)) {
                authcFilter.setSuccessUrl(successUrl);
            }
        }
    }



ShiroFilterFactoryBean 这个类对注入的filter属性loginUrl、successUrl进行初始化,如何原来是默认值的化,就被初始成新的值,否则不改变。

你可能感兴趣的:(java,spring,shiro)