shiro权限控制

shiro 权限控制

1.增加jar包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

<!--shiro start -->
<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-ehcache</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${shiro.version}</version>
</dependency>

<!--shiro end -->

2.配置xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd"
default-lazy-init="false">

<description>Shiro安全配置</description>

<bean id="sessionDAO"
class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO" />

<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<property name="name" value="xxxxxx"></property>
<property name="path" value="/"></property>
</bean>

<bean id="shiroSessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionDAO" ref="sessionDAO" />
<property name="sessionValidationInterval" value="1800000" />
<property name="globalSessionTimeout" value="1800000" />
<property name="sessionIdUrlRewritingEnabled" value="false" />
<property name="sessionIdCookieEnabled" value="true" />
<property name="sessionIdCookie" ref="sessionIdCookie" />

</bean>


<!-- Shiro's main business-tier object for web-enabled applications -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="shiroCacheManager" />
<property name="sessionManager" ref="shiroSessionManager" />
<property name="realms">
<list>
<ref local="shiroHttpRealm" />
</list>
</property>
</bean>

<!-- 编写域 -->
<bean id="shiroHttpRealm" class="xxx.xx.ShiroHttpRealm">
</bean>

<!-- Shiro Filter -->
<bean id="roleOrFilter"
class="xxx.CustomRolesAuthorizationFilter" />
<bean id="updateCurrentInfoFilter"
class="xxx.ModuleAuthorizationFilter" />
<bean id="validateFilter"
class="xxx.ValidateURLFilter" />
<bean id="productFilter"
class="xxx.ProductAuthFilter" />
<bean id="permissionInfoFilter"
class="xxx.PermissionInfoFilter" />

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="${APP_URL}/login" />
<property name="successUrl" value="/index" />
<property name="unauthorizedUrl" value="/unauthorized" />
<property name="filters">
<map>
<entry key="roles" value-ref="roleOrFilter" />
<entry key="current" value-ref="updateCurrentInfoFilter" />
<entry key="validate" value-ref="validateFilter" />
<entry key="productAuth" value-ref="productFilter" />
<entry key="permissionInfo" value-ref="permissionInfoFilter" />
</map>
</property>

<!-- 从上到下,从左往右验证-->
<property name="filterChainDefinitions">
<value>
<!-- anon 任何用户发送的请求都能够访问-->
/unauthorized = anon
/assets/** = anon
/login/** = anon

<!-- user 用户是身份验证通过或RememberMe 登录-->
/feedback/** = user

<!-- authc 登录成功才能够访问-->
/common/**=authc

<!-- 具有1,2,3角色,通过productAuth,validate,permissionInfo过滤器验证-->
/**/edit**=roles[1,2,3],productAuth,validate,permissionInfo

<!-- 具有1角色,通过permissionInfo过滤器验证-->
/adminSettings/**=roles[1],permissionInfo

/index/**=authc
/** = authc

</value>
</property>
</bean>


<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

</beans>

3.编写shiro域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class ShiroHttpRealm extends AuthorizingRealm {

//判断用户是否正常,正常登录返回该用户信息
@Override
protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals ) {
// TODO Auto-generated method stub
return null;
}

//加载该用户权限信息,方便后面做判断
@Override
protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken token ) throws AuthenticationException {
// TODO Auto-generated method stub
return null;
}




}
文章目录
  1. 1. shiro 权限控制
    1. 1.1. 1.增加jar包
    2. 1.2. 2.配置xml
    3. 1.3. 3.编写shiro域
|