Spring安全权限管理(Spring Security)

Spring Security以前叫做acegi,是后来才成为Spring的一个子项目,也是目前最为流行的一个安全权限管理框架,它与Spring紧密结合在一起。

Spring Security关注的重点是在企业应用安全层为您提供服务,你将发现业务问题领域存在着各式各样的需求。银行系统跟电子商务应用就有很大的不同。电子商 务系统与企业销售自动化工具又有很大不同。这些客户化需求让应用安全显得有趣,富有挑战性而且物有所值。Spring Security为基于J2EE的企业应用软件提供了一套全面的安全解决方案。

2.为Spring Security配置过滤器和其他参数

要使用Spring Security,首先就是在web.xml中为它配置过滤器, 其次因为我的spring配置文件是放在WEB-INF下的,因此还要配置上下文的参数,最后添加spring的监听器:

  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < web-app   version = "2.5"   xmlns = "http://java.sun.com/xml/ns/javaee"   
  3.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >   
  5.     <!-- 配置上下文参数,指定spring配置文件的位置 -->   
  6.     < context-param >   
  7.         < param-name > contextConfigLocation </ param-name >   
  8.         < param-value > /WEB-INF/spring-*.xml </ param-value >   
  9.     </ context-param >   
  10.     <!-- spring security必须的过滤器,保证在访问所有的页面时都必须通过认证 -->   
  11.     < filter >   
  12.         < filter-name > springSecurityFilterChain </ filter-name >   
  13.         < filter-class >   
  14.             org.springframework.web.filter.DelegatingFilterProxy  
  15.         </ filter-class >   
  16.     </ filter >   
  17.     < filter-mapping >   
  18.         < filter-name > springSecurityFilterChain </ filter-name >   
  19.         < url-pattern > /* </ url-pattern >   
  20.     </ filter-mapping >   
  21.     < listener >   
  22.         < listener-class >   
  23.             org.springframework.web.context.ContextLoaderListener  
  24.         </ listener-class >   
  25.     </ listener >   
  26.     < welcome-file-list >   
  27.         < welcome-file > index.jsp </ welcome-file >   
  28.     </ welcome-file-list >   
  29.     < login-config >   
  30.         < auth-method > BASIC </ auth-method >   
  31.     </ login-config >   
  32. </ web-app >   

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置上下文参数,指定spring配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-*.xml</param-value> </context-param> <!-- spring security必须的过滤器,保证在访问所有的页面时都必须通过认证 --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>

3.配置security(spring-security.xml)

  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. <!-- 这里必须使用security的命名空间,提供了beans这个假名 -->   
  3. < beans:beans   xmlns = "http://www.springframework.org/schema/security"   
  4.     xmlns:beans = "http://www.springframework.org/schema/beans"   
  5.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  6.     xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.                         http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">   
  8.   
  9.     <!-- Spring Security 采用就近原则,有多个约束时,从上至下只要找到第一条满足就返回,因此因该将最严格的约束放在最前面,而将最宽松的约束放在最后面.auto- config属性可以让spring security为我们自动配置几种常用的权限控制机制,包括 form,anonymous, rememberMe等。当然你也可以手工配置。-->   
  10.     < http   auto-config = "true" >   
  11.         <!-- 我 们利用intercept-url来判断用户需要具有何种权限才能访问对应的url资源,可以在pattern中指定一个特定的url资源,也可以使用通 配符指定一组类似的url资源。例子中定义的两个intercepter-url,第一个用来控制对/security/**的访问,第二个使用了通配符 /**,说明它将控制对系统中所有url资源的访问。 -->   
  12.         < intercept-url   pattern = "/security/**"   access = "ROLE_ADMIN"   />   
  13.         < intercept-url   pattern = "/**"   access = "ROLE_ADMIN,ROLE_USER"   />   
  14.         < intercept-url   pattern = "/login.jsp*"   filters = "none"   />   
  15.         < logout   logout-url = "/logout.jsp"   
  16.             logout-success-url = "/j_spring_security_check"   />   
  17.     </ http >   
  18.   
  19.     <!-- 使用内存权限管理的配置信息, 在tomcat启动时,会加载这个文件并一直保存在内存中,知道应用程序重启,所以也叫内存权限管理  
  20.         < authentication-provider >   
  21.         < user-service >   
  22.         < user   name = "admin"   password = "tomcat"   authorities = "ROLE_ADMIN" />   
  23.         < user   name = "liky"   password = "redhat"   authorities = "ROLE_USER" />        
  24.         </ user-service >   
  25.         </ authentication-provider >   
  26.     -->   
  27.     <!-- 使用数据库作为权限管理的来源,data-source-ref指定了数据源,所指定的数据源必须包含users, authorities表,并符合security的定义规范 -->   
  28.     < authentication-provider >   
  29.         < jdbc-user-service   data-source-ref = "dataSource"   />   
  30.     </ authentication-provider >   
  31.   
  32. </ beans:beans >   

<?xml version="1.0" encoding="UTF-8"?> <!-- 这里必须使用security的命名空间,提供了beans这个假名 --> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="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-2.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"> <!-- Spring Security采用就近原则,有多个约束时,从上至下只要找到第一条满足就返回,因此因该将最严格的约束放在最前面,而将最宽松的约束放在最后 面.auto-config属性可以让spring security为我们自动配置几种常用的权限控制机制,包括form,anonymous, rememberMe等。当然你也可以手工配置。--> <http auto-config="true"> <!-- 我们利用intercept-url来判断用户需要具有何种权限才能访问对应的url资源,可以在pattern中指定一个特定的url资源,也可以使用 通配符指定一组类似的url资源。例子中定义的两个intercepter-url,第一个用来控制对/security/**的访问,第二个使用了通配 符/**,说明它将控制对系统中所有url资源的访问。 --> <intercept-url pattern="/security/**" access="ROLE_ADMIN" /> <intercept-url pattern="/**" access="ROLE_ADMIN,ROLE_USER" /> <intercept-url pattern="/login.jsp*" filters="none" /> <logout logout-url="/logout.jsp" logout-success-url="/j_spring_security_check" /> </http> <!-- 使用内存权限管理的配置信息, 在tomcat启动时,会加载这个文件并一直保存在内存中,知道应用程序重启,所以也叫内存权限管理 <authentication-provider> <user-service> <user name="admin" password="tomcat" authorities="ROLE_ADMIN"/> <user name="liky" password="redhat" authorities="ROLE_USER"/> </user-service> </authentication-provider> --> <!-- 使用数据库作为权限管理的来源,data-source-ref指定了数据源,所指定的数据源必须包含users, authorities表,并符合security的定义规范 --> <authentication-provider> <jdbc-user-service data-source-ref="dataSource" /> </authentication-provider> </beans:beans>

4.数据源的配置(spring-common.xml)

  1. <?xml version= "1.0"  encoding= "UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >  
  5.   
  6.     <!-- 定义数据源 -->  
  7.     <bean id="dataSource"   
  8.         class = "org.apache.commons.dbcp.BasicDataSource" >  
  9.         <property name="driverClassName"   
  10.             value="com.mysql.jdbc.Driver" >  
  11.         </property>  
  12.         <property name="url"  value= "jdbc:mysql://localhost:3306/csu" ></property>  
  13.         <property name="username"  value= "root" ></property>  
  14.         <property name="password"  value= "redhat" ></property>  
  15.         <property name="maxActive"  value= "100" ></property>  
  16.         <property name="maxIdle"  value= "30" ></property>  
  17.         <property name="maxWait"  value= "300" ></property>  
  18.         <property name="defaultAutoCommit"  value= "true" ></property>  
  19.     </bean>     
  20. </beans>  

<?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-2.5.xsd"> <!-- 定义数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/csu"></property> <property name="username" value="root"></property> <property name="password" value="redhat"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="300"></property> <property name="defaultAutoCommit" value="true"></property> </bean> </beans>

5.项目的目录结构

6. 数据库脚本

  1. /-- 注意这里的脚本是MYSQL的,因此在你演示这个实例的时候,要加入MySQL的驱动包 --/  
  2.    
  3. create table users  
  4. (  
  5. username varchar(50) primary key,  
  6. password varchar(50),  
  7. enabled tinyint(1)  
  8. );  
  9.   
  10. create table authorities  
  11. (  
  12. id int auto_increment primary key,  
  13. username varchar(50),  
  14. authority varchar(50),  
  15. constraint fk_authorities_users foreign key(username) references users(username)  
  16. );  
  17.   
  18. create unique index ix_auth_username on authorities (username,authority);  

/-- 注意这里的脚本是MYSQL的,因此在你演示这个实例的时候,要加入MySQL的驱动包 --/ create table users ( username varchar(50) primary key, password varchar(50), enabled tinyint(1) ); create table authorities ( id int auto_increment primary key, username varchar(50), authority varchar(50), constraint fk_authorities_users foreign key(username) references users(username) ); create unique index ix_auth_username on authorities (username,authority);

7.部署和配置的要点说明

这是一个Spring Security的数据库认证实例,要注意以下几点:
(1)请自行加入Spring必须的包,Spring security的包和MySQL的驱动包,当然你也可以换成其他的数据库,但是你要相应的修改spring-common.xml中的dataSource部分
(2)数据库中的两个表users,authorites必须完全按照脚本所示来定义,也就是说表的名字不能修改.
(3)users表必须包含username,password,enabled字段,这三个字段是绝对不能少的,也不能修改类型.另外enabled一定要为1才能登录
(4)authorities 表必须包含username字段,这个字段引用users的username作为外键,authority字段就是角色的名字,角色名字必须满足 ROLE_XXX的格式(例如:ROLE_ADMIN,ROLE_USER,ROLE_MAMAGER)
(5)如果一个用户有多个角色,不要将多 个角色放在一起用逗号隔开.而是每个角色定义一条记录(例如:abu有ROLE_ADMIN,ROLE_USER两个角色,那么应该定义两条记录: 一条为abu, ROLE_USER,另一条为abu, ROLE_ADMIN.而不是只有一条:abu, ROLE_ADMIN,ROLE_USER)
(6)你可以给authorities表添加一个id字段作为主键.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值