1.引入Shiro的Maven依赖

  1. <!-- Spring 整合Shiro需要的依赖 -->  

  2.   

  3.     <dependency>  

  4.         <groupId>org.apache.shiro</groupId>  

  5.         <artifactId>shiro-core</artifactId>  

  6.         <version>1.2.1</version>  

  7.     </dependency>  

  8.     <dependency>  

  9.         <groupId>org.apache.shiro</groupId>  

  10.         <artifactId>shiro-web</artifactId>  

  11.         <version>1.2.1</version>  

  12.     </dependency>  

  13.     <dependency>  

  14.         <groupId>org.apache.shiro</groupId>  

  15.         <artifactId>shiro-ehcache</artifactId>  

  16.         <version>1.2.1</version>  

  17.     </dependency>  

  18.     <dependency>  

  19.         <groupId>org.apache.shiro</groupId>  

  20.         <artifactId>shiro-spring</artifactId>  

  21.         <version>1.2.1</version>  

  22.     </dependency>  

  23.     <!-- 除此之外还有一些东西也不可少spring, spring-mvc, ibatis等 spring.3.1.2 spring-mvc.3.1.2 ibatis.2.3.4 cglib.2.2 -->  

<!-- Spring 整合Shiro需要的依赖 -->

	<dependency>
		<groupId>org.apache.shiro</groupId>
		<artifactId>shiro-core</artifactId>
		<version>1.2.1</version>
	</dependency>
	<dependency>
		<groupId>org.apache.shiro</groupId>
		<artifactId>shiro-web</artifactId>
		<version>1.2.1</version>
	</dependency>
	<dependency>
		<groupId>org.apache.shiro</groupId>
		<artifactId>shiro-ehcache</artifactId>
		<version>1.2.1</version>
	</dependency>
	<dependency>
		<groupId>org.apache.shiro</groupId>
		<artifactId>shiro-spring</artifactId>
		<version>1.2.1</version>
	</dependency>
	<!-- 除此之外还有一些东西也不可少spring, spring-mvc, ibatis等 spring.3.1.2 spring-mvc.3.1.2 
		ibatis.2.3.4 cglib.2.2 -->


2.web.xml中配置

  1. <!-- 配置shiro的核心拦截器 -->  

  2.    <filter>    

  3.        <filter-name>shiroFilter</filter-name>    

  4.        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    

  5.    </filter>    

  6.    <filter-mapping>    

  7.        <filter-name>shiroFilter</filter-name>    

  8.        <url-pattern>/*</url-pattern>    

  9.    </filter-mapping>   

 <!-- 配置shiro的核心拦截器 -->
    <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>


3.编写自己的UserRealm类继承自Realm,主要实现认证和授权的管理操作


  1. package com.jay.demo.shiro;  

  2.   

  3. import java.util.HashSet;  

  4. import java.util.Iterator;  

  5. import java.util.Set;  

  6.   

  7. import org.apache.shiro.authc.AuthenticationException;  

  8. import org.apache.shiro.authc.AuthenticationInfo;  

  9. import org.apache.shiro.authc.AuthenticationToken;  

  10. import org.apache.shiro.authc.LockedAccountException;  

  11. import org.apache.shiro.authc.SimpleAuthenticationInfo;  

  12. import org.apache.shiro.authc.UnknownAccountException;  

  13. import org.apache.shiro.authz.AuthorizationInfo;  

  14. import org.apache.shiro.authz.SimpleAuthorizationInfo;  

  15. import org.apache.shiro.realm.AuthorizingRealm;  

  16. import org.apache.shiro.subject.PrincipalCollection;  

  17. import org.springframework.beans.factory.annotation.Autowired;  

  18.   

  19. import com.jay.demo.bean.Permission;  

  20. import com.jay.demo.bean.Role;  

  21. import com.jay.demo.bean.User;  

  22. import com.jay.demo.service.UserService;  

  23.   

  24. public class UserRealm extends AuthorizingRealm{  

  25.       

  26.     @Autowired  

  27.     private UserService userService;  

  28.   

  29.     /** 

  30.      * 授权操作 

  31.      */  

  32.     @Override  

  33.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {  

  34. //      String username = (String) getAvailablePrincipal(principals);  

  35.         String username = (String) principals.getPrimaryPrincipal();  

  36.           

  37.         Set<Role> roleSet =  userService.findUserByUsername(username).getRoleSet();  

  38.         //角色名的集合  

  39.         Set<String> roles = new HashSet<String>();  

  40.         //权限名的集合  

  41.         Set<String> permissions = new HashSet<String>();  

  42.           

  43.         Iterator<Role> it = roleSet.iterator();  

  44.         while(it.hasNext()){  

  45.             roles.add(it.next().getName());  

  46.             for(Permission per:it.next().getPermissionSet()){  

  47.                 permissions.add(per.getName());  

  48.             }  

  49.         }  

  50.   

  51.           

  52.         SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();  

  53.           

  54.         authorizationInfo.addRoles(roles);  

  55.         authorizationInfo.addStringPermissions(permissions);  

  56.           

  57.           

  58.         return authorizationInfo;  

  59.     }  

  60.   

  61.     /** 

  62.      * 身份验证操作 

  63.      */  

  64.     @Override  

  65.     protected AuthenticationInfo doGetAuthenticationInfo(  

  66.             AuthenticationToken token) throws AuthenticationException {  

  67.           

  68.         String username = (String) token.getPrincipal();  

  69.         User user = userService.findUserByUsername(username);  

  70.           

  71.         if(user==null){  

  72.             //木有找到用户  

  73.             throw new UnknownAccountException("没有找到该账号");  

  74.         }  

  75.         /* if(Boolean.TRUE.equals(user.getLocked())) {   

  76.                 throw new LockedAccountException(); //帐号锁定   

  77.             } */  

  78.           

  79.         /** 

  80.          * 交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配,如果觉得人家的不好可以在此判断或自定义实现   

  81.          */  

  82.         SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),getName());  

  83.           

  84.           

  85.         return info;  

  86.     }  

  87.       

  88.     @Override  

  89.     public String getName() {  

  90.         return getClass().getName();  

  91.     }  

  92.   

  93. }  

package com.jay.demo.shiro;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.jay.demo.bean.Permission;
import com.jay.demo.bean.Role;
import com.jay.demo.bean.User;
import com.jay.demo.service.UserService;

public class UserRealm extends AuthorizingRealm{
	
	@Autowired
	private UserService userService;

	/**
	 * 授权操作
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//		String username = (String) getAvailablePrincipal(principals);
		String username = (String) principals.getPrimaryPrincipal();
		
		Set<Role> roleSet =  userService.findUserByUsername(username).getRoleSet();
		//角色名的集合
		Set<String> roles = new HashSet<String>();
		//权限名的集合
		Set<String> permissions = new HashSet<String>();
		
		Iterator<Role> it = roleSet.iterator();
		while(it.hasNext()){
			roles.add(it.next().getName());
			for(Permission per:it.next().getPermissionSet()){
				permissions.add(per.getName());
			}
		}

		
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		
		authorizationInfo.addRoles(roles);
		authorizationInfo.addStringPermissions(permissions);
		
		
		return authorizationInfo;
	}

	/**
	 * 身份验证操作
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		
		String username = (String) token.getPrincipal();
		User user = userService.findUserByUsername(username);
		
		if(user==null){
			//木有找到用户
			throw new UnknownAccountException("没有找到该账号");
		}
		/* if(Boolean.TRUE.equals(user.getLocked())) {  
	            throw new LockedAccountException(); //帐号锁定  
	        } */
		
		/**
		 * 交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配,如果觉得人家的不好可以在此判断或自定义实现  
		 */
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),getName());
		
		
		return info;
	}
	
	@Override
	public String getName() {
		return getClass().getName();
	}

}


4.在Spring的applicationContext.xml中进行Shiro的相关配置


1、添加shiroFilter定义 

Xml代码  收藏代码

  1. <!-- Shiro Filter -->  

  2. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  

  3.     <property name="securityManager" ref="securityManager" />  

  4.     <property name="loginUrl" value="/login" />  

  5.     <property name="successUrl" value="/user/list" />  

  6.     <property name="unauthorizedUrl" value="/login" />  

  7.     <property name="filterChainDefinitions">  

  8.         <value>  

  9.             /login = anon  

  10.             /user/** = authc  

  11.             /role/edit/* = perms[role:edit]  

  12.             /role/save = perms[role:edit]  

  13.             /role/list = perms[role:view]  

  14.             /** = authc  

  15.         </value>  

  16.     </property>  

  17. </bean>  


2、添加securityManager定义 

Xml代码  收藏代码

  1. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  

  2.     <property name="realm" ref="myRealm" />  

  3. </bean>  


3、添加realm定义 

Xml代码  收藏代码

  1. <bean id=" myRealm" class="com.jay.demo.shiro.


  2. UserRealm<span class="attribute-value" style="font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: rgb(250, 250, 250);">"</span><span style="color: black; font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: rgb(250, 250, 250);"> </span><span class="tag" style="font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; color: rgb(0, 102, 153); font-weight: bold; background-color: rgb(250, 250, 250);">/></span><span style="color: black; font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: rgb(250, 250, 250);">  </span>
    1. UserRealm<span class="attribute-value" style="font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: rgb(250, 250, 250);">"</span><span style="color: black; font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono''Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: rgb(250250250);"> </span><span class="tag" style="font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono''Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; color: rgb(0102153); font-weight: bold; background-color: rgb(250250250);">/></span><span style="color: black; font-size: 1em; font-family: Monaco, 'DejaVu Sans Mono''Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: rgb(250250250);">  </span>  

4、配置EhCache


  <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager" />

5、保证实现了Shiro内部lifecycle函数的bean执行

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>


特别注意:

   如果使用Shiro相关的注解,需要在springmvc-servlet.xml中配置一下信息



<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/><bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">    <property name="securityManager" ref="securityManager"/></bean>

备注:Shiro权限管理的过滤器解释

默认过滤器(10个)   

  1. anon -- org.apache.shiro.web.filter.authc.AnonymousFilter  

  2. authc -- org.apache.shiro.web.filter.authc.FormAuthenticationFilter  

  3. authcBasic -- org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter  

  4. perms -- org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter  

  5. port -- org.apache.shiro.web.filter.authz.PortFilter  

  6. rest -- org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter  

  7. roles -- org.apache.shiro.web.filter.authz.RolesAuthorizationFilter  

  8. ssl -- org.apache.shiro.web.filter.authz.SslFilter  

  9. user -- org.apache.shiro.web.filter.authc.UserFilter  

  10. logout -- org.apache.shiro.web.filter.authc.LogoutFilter  

  11.   

  12.   

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

  14. authc:例如/admins/user/**=authc表示需要认证(登录)才能使用,没有参数   

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

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

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

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

  19. authcBasic:例如/admins/user/**=authcBasic没有参数表示httpBasic认证   

  20. ssl:例子/admins/user/**=ssl没有参数,表示安全的url请求,协议为https   

  21. user:例如/admins/user/**=user没有参数表示必须存在用户,当登入操作时不做检查   

默认过滤器(10个) 
anon -- org.apache.shiro.web.filter.authc.AnonymousFilter
authc -- org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasic -- org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
perms -- org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
port -- org.apache.shiro.web.filter.authz.PortFilter
rest -- org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
roles -- org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
ssl -- org.apache.shiro.web.filter.authz.SslFilter
user -- org.apache.shiro.web.filter.authc.UserFilter
logout -- org.apache.shiro.web.filter.authc.LogoutFilter


anon:例子/admins/**=anon 没有参数,表示可以匿名使用。 
authc:例如/admins/user/**=authc表示需要认证(登录)才能使用,没有参数 
roles:例子/admins/user/**=roles[admin],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,当有多个参数时,例如admins/user/**=roles["admin,guest"],每个参数通过才算通过,相当于hasAllRoles()方法。 
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是你访问的url里的?后面的参数。 
authcBasic:例如/admins/user/**=authcBasic没有参数表示httpBasic认证 
ssl:例子/admins/user/**=ssl没有参数,表示安全的url请求,协议为https 
user:例如/admins/user/**=user没有参数表示必须存在用户,当登入操作时不做检查


关于Shiro的标签应用:

  1. <shiro:authenticated> 登录之后  

  2. <shiro:notAuthenticated> 不在登录状态时  

  3. <shiro:guest> 用户在没有RememberMe时  

  4. <shiro:user> 用户在RememberMe时  

  5. <shiro:hasAnyRoles name="abc,123" > 在有abc或者123角色时  

  6. <shiro:hasRole name="abc"> 拥有角色abc  

  7. <shiro:lacksRole name="abc"> 没有角色abc  

  8. <shiro:hasPermission name="abc"> 拥有权限abc  

  9. <shiro:lacksPermission name="abc"> 没有权限abc  

  10. <shiro:principal> 显示用户登录名  

<shiro:authenticated> 登录之后
<shiro:notAuthenticated> 不在登录状态时
<shiro:guest> 用户在没有RememberMe时
<shiro:user> 用户在RememberMe时
<shiro:hasAnyRoles name="abc,123" > 在有abc或者123角色时
<shiro:hasRole name="abc"> 拥有角色abc
<shiro:lacksRole name="abc"> 没有角色abc
<shiro:hasPermission name="abc"> 拥有权限abc
<shiro:lacksPermission name="abc"> 没有权限abc
<shiro:principal> 显示用户登录名


以上是Shiro的相关配置,出于安全的考虑,一般都会使用ACL(基于角色的用户权限管理去控制用户登录后的权限)

ACL详细代码案例如下:


涉及到的表:3+2(User,Role,Permission  +  user-role,role-permission)

3张实体表+2张关系表

1.关于User类:

  1. package com.jay.demo.bean;  

  2.   

  3. import java.util.HashSet;  

  4. import java.util.Set;  

  5.   

  6. public class User {  

  7.     private String id;  

  8.     private String username;  

  9.     private String password;  

  10.     private Set<Role> roleSet = new HashSet<Role>();  

  11.       

  12.     public User() {  

  13.     }  

  14.   

  15.     public String getId() {  

  16.         return id;  

  17.     }  

  18.   

  19.     public void setId(String id) {  

  20.         this.id = id;  

  21.     }  

  22.   

  23.     public String getUsername() {  

  24.         return username;  

  25.     }  

  26.   

  27.     public void setUsername(String username) {  

  28.         this.username = username;  

  29.     }  

  30.   

  31.     public String getPassword() {  

  32.         return password;  

  33.     }  

  34.   

  35.     public void setPassword(String password) {  

  36.         this.password = password;  

  37.     }  

  38.   

  39.     public Set<Role> getRoleSet() {  

  40.         return roleSet;  

  41.     }  

  42.   

  43.     public void setRoleSet(Set<Role> roleSet) {  

  44.         this.roleSet = roleSet;  

  45.     }  

  46.   

  47.       

  48. }  

package com.jay.demo.bean;

import java.util.HashSet;
import java.util.Set;

public class User {
	private String id;
	private String username;
	private String password;
	private Set<Role> roleSet = new HashSet<Role>();
	
	public User() {
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Set<Role> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<Role> roleSet) {
		this.roleSet = roleSet;
	}

	
}


2.关于Role表

  1. package com.jay.demo.bean;  

  2.   

  3. import java.io.Serializable;  

  4. import java.util.HashSet;  

  5. import java.util.Set;  

  6.   

  7. public class Role implements Serializable {  

  8.   

  9.     private static final long serialVersionUID = -4987248128309954399L;  

  10.   

  11.     private Integer id;  

  12.     private String name;  

  13.     private Set<Permission> permissionSet = new HashSet<Permission>();  

  14.   

  15.     public Role() {  

  16.         super();  

  17.     }  

  18.       

  19.     // --------------------------------------------------------------------------------  

  20.   

  21.     @Override  

  22.     public int hashCode() {  

  23.         final int prime = 31;  

  24.         int result = 1;  

  25.         result = prime * result + ((id == null) ? 0 : id.hashCode());  

  26.         return result;  

  27.     }  

  28.   

  29.     @Override  

  30.     public boolean equals(Object obj) {  

  31.         if (this == obj)  

  32.             return true;  

  33.         if (obj == null)  

  34.             return false;  

  35.         if (getClass() != obj.getClass())  

  36.             return false;  

  37.         Role other = (Role) obj;  

  38.         if (id == null) {  

  39.             if (other.id != null)  

  40.                 return false;  

  41.         } else if (!id.equals(other.id))  

  42.             return false;  

  43.         return true;  

  44.     }  

  45.       

  46.     // --------------------------------------------------------------------------------  

  47.   

  48.     public Integer getId() {  

  49.         return id;  

  50.     }  

  51.   

  52.     public void setId(Integer id) {  

  53.         this.id = id;  

  54.     }  

  55.   

  56.     public String getName() {  

  57.         return name;  

  58.     }  

  59.   

  60.     public void setName(String name) {  

  61.         this.name = name;  

  62.     }  

  63.   

  64.     public Set<Permission> getPermissionSet() {  

  65.         return permissionSet;  

  66.     }  

  67.   

  68.     public void setPermissionSet(Set<Permission> permissionSet) {  

  69.         this.permissionSet = permissionSet;  

  70.     }  

  71.   

  72. }  

package com.jay.demo.bean;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Role implements Serializable {

	private static final long serialVersionUID = -4987248128309954399L;

	private Integer id;
	private String name;
	private Set<Permission> permissionSet = new HashSet<Permission>();

	public Role() {
		super();
	}
	
	// --------------------------------------------------------------------------------

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Role other = (Role) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	// --------------------------------------------------------------------------------

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Permission> getPermissionSet() {
		return permissionSet;
	}

	public void setPermissionSet(Set<Permission> permissionSet) {
		this.permissionSet = permissionSet;
	}

}


3.关于permission表

  1. <pre name="code" class="java">package com.jay.demo.bean;  

  2.   

  3. import java.io.Serializable;  

  4.   

  5. public class Permission implements Serializable {  

  6.   

  7.     private static final long serialVersionUID = -8025597823572680802L;  

  8.   

  9.     private Integer id;  

  10.     private String name;  

  11.   

  12.     public Permission() {  

  13.         super();  

  14.     }  

  15.   

  16.     // --------------------------------------------------------------------------------------  

  17.   

  18.     @Override  

  19.     public int hashCode() {  

  20.         final int prime = 31;  

  21.         int result = 1;  

  22.         result = prime * result + ((id == null) ? 0 : id.hashCode());  

  23.         return result;  

  24.     }  

  25.   

  26.     @Override  

  27.     public boolean equals(Object obj) {  

  28.         if (this == obj)  

  29.             return true;  

  30.         if (obj == null)  

  31.             return false;  

  32.         if (getClass() != obj.getClass())  

  33.             return false;  

  34.         Permission other = (Permission) obj;  

  35.         if (id == null) {  

  36.             if (other.id != null)  

  37.                 return false;  

  38.         } else if (!id.equals(other.id))  

  39.             return false;  

  40.         return true;  

  41.     }  

  42.   

  43.     // --------------------------------------------------------------------------------------  

  44.   

  45.     public Integer getId() {  

  46.         return id;  

  47.     }  

  48.   

  49.     public void setId(Integer id) {  

  50.         this.id = id;  

  51.     }  

  52.   

  53.     public String getName() {  

  54.         return name;  

  55.     }  

  56.   

  57.     public void setName(String name) {  

  58.         this.name = name;  

  59.     }  

  60.   

  61. }  

<pre name="code" class="java">package com.jay.demo.bean;

import java.io.Serializable;

public class Permission implements Serializable {

	private static final long serialVersionUID = -8025597823572680802L;

	private Integer id;
	private String name;

	public Permission() {
		super();
	}

	// --------------------------------------------------------------------------------------

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Permission other = (Permission) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	// --------------------------------------------------------------------------------------

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}


4.dao层接口


  1. package com.jay.demo.dao;  

  2.   

  3. import com.jay.demo.bean.User;  

  4.   

  5. public interface UserDao {  

  6.       

  7.     User findUserByUsername(String username);   

  8. }  

package com.jay.demo.dao;

import com.jay.demo.bean.User;

public interface UserDao {
	
	User findUserByUsername(String username); 
}




4.使用Mybatis完成的Dao层实现

  1. <?xml version="1.0" encoding="UTF-8" ?>  

  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  

  3. <mapper namespace="com.jay.demo.dao.UserDao">  

  4.     <resultMap id="userMap" type="com.jay.demo.bean.User">  

  5.     <id property="id" column="USER_ID"/>  

  6.     <result property="username" column="USER_USERNAME"/>  

  7.     <result property="password" column="USER_PASSWORD"/>  

  8.     <!-- 进行 多表关联插叙,先关联user和role -->  

  9.     <collection property="roleSet" column="roleid" ofType="com.jay.demo.bean.Role">  

  10.     <id property="id" column="ROLE_ID"/>  

  11.     <result property="name" column="ROLE_NAME"/>  

  12.     <!-- 再在role中关联role和permission -->  

  13.     <collection property="permissionSet" column="permissionid" ofType="com.jay.demo.bean.Permission">  

  14.     <id property="id" column="permission_id"/>  

  15.     <result property="name" column="permission_name"/>  

  16.     </collection>  

  17.     </collection>  

  18.       

  19.     </resultMap>  

  20.       


  21.       

  22.       

  23.     <!--  通过User来查找Role   -->    

  24.     <!-- <select id="selectRoleByUser" parameterType="int" resultMap="RoleMap">    

  25.         select * from tbl_role_user user_id  = #{id}     

  26.     </select>    

  27.       

  28.   

  29.     <resultMap  id="roleMap" type="com.jay.demo.bean.User">  

  30.         <result property="id" column="ROLE_ID" />  

  31.         <result property="name" column="ROLE_NAME" />  

  32.     </resultMap>  

  33.       

  34.     <resultMap id="permissionMap" type="com.jay.demo.bean.Permission">  

  35.         <result property="id" column="PERMISSION_ID" />  

  36.         <result property="name" column="PERMISSION_NAME" />  

  37.     </resultMap> -->  

  38.       

  39.       

  40.   

  41. <sql id="select-base-01">    

  42.         SELECT     

  43.             u.USER_ID,    

  44.             u.USER_USERNAME,    

  45.             u.USER_PASSWORD,    

  46.             r.ROLE_ID,    

  47.             r.ROLE_NAME,    

  48.             p.PERMISSION_ID,    

  49.             p.PERMISSION_NAME    

  50.         FROM    

  51.           tbl_user as u,    

  52.           tbl_role as r,    

  53.           tbl_permission as p,    

  54.           tbl_permission_role as pr,    

  55.           tbl_role_user as ru    

  56.         WHERE    

  57.           u.USER_ID = ru.USER_ID    

  58.         AND    

  59.           r.ROLE_ID = ru.ROLE_ID    

  60.         AND    

  61.           p.PERMISSION_ID = pr.PERMISSION_ID    

  62.         AND    

  63.           r.ROLE_ID = pr.ROLE_ID    

  64.     </sql>    

  65.       

  66.     <select id="findUserByUsername" parameterType="string" resultMap="userMap">    

  67.        <include refid="select-base-01" />    

  68.         AND    

  69.             u.USER_USERNAME = #{username}      

  70.             <!-- select * from tbl_user u, tbl_role r, tbl_role_user tu   

  71.             where u.user_id = tu.user_id and r.role_id = tu.role_id   

  72.             and user_username=#{username} -->  

  73.     </select>  

  74.       

  75. </mapper>  

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jay.demo.dao.UserDao">
	<resultMap id="userMap" type="com.jay.demo.bean.User">
	<id property="id" column="USER_ID"/>
	<result property="username" column="USER_USERNAME"/>
	<result property="password" column="USER_PASSWORD"/>
	<!-- 进行 多表关联插叙,先关联user和role -->
	<collection property="roleSet" column="roleid" ofType="com.jay.demo.bean.Role">
	<id property="id" column="ROLE_ID"/>
	<result property="name" column="ROLE_NAME"/>
	<!-- 再在role中关联role和permission -->
	<collection property="permissionSet" column="permissionid" ofType="com.jay.demo.bean.Permission">
	<id property="id" column="permission_id"/>
	<result property="name" column="permission_name"/>
	</collection>
	</collection>
	
	</resultMap>
	
	
	
	
	<!--  通过User来查找Role   -->  
    <!-- <select id="selectRoleByUser" parameterType="int" resultMap="RoleMap">  
        select * from tbl_role_user user_id  = #{id}   
    </select>  
    

	<resultMap  id="roleMap" type="com.jay.demo.bean.User">
		<result property="id" column="ROLE_ID" />
		<result property="name" column="ROLE_NAME" />
	</resultMap>
	
	<resultMap id="permissionMap" type="com.jay.demo.bean.Permission">
		<result property="id" column="PERMISSION_ID" />
		<result property="name" column="PERMISSION_NAME" />
	</resultMap> -->
	
	

<sql id="select-base-01">  
        SELECT   
            u.USER_ID,  
            u.USER_USERNAME,  
            u.USER_PASSWORD,  
            r.ROLE_ID,  
            r.ROLE_NAME,  
            p.PERMISSION_ID,  
            p.PERMISSION_NAME  
        FROM  
          tbl_user as u,  
          tbl_role as r,  
          tbl_permission as p,  
          tbl_permission_role as pr,  
          tbl_role_user as ru  
        WHERE  
          u.USER_ID = ru.USER_ID  
        AND  
          r.ROLE_ID = ru.ROLE_ID  
        AND  
          p.PERMISSION_ID = pr.PERMISSION_ID  
        AND  
          r.ROLE_ID = pr.ROLE_ID  
    </sql>  
    
    <select id="findUserByUsername" parameterType="string" resultMap="userMap">  
       <include refid="select-base-01" />  
        AND  
            u.USER_USERNAME = #{username}    
            <!-- select * from tbl_user u, tbl_role r, tbl_role_user tu 
            where u.user_id = tu.user_id and r.role_id = tu.role_id 
            and user_username=#{username} -->
    </select>
    
</mapper>



说明:详细代码和demo见附件

    

http://download.csdn.net/detail/he90227/7778689