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>






原创粉丝点击