spring security2中对method进行拦截的配置
2009-07-28 10:19
344 查看
1、配置web.xml文件:
2、配置spring-security.xml(该文件名字与web.xml中的<context-param />中的相对应) :
method拦截有三种配置方式:
第一种方式是在java代码添加注释@Secured({"ROLE_SUPER"})、在spring-security.xml中添加<global-method-security secured-annotations="enabled"></global-method-security>,具体配置如下:
spring-security.xml
@Secured({"ROLE_SUPER"})注释中Secured是注释的名称,括号中是可以访问该方法的角色,用大括号括起来,多个角色用分号分割。可以加在service中也可以加在dao中,可以加在接口中也可以加在接口的实现中,此处是加在service接口的实现中。
CategoryServiceImpl.java
第二种方法是不需要在代码中添加注释,只需在spring-security.xml中添加pointcut,利用表达式和通配符指定要拦截的类及方法:
spring-security.xml
第三种方法是直接在要拦截的bean定义中添加<intercept-methods />对指定方法进行拦截,可以使用通配符:
注意:三种方法都需要在spring-security.xml中保留<http auto-config="true">...</http>,否则会出现找不到springSecurityFilterChain的错误。
<!-- 指定spring security的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-security.xml</param-value> </context-param> <!-- spring security 的 Filter Chain 代理 --> <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> <!-- 取得Spring的Context --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
2、配置spring-security.xml(该文件名字与web.xml中的<context-param />中的相对应) :
method拦截有三种配置方式:
第一种方式是在java代码添加注释@Secured({"ROLE_SUPER"})、在spring-security.xml中添加<global-method-security secured-annotations="enabled"></global-method-security>,具体配置如下:
spring-security.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <global-method-security secured-annotations="enabled"></global-method-security> <http auto-config="true"> <!-- 登录页面不拦截,任何人都可以访问到 --> <intercept-url pattern="/login.jsp" filters="none" /> <!-- 需要拦截的路径,pattern表示要拦截的路径,access表示能够访问的角色 --> <intercept-url pattern="/a/*" access="ROLE_A,ROLE_ADMIN,ROLE_SUPER" /> <intercept-url pattern="/a/aa/*" access="ROLE_AA,ROLE_ADMIN,ROLE_SUPER" /> <intercept-url pattern="/b/*" access="ROLE_B,ROLE_ADMIN,ROLE_SUPER" /> <intercept-url pattern="/b/bb/*" access="ROLE_BB,ROLE_ADMIN,ROLE_SUPER" /> <!-- 指定登录页面 --> <form-login login-page="/login.jsp" /> <!-- 指定退出后要显示的页面 --> <logout logout-success-url="/index.jsp"></logout> <!-- 同步session控制 --> <concurrent-session-control max-sessions="1" /> </http> <!-- 认证提供器 --> <authentication-provider> <password-encoder hash="plaintext"></password-encoder> <user-service> <user password="super" name="super" authorities="ROLE_SUPER" /> <user password="admin" name="admin" authorities="ROLE_ADMIN" /> <user password="a" name="a" authorities="ROLE_A" /> <user password="b" name="b" authorities="ROLE_B" /> <user password="aa" name="aa" authorities="ROLE_AA" /> <user password="bb" name="bb" authorities="ROLE_BB" /> </user-service> </authentication-provider> </beans:beans>
@Secured({"ROLE_SUPER"})注释中Secured是注释的名称,括号中是可以访问该方法的角色,用大括号括起来,多个角色用分号分割。可以加在service中也可以加在dao中,可以加在接口中也可以加在接口的实现中,此处是加在service接口的实现中。
CategoryServiceImpl.java
package com.king.springsecurity.service.impl; import java.util.List; import org.springframework.security.annotation.Secured; import com.king.springsecurity.dao.ICategoryDao; import com.king.springsecurity.dao.impl.CategoryDaoImpl; import com.king.springsecurity.model.Category; import com.king.springsecurity.service.ICategoryService; public class CategoryServiceImpl implements ICategoryService { private ICategoryDao categoryDao = new CategoryDaoImpl(); @Override public void addCategory(Category category) { // TODO Auto-generated method stub categoryDao.saveCateogry(category); } @Override @Secured({"ROLE_SUPER"}) public void deleteCategoryById(long id) { // TODO Auto-generated method stub categoryDao.deleteCategoryById(id); } @Override public Category getCategoryById(long id) { // TODO Auto-generated method stub return categoryDao.getCategoryById(id); } @Override @Secured({"ROLE_ADMIN"}) public List<Category> listCategory() { // TODO Auto-generated method stub return categoryDao.getAllCategory(); } public void setCategoryDao(ICategoryDao categoryDao) { this.categoryDao = categoryDao; } }
第二种方法是不需要在代码中添加注释,只需在spring-security.xml中添加pointcut,利用表达式和通配符指定要拦截的类及方法:
spring-security.xml
<?xml version="1.0" encoding="UTF-8"?> <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"> <global-method-security secured-annotations="enabled"> <!-- 利用pointcut使用表达式的方式指定拦截的类及拦截的方法,可以使用通配符。 --> <protect-pointcut expression="execution(* com.king.springsecurity.service.impl.CategoryServiceImpl.delete*(..))" access="ROLE_SUPER" /> <protect-pointcut expression="execution(* com.king.springsecurity.service.ICategoryService.list*(..))" access="ROLE_ADMIN" /> ...... </global-method-security> <http auto-config="true"> <!-- 登录页面不拦截,任何人都可以访问到 --> <intercept-url pattern="/login.jsp" filters="none" /> <!-- 需要拦截的路径,pattern表示要拦截的路径,access表示能够访问的角色 --> <intercept-url pattern="/a/*" access="ROLE_A,ROLE_ADMIN,ROLE_SUPER" /> <intercept-url pattern="/a/aa/*" access="ROLE_AA,ROLE_ADMIN,ROLE_SUPER" /> <intercept-url pattern="/b/*" access="ROLE_B,ROLE_ADMIN,ROLE_SUPER" /> <intercept-url pattern="/b/bb/*" access="ROLE_BB,ROLE_ADMIN,ROLE_SUPER" /> <!-- 指定登录页面 --> <form-login login-page="/login.jsp" /> <!-- 指定退出后要显示的页面 --> <logout logout-success-url="/index.jsp"></logout> <!-- 同步session控制 --> <concurrent-session-control max-sessions="1" /> </http> <!-- 认证提供器 --> <authentication-provider> <password-encoder hash="plaintext"></password-encoder> <user-service> <user password="super" name="super" authorities="ROLE_SUPER" /> <user password="admin" name="admin" authorities="ROLE_ADMIN" /> <user password="a" name="a" authorities="ROLE_A" /> <user password="b" name="b" authorities="ROLE_B" /> <user password="aa" name="aa" authorities="ROLE_AA" /> <user password="bb" name="bb" authorities="ROLE_BB" /> </user-service> </authentication-provider> </beans:beans>
第三种方法是直接在要拦截的bean定义中添加<intercept-methods />对指定方法进行拦截,可以使用通配符:
<?xml version="1.0" encoding="UTF-8"?> <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"> <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <beans:property name="driverClassName"> <beans:value>com.mysql.jdbc.Driver</beans:value> </beans:property> <beans:property name="url"> <beans:value> jdbc:mysql://localhost:3306/springsecurity </beans:value> </beans:property> <beans:property name="username"> <beans:value>root</beans:value> </beans:property> <beans:property name="password"> <beans:value>root</beans:value> </beans:property> </beans:bean> <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy"> <beans:property name="dataSource"> <beans:ref bean="dataSource" /> </beans:property> <!-- 一种方法是通过mappingResources直接指定hbm文件 --> <!-- <beans:property name="mappingResources"> <beans:list> <beans:value> com/king/springsecurity/model/Category.hbm.xml </beans:value> </beans:list> </beans:property> --> <!-- 另一种方法是通过mappingDirectoryLocations指定某一文件夹下的所以hbm文件 --> <beans:property name="mappingDirectoryLocations"> <beans:list> <beans:value> classpath:com/king/springsecurity/model </beans:value> </beans:list> </beans:property> <beans:property name="hibernateProperties"> <beans:props> <beans:prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </beans:prop> </beans:props> </beans:property> </beans:bean> <beans:bean id="categoryDao" class="com.king.springsecurity.dao.impl.CategoryDaoImpl"> <beans:property name="sessionFactory"> <beans:ref bean="sessionFactory" /> </beans:property> </beans:bean> <beans:bean id="categoryService" class="com.king.springsecurity.service.impl.CategoryServiceImpl"> <intercept-methods> <protect method="delete*" access="ROLE_USER" /> <protect method="list*" access="ROLE_ADMIN" /> </intercept-methods> <beans:property name="categoryDao"> <beans:ref bean="categoryDao" /> </beans:property> </beans:bean> <beans:bean name="/category" class="com.king.springsecurity.action.CategoryAction"> <beans:property name="categoryService"> <beans:ref bean="categoryService" /> </beans:property> </beans:bean> <http auto-config="true"> <!-- 登录页面不拦截,任何人都可以访问到 --> <intercept-url pattern="/login.jsp" filters="none" /> <!-- 需要拦截的路径,pattern表示要拦截的路径,access表示能够访问的角色 --> <!-- <intercept-url pattern="/**" access="ROLE_USER" />--> <intercept-url pattern="/a/*" access="ROLE_A" /> <intercept-url pattern="/a/aa/*" access="ROLE_AA" /> <intercept-url pattern="/b/*" access="ROLE_B" /> <intercept-url pattern="/b/bb/*" access="ROLE_BB" /> <!-- 指定登录页面 --> <form-login login-page="/login.jsp" /> <!-- 指定退出后要显示的页面 --> <logout logout-success-url="/index.jsp"></logout> <!-- 同步session控制 --> <concurrent-session-control max-sessions="1" /> </http> <!-- 认证提供器 --> <authentication-provider> <password-encoder hash="plaintext"></password-encoder> <user-service> <user password="serviceimpl" name="serviceimpl" authorities="ROLE_SERVICEIMPL" /> <user password="service" name="service" authorities="ROLE_SERVICE" /> <user password="daoimpl" name="daoimpl" authorities="ROLE_DAOIMPL" /> <user password="dao" name="dao" authorities="ROLE_DAO" /> <user password="super" name="super" authorities="ROLE_SUPER,ROLE_ADMIN,ROLE_USER,ROLE_A,ROLE_B,ROLE_AA,ROLE_BB" /> <user password="admin" name="admin" authorities="ROLE_ADMIN,ROLE_USER,ROLE_A,ROLE_B,ROLE_AA,ROLE_BB" /> <user password="user" name="user" authorities="ROLE_USER,ROLE_A,ROLE_B,ROLE_AA,ROLE_BB" /> <user password="a" name="a" authorities="ROLE_A" /> <user password="b" name="b" authorities="ROLE_B" /> <user password="aa" name="aa" authorities="ROLE_AA" /> <user password="bb" name="bb" authorities="ROLE_BB" /> </user-service> </authentication-provider> </beans:beans>
注意:三种方法都需要在spring-security.xml中保留<http auto-config="true">...</http>,否则会出现找不到springSecurityFilterChain的错误。
相关文章推荐
- spring security2中对url进行拦截的配置
- spring mvc 拦截器的配置 对某个url进行拦截
- 有个奇怪的问题,配置成/system/index,jsp页面时没有经过过滤器进行拦截,而配置成redirectAction时是可以直接跳转刀片loginJsp.action
- struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证
- Spring mvc + Spring Security 拦截 Restful格式的URL并进行验证,支持Remember me token数据库存储
- Spring Security配置命名空间 - global-method-security
- 【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut
- Bean第一次从容器获取的时候,如果bean配置了lookup-method,那么就使用了cglib来进行调用方法转换
- web.xml配置监听器来进行shiro拦截
- 恶补使用CVS进行配置管理
- 如何通过WiFi进行简单的配置
- 常用的一些log4j的配置的属性进行说明
- 配置错误:未能使用提供程序“RsaProtectedConfigurationProvider”进行解密。提供程序返回错误信息为: 打不开 RSA 密钥容器。
- 了解一下 Redis 并在 CentOS 下进行安装配置
- 从配置文件看Nagios通过NRPE, SNMP进行监控的工作原理
- Sonar6.0应用之二:Sonar Web界面配置及与Runner、Scanner集成进行命令行代码分析
- phpstorm配置Xdebug进行调试PHP教程
- spring配置文件中 destroy-method="close"的作用
- asp.net 4.0 尚未在服务商注册 您需要手动将web服务器配置为 ASP.NET4.0,这样您的网站才能正常进行。
- 在 Windows 下配置 Vim 使用 Ack 进行快速查找