刚刚配置成功就来写总结啦~(≧▽≦)/~
这一篇不是不同于之前两篇,之前两篇是一边配置一边写,所以按照步骤来肯定是没有问题的。这一篇是我配置成功后来写总结的,可能有纰漏。如果按照步骤配置不成功,请在下面留言说明一下问题,我看到了会立刻解答并修改博客。
项目结构
搭建步骤
添加依赖
1 | <dependency> |
数据库属性配置
我连接的是mysql,从上面的依赖就看出来了。dataSource
配置使用的是阿里巴巴的druid连接池。我把数据库配置放在了jdbc.properties
中。1
2
3
4jdbc_dirverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8
jdbc_username=your_dbadmin
jdbc_password=your_password
dataSource数据源配置
在conf
目录下新建spring.xml,文件内容如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
">
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:conf/jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName">
<value>${jdbc_dirverClassName}</value>
</property>
<property name="url">
<value>${jdbc_url}</value>
</property>
<property name="username">
<value>${jdbc_username}</value>
</property>
<property name="password">
<value>${jdbc_password}</value>
</property>
<!-- 连接池最大使用连接数 -->
<property name="maxActive">
<value>20</value>
</property>
<!-- 初始化连接大小 -->
<property name="initialSize">
<value>1</value>
</property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait">
<value>60000</value>
</property>
<!-- 连接池最大空间 -->
<property name="maxIdle">
<value>20</value>
</property>
<!-- 连接池最小空间 -->
<property name="minIdle">
<value>3</value>
</property>
<!-- 自动清除无用连接 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<!-- 清除无用连接的等待时间 -->
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<!-- 连接属性 -->
<property name="connectionProperties">
<value>clientEncoding=UTF-8</value>
</property>
</bean>
</beans>
更新web.xml
更新web.xml的context-param元素,
在param-value
元素值后面添加;classpath:conf/spring.xml
,
结果如下:1
2
3
4<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-security.xml;classpath:conf/springmvc-servlet.xml;classpath:conf/spring.xml</param-value>
</context-param>
更新spring-security.xml:
1 | <authentication-manager> |
说明:
- user-service元素Spring Security底层使用的UserDetailsService是InMemoryDaoImpl。InMemoryDaoImpl主要是测试用的,其只是简单地将用户信息保存在内存中。要从数据库加载UserDetails,则要使用JdbcDaoImpl。而jdbc-user-service元素Spring Security底层使用的UserDetailsService正是JdbcDaoImpl。
- 我使用的是默认的SQL语句进行查询的,所以jdbc-user-service元素的users-by-username-query,authorities-by-username-query,group-authorities-by-username-query3个属性可以不指定。这3个属性的属性值是SQL语句。看到这里,你也应该知道怎么连接自己的数据表,没错,指定这3个属性即可。这3个属性的作用分别是:
users-by-username-query
通过用户名查询用户信息authorities-by-username-query
通过用户名查询用户权限group-authorities-by-username-query
通过用户名查询用户组权限
- JdbcDaoImpl使用enableAuthorities和enableGroups两个属性来控制权限的启用。默认启用的是enableAuthorities,即用户权限,而enableGroups默认是不启用的。如果要启用用户组权限,需要指定enableGroups属性值为true。当然这两种权限是可以同时启用的。需要注意的是使用jdbc-user-service定义的UserDetailsService是不支持用户组权限的,如果需要支持用户组权限的话需要使用JdbcDaoImpl。
启用用户组
1 | <authentication-manager alias="authenticationManager"> |
小结
引入命名空间,若等于号左侧形如“xmlns:xxxxxx”,那么引用命名空间内的元素要加前缀“xxxxxx:”。
下一篇将对spring-security.xml配置文件进行更详细的配置与说明。