shiro配置

我项目中的shiro配置

  1. pom引入的jar

        <dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-ehcache</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-cas</artifactId>
		</dependency>
		<dependency>
			<groupId>org.crazycake</groupId>
			<artifactId>shiro-redis</artifactId>
			<version>2.4.2.1-RELEASE</version>
		</dependency>

Spring整合配置

  1. web.xml中配置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>
    		<dispatcher>REQUEST</dispatcher>
    		<dispatcher>INCLUDE</dispatcher>
    	</filter-mapping>
    

     

  2. 在Spring的applicationContext.xml中添加shiro配置 
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    		<property name="securityManager" ref="securityManager" />
    		<property name="loginUrl" value="/login/loginPage" />
    		<property name="unauthorizedUrl" value="/index/401" />
    		<property name="filters">
                <map>
                    <entry key="roleOrFilter">
                        <bean class="com.tl.b2b.util.RolesOrAuthorizationFilter" />
                    </entry>
                </map>
            </property>
    		<property name="filterChainDefinitions">
    			<value>
    			<!-- 注意顺序为:权限控制,不需要登录,需要登录 。新增链接一定要按照顺序添加,否则会出现页面访问问题-->
    			

    securityManager:这个属性是必须的。

    loginUrl:没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面。

    successUrl:登录成功默认跳转页面,不配置则跳转至”/”。如果登陆前点击的一个需要登录的页面,则在登录自动跳转到那个需要登录的页面。不跳转到此。

    unauthorizedUrl:没有权限默认跳转的页面。

  3. anon,authcBasic,auchc,user是认证过滤器

    anon:例子/admins/**=anon 没有参数,表示可以匿名使用。

    authc:例如/admins/user/**=authc表示需要认证(登录)才能使用,没有参数                                                                              user:例如/admins/user/**=user没有参数表示必须存在用户,当登入操作时不做检查                                                               

    roles:例子/admins/user/**=roles[admin],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,当有多个参数时,例如admins/user/**=roles["admin,guest"],每个参数通过才算通过,相当于hasAllRoles()方法。

  4.  

    perms,roles,ssl,rest,port是授权过滤器

    perms:例子/admins/user/**=perms[user:add:*],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,例如/admins/user/**=perms["user:add:*,user:modify:*"],当有多个参数时必须每个参数都通过才通过,想当于isPermitedAll()方法。

     

    rest:例子/admins/user/**=rest[user],根据请求的方法,相当于/admins/user/**=perms[user:method] ,其中method为post,get,delete等。

    port:例子/admins/user/**=port[8081],当请求的url的端口不是8081是跳转到schemal://serverName:8081?queryString,其中schmal是协议http或https等,serverName是你访问的host,8081是url配置里port的端口,queryString

  5. 在applicationContext.xml中添加securityManagerper配置

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    		<!-- <property name="authenticator" ref="authenticator" /> -->
    		<property name="realm" ref="MyRealm"/> 
    		<property name="cacheManager" ref="shiroEhcacheManager" />
    		<!-- 记住我功能 -->
            <property name="rememberMeManager" ref="rememberMeManager"/>
            <property name="sessionManager" ref="sessionManager"/>
    	</bean>
    
    <bean id="MyRealm" class="com.xx.xxx.security.realm.ShiroDbRealm"></bean>
    	<bean id="atLeastOneSuccessfulStrategy" class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy" />
    	
    	<!-- 用户授权信息Cache, 采用EhCache -->  
    
    	<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
    		<property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml" />
    	</bean>
    	
    	<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">  
    		<property name="cipherKey" value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}"/>  
    		<property name="cookie" ref="rememberMeCookie"/>  
    	</bean>
    	
    	<!-- 会话管理器 -->
    	<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
    		<property name="sessionIdCookieEnabled" value="true"/>
    		<property name="sessionIdCookie" ref="sessionIdCookie"/>
    	</bean>

     

  6. 配置相应的realm等
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值