日期:2014-05-16 浏览次数:20550 次
关于安全性服务,有两个重要的概念需要理解,一是认证,即判断用户是否在应用程序中存在,二是授权,即判断用户是否有权限访问资源。
?
Spring security为我们提供的HTTP Basic,Digest等认证服务,因为要在配置文件中对用户和权限进行配置,所以局限性很强。除此之外,它还允许我们在数据库中对用户和权限进行配置。那么如何实现呢?
?
在系统中定义用户,角色,权限这三种实体,一个用户可以拥有多个角色,一个角色可以被多个用户拥有,所以用户与角色之间是多对多的关系,为了易于理解,这里加入第三种实体权限,作为用户和角色的中间关联实体,把用户与角色间的多对多关系拆为两个一对多的关联关系。这样一个用户就对应着多个权限,一个权限对应着一个用户,而一个角色对应着多个权限,一个权限对应着一个角色。
?
一。User.java
?
package com.template.security; import java.util.List; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-7-28 * Time: 下午9:46 * To change this template use File | Settings | File Templates. */ public class User { private Integer id; private String username; private String password; private Boolean enabled; private List<Authority> authorities; public User() { } }
?
?
二。User.hbm.xml
?
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-access="field"> <class name="com.template.security.User" table="user" dynamic-insert="true" dynamic-update="true"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="username" column="username" type="java.lang.String"/> <property name="password" column="password" type="java.lang.String"/> <property name="enabled" column="enabled" type="java.lang.Boolean"/> <bag name="authorities"> <key column="userid"/> <one-to-many class="com.template.security.Authority"/> </bag> </class> </hibernate-mapping>
?
?
三。Role.java
?
package com.template.security; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-7-28 * Time: 下午9:47 * To change this template use File | Settings | File Templates. */ public class Role { private Integer id; private String name; public Role() { } }
?
?
四。Role.hbm.xml
?
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-access="field"> <class name="com.template.security.Role" table="role" dynamic-insert="true" dynamic-update="true"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id> <property name="name" column="name" type="java.lang.String"/> </class> </hibernate-mapping>
?
?
五。Authority.java
?
package com.template.security; /** * Created by IntelliJ IDEA. * User: Zhong Gang * Date: 11-7-28 * Time: 下午9:47 * To change this template use File | Settings | File Templates. */ public class Authority { private Integer id; private User user; private Role role; public Authority() { } }
?
?
六。Authority.hbm.xml
?
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-access="field"> <class name="com.template.security.Authority" table="authority" dynamic-insert="true" dynamic-update="true"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"/> </id>