Spring Security的XML配置是传统Java Web项目中常用的安全框架配置方式,通过XML文件可以集中管理认证、授权、会话管理等安全相关规则,不需要编写大量Java配置类,适配很多基于XML配置的旧项目架构。

前置依赖准备
首先需要在项目的pom.xml中引入Spring Security的相关依赖,确保版本与项目使用的Spring框架版本兼容,以下是常用的依赖配置:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.6.8</version>
</dependency>
基础XML配置文件结构
Spring Security的XML配置需要引入专属的命名空间,通常我们在项目的applicationContext-security.xml文件中编写配置,基础结构如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 安全配置内容写在这里 -->
</beans>
核心配置项说明
1. 开启Security注解支持
如果需要使用@PreAuthorize、@Secured等注解控制方法权限,需要开启注解支持:
<security:global-method-security pre-post-annotations="enabled" secured-annotations="enabled"/>
2. 配置HTTP请求安全规则
通过security:http标签定义请求拦截规则,指定哪些路径需要认证、哪些路径可以直接访问,同时可以配置登录页、登出规则等:
<security:http auto-config="true" use-expressions="true">
<!-- 放行静态资源和登录页 -->
<security:intercept-url pattern="/static/**" access="permitAll()"/>
<security:intercept-url pattern="/login.jsp" access="permitAll()"/>
<!-- 管理员路径需要ROLE_ADMIN权限 -->
<security:intercept-url pattern="/admin/**" access="hasRole('ADMIN')"/>
<!-- 其他所有请求需要认证 -->
<security:intercept-url pattern="/**" access="authenticated()"/>
<!-- 自定义登录页配置 -->
<security:form-login
login-page="/login.jsp"
login-processing-url="/doLogin"
default-target-url="/index.jsp"
authentication-failure-url="/login.jsp?error=1"/>
<!-- 登出配置 -->
<security:logout
logout-url="/doLogout"
logout-success-url="/login.jsp?logout=1"/>
<!-- 关闭跨站请求伪造防护,方便测试,生产环境建议开启 -->
<security:csrf disabled="true"/>
</security:http>
3. 配置认证管理器
通过security:authentication-manager标签定义认证规则,可以配置内存用户、JDBC用户或者自定义用户服务:
内存用户配置示例
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<!-- 定义两个用户,user有ROLE_USER权限,admin有ROLE_USER和ROLE_ADMIN权限 -->
<security:user name="user" password="{noop}123456" authorities="ROLE_USER"/>
<security:user name="admin" password="{noop}123456" authorities="ROLE_USER,ROLE_ADMIN"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
JDBC用户配置示例
如果用户信息存储在数据库中,可以使用JDBC认证方式,需要提前在数据库中创建用户表和权限表:
<security:authentication-manager>
<security:authentication-provider>
<security:jdbc-user-service
data-source-ref="dataSource"
users-by-username-query="select username,password,enabled from sys_user where username=?"
authorities-by-username-query="select u.username,r.role_name from sys_user u left join sys_user_role ur on u.id=ur.user_id left join sys_role r on ur.role_id=r.id where u.username=?"/>
</security:authentication-provider>
</security:authentication-manager>
Web.xml中的配置
完成Spring Security的XML配置后,还需要在web.xml中添加过滤器,让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>
<!-- 加载Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
常见问题说明
- 密码前面添加
{noop}表示使用无加密方式,生产环境建议使用BCryptPasswordEncoder等加密方式,对应密码前缀为{bcrypt} - 使用表达式配置权限时,必须设置
use-expressions="true",否则无法识别hasRole、permitAll等表达式 - 自定义登录页的表单提交路径需要和
login-processing-url配置一致,表单中的用户名和密码参数名默认是username和password,也可以通过username-parameter和password-parameter自定义
Spring_SecurityXML配置Java权限控制安全框架修改时间:2026-06-14 14:12:23