Spring Security学习
来源:互联网 发布:十一选五最大遗漏数据 编辑:程序博客网 时间:2024/06/05 18:00
最近在自学Spring Security,期间不断google和参考别人的例子,经验啊,什么的,感谢他们的share。
在自己的摸索中,有一些小的tips,在此记录下来,以备日后参考。先上代码,逐一列举。
<?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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> ..... </beans:bean> <http auto-config="true" access-denied-page="/common/403.jsp" use-expressions="true"> <intercept-url pattern="/home.jsp" filters="none"/> <intercept-url pattern="/index.jsp" access="hasRole('EMPLOYEE')" /> <intercept-url pattern="/login.jsp" filters="none" /> <intercept-url pattern="/common/**" filters="none" /> <intercept-url pattern="/script/**" filters="none" /> <intercept-url pattern="/admin.jsp" access="hasRole('ADMIN')" /> <intercept-url pattern="/user.jsp" access="hasRole('CLIENT')" /> <form-login login-page="/login.jsp" authentication-failure-url="/common/403.jsp" default-target-url="/home.jsp" /> <logout logout-url="/logout" logout-success-url="/home.jsp"/> </http> <authentication-manager> <authentication-provider> <jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,pazzword,enabled from security_user where username = ? and enabled = 1" authorities-by-username-query="select u.username, r.rolename from security_user u join security_map ur on u.userid = ur.userid join security_user_group r on r.roleid = ur.roleid where u.username = ?" /> </authentication-provider> </authentication-manager> </beans:beans>由于我们公司做的项目很多是基于数据库表的用户权限验证,所以特意学习jdbc-user-service部分。
- 首先,要定义好dataSource,其包含了jdbc-user-service中的sql语句涉及到的表。
- 标签<http>中的user-expressions的默认值是false, 这里我将其设置为true, 这样设置后就可以使用Expression了,比如在java method上面,我可以使用annotation写:@PreAuthorize("hasRole('CLIENT')"), 在JSP页面,我们可以写:<sec:authorize access="hasRole('ADMIN')">, 不然我们就比较麻烦,在applicationContext.xml中,我们只能使用list 的方式写出access的权限:access="ADMIN, CLIENT"。
- intercept-url 中,access 里面的角色比如ADMIN, CLIENT,其大小写必须和数据库里面的保持一致,也就是大小写敏感的。
- 在jdbc-user-service中,两个sql 语句里面的select 子句都是必不可少的,如果移去了任意username,都导致验证失败(没研究源码,猜测两者通过username做map的)
- 如果不设置<http>中的use-expressions (默认是false), 那么access 的值就必须是以 ROLE_ 打头的角色名称, 否则会报错,但是数据库保存的值往往是大写的如ADMIN, CLIENT, EMPLOYEE, 那么就需要设置<jdbc-user-service>里面的 role-prefix 的值,而且是 ROLE_ 。如下:
<http auto-config="true" access-denied-page="/common/403.jsp"> <intercept-url pattern="/home.jsp" filters="none"/> <intercept-url pattern="/index.jsp" access="ROLE_EMPLOYEE, ROLE_ADMIN" /> <intercept-url pattern="/login.jsp" filters="none" /> <intercept-url pattern="/common/**" filters="none" /> <intercept-url pattern="/script/**" filters="none" /> <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" /> <intercept-url pattern="/user.jsp" access="ROLE_CLIENT" /> </http> <authentication-manager> <authentication-provider> <jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,pazzword,enabled from security_user where username = ? and enabled = 1" authorities-by-username-query="select u.username, r.rolename from security_user u join security_map ur on u.userid = ur.userid join security_user_group r on r.roleid = ur.roleid where u.username = ?" role-prefix="ROLE_"/> </authentication-provider> </authentication-manager>
- 在页面使用security标签的时候,还必须将spring-security-taglibs这个jar 引入近来,否则报错, pom.xml 如下:
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-taglibs</artifactId><version>3.0.0.RELEASE</version></dependency>然后在页面引入taglib,就可以使用了:
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> .....<BR><a href="${pageContext.request.contextPath}/logout">Logout</a><BR><sec:authorize access="hasRole('ADMIN')"> only users who have the "ADMIN" privilege can see this content.</sec:authorize>
- Spring Security学习
- Spring Security学习资料
- spring security学习笔记
- Spring Security学习总结
- Spring Security学习
- Spring Security学习笔记
- spring security学习
- Spring security学习
- spring security学习
- Spring Security学习
- Spring Security学习总结
- spring security 3学习
- Spring Security学习总结
- spring security学习笔记
- Spring Security 4学习
- spring security学习资料
- Spring Security学习
- Spring Security学习
- Vbox Ubuntu 共享windows7 文件的两种方法
- Tomcat 7下SSL配置出现异常: '/root/.keystore' did not find a matching property
- 转jquery 获得窗口宽高
- Android的APK包里的文件类型都是什么?
- LINUX 或 UNIX系统下 后台执行 SQL脚本
- Spring Security学习
- C语言学习笔记(2)形参和实参
- Hello Views之List View(yaozq翻译,仅供参考)
- struts2文件上传与多文件上传
- 未能解析引用的程序集
- Linux系统启动过程分析
- 10 3D对象的衣服(Material)
- mysql 主从同步设置
- 【转】以为存储研发者的图书清单