web项目shiro与spring集成 maven依赖及web配置详解

依赖shiro的maven坐标:

            <dependency>  
	            <groupId>org.apache.shiro</groupId>  
	            <artifactId>shiro-core</artifactId>  
	            <version>${shiro.version}</version>  
	        </dependency>  
	        <dependency>  
	            <groupId>org.apache.shiro</groupId>  
	            <artifactId>shiro-web</artifactId>  
	            <version>${shiro.version}</version>  
	        </dependency>  
	        <dependency>  
	            <groupId>org.apache.shiro</groupId>  
	            <artifactId>shiro-cas</artifactId>  
	            <version>${shiro.version}</version>  
	        </dependency>  
	        <dependency>  
	            <groupId>org.apache.shiro</groupId>  
	            <artifactId>shiro-spring</artifactId>  
	            <version>${shiro.version}</version>  
	  	    </dependency>
		    <dependency>  
		        <groupId>org.apache.shiro</groupId>  
		        <artifactId>shiro-ehcache</artifactId>  
		        <version>1.2.0</version>  
		    </dependency> 
	  	    <!-- ehcache -->
	  	    <dependency>  
		        <groupId>net.sf.ehcache</groupId>  
		        <artifactId>ehcache-core</artifactId>  
		        <version>2.5.3</version>  
	 	    </dependency>  
在web.xml中添加shiro的filter

<!-- 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> 

添加shiro的xml配置文件applicationContext-shiro.xml

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> 
<!-- shiroFilter Bean的id 必须和web.xml中配置的filter的name一直  --> 
 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">    
        <property name="securityManager" ref="securityManager" />  
<!--loginUrl 没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面。 -->  
 <property name="loginUrl" value="/login" />  
<!--successUrl 登录成功默认跳转页面,不配置则跳转至”/”。如果登陆前点击的一个需要登录的页面,则在登录自动跳转到那个需要登录的页面。不跳转到此。-->  
 <property name="successUrl" value="/login/loginSuccessFull" />  
<!--unauthorizedUrl:没有权限默认跳转的页面。-->    
        <property name="unauthorizedUrl" value="/login/unauthorized" />   
<!-- 这个就是权限路径过滤  会在下面单独列出来 -->   
        <property name="filterChainDefinitions">    
            <value>    
                /home* = anon    
                / = anon    
                /logout = logout    
                /role/** = roles[admin]    
                /permission/** = perms[permssion:look]    
                /** = authc    
            </value>    
        </property>    
    </bean>  
<!-- 配置securityManager -->  
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">    
<!-- 单realm应用。如果有多个realm,使用‘realms’属性代替 -->    
    <property name="realm" ref="sampleRealm" />    
    <property name="cacheManager" ref="cacheManager" />  <!-- 缓存配置,集群环境慎用 -->  
</bean>   
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager" />  
<!-- 一个简单的jdbcRealm -->  
<bean id="sampleRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">    
    <property name="dataSource" ref="dataSource" />  
    <!-- dataSource数据源,可以引用spring中配置的数据源 -->  
    <property name="authenticationQuery"  value="select t.password from my_user t where t.username = ?" />  
    <!-- authenticationQuery登录认证用户的查询SQL,需要用登录用户名作为条件,查询密码字段。 -->   
    <property name="userRolesQuery"  value="select a.rolename from my_user_role t left join my_role a on t.roleid = a.id where t.username = ? " />    
    <!-- userRolesQuery用户角色查询SQL,需要通过登录用户名去查询。查询角色字段 -->   
    <property name="permissionsQuery"  value="SELECT B.PERMISSION FROM MY_ROLE T LEFT JOIN MY_ROLE_PERMISSION A ON T.ID = A.ROLE_ID LEFT JOIN MY_PERMISSION B ON A.PERMISSION = B.ID WHERE T.ROLENAME = ? " />    
    <!-- permissionsQuery用户的权限资源查询SQL,需要用单一角色查询角色下的权限资源,如果存在多个角色,则是遍历每个角色,分别查询出权限资源并添加到集合中。 -->  
    <property name="permissionsLookupEnabled" value="true" />  
    <!-- permissionsLookupEnabled默认false。False时不会使用permissionsQuery的SQL去查询权限资源。设置为true才会去执行。-->    
    <property name="saltStyle" value="NO_SALT" />    
    <!-- saltStyle密码是否加盐,默认是NO_SALT不加盐。加盐有三种选择CRYPT,COLUMN,EXTERNAL。这里按照不加盐处理。 -->  
    <property name="credentialsMatcher" ref="hashedCredentialsMatcher" />   
    <!-- credentialsMatcher密码匹配规则 -->   
</bean>    
<bean id="hashedCredentialsMatcher"    
    class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">    
        <property name="hashAlgorithmName" value="MD5" />  
        <!-- hashAlgorithmName必须的,没有默认值。可以有MD5或者SHA-1,如果对密码安全有更高要求可以用SHA-256或者更高。这里使用MD5 -->    
        <property name="storedCredentialsHexEncoded" value="true" />  
        <!-- storedCredentialsHexEncoded默认是true,此时用的是密码加密用的是Hex编码;false时用Base64编码 -->    
        <property name="hashIterations" value="1" />  
        <!-- hashIterations迭代次数,默认值是1。 -->    
</bean> 
</beans>   


官方给出的内置权限过滤器(有两种)
第一种:认证过滤器
anon org.apache.shiro.web.filter.authc.AnonymousFilter
anon 表示可以匿名使用。(即无权限控制)
authc org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authc表示需要认证(登录)才能使用
user org.apache.shiro.web.filter.authc.UserFilter
user 表示必须存在用户,当登入操作时不做检查      
---------注意user和authc不同:当应用开启了rememberMe时,用户下次访问时可以是一个user,但绝不会是authc,因为authc是需要重新认证的
authcBasic org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
authcBasic 表示httpBasic认证
第二种:授权过滤器
perms org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
perms 参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,例如/admins/user/**=perms["user:add:*,user:modify:*"],当有多个参数时必须每个参数都通过才通过,想当于isPermitedAll()方法。


port org.apache.shiro.web.filter.authz.PortFilter
port 当请求的url的端口不是指定端口是跳转到schemal://serverName:指定端口?queryString,其中schmal是协议http或https等,serverName是你访问的host,指定端口是url配置里port的端口,queryString


rest org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
rest这个暂时没有理解,理解后过来进行修改
roles org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
roles 参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,当有多个参数时,例如admins/user/**=roles["admin,guest"],每个参数通过才算通过,相当于hasAllRoles()方法。


ssl org.apache.shiro.web.filter.authz.SslFilter
ssl 表示安全的url请求,协议为https




























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值