写点什么

如何在 Java Web 应用程序中启用 Spring Security?

  • 2019-07-08
  • 本文字数:2982 字

    阅读完需:约 10 分钟

如何在Java Web应用程序中启用Spring Security?

Spring Security 是通过声明方式在 Java Web 应用程序中实现安全性的流行开源框架之一。 它提供了基本的安全功能,如 LDAP 身份验证,授权,基于角色的访问控制,记住密码,URL 保护,并发活动 Session 管理等。为了在 Java Web 应用程序中启用 Spring Security,需要配置如下三项内容 :


  • 在 web.xml 中声明委托代理过滤器(delegating proxy filter )

  • 在 web.xml 中添加 ContextLoaderListener

  • 在 applicationContext-Security.xml 文件上提供实际的安全性约束。


由于 Spring Security 使用一系列过滤器来实现各种安全约束,也称为 Spring 的“安全链过滤器”,它依赖于通过 Java Web 容器来初始化过滤器的代理。


如果你还记得,过滤器是由像 Tomcat 或 Jetty 这样的 Servlet 容器创建、维护和销毁。 通过在 web.xml 中声明过滤器,并且 Web 容器通过调用过滤器的 init(FilterConfig config)方法来进行初始化。 然后,此过滤器将预处理和后处理任务委托给 Spring Security 框架的过滤器实现。


每当请求或响应通过匹配过滤器的 URL 模式,Servlet 容器就会调用 DelegatingFilterProxy 的 doFilter()方法来进行处理。


此方法可以访问 ServletRequest,ServletResponse 和 FilterChain 对象,这意味着它可以在将请求发送到 Servlet 或响应客户端之前修改请求标头,响应标头和响应正文。 FilterChain 对象用于将请求路由中转到过滤链中的另一个过滤器进一步处理。


顺便说一下,在深入 Spring Security 之前了解 Spring 框架很重要,如果你觉得自己对 Spring 框架知之甚少,那么我建议你先学习一下像 Spring Framework 5 这样的综合课程:初学者到Guru


在 Web 应用程序中启用 Spring Security 的步骤。


以下是在 Java Web 应用程序中启用 Spring Security 功能的三个步骤:


1、首先在 web.xml 中声明 DelegatingFilterProxy 过滤器;


2、接着,将 Spring 应用程序上下文文件指定给 ContextLoaderListener;


3、最后,在 applicationContext-Security.xml 文件中指定 Spring Security 模板匹配 URL。


在 web.xml 中使用 DelegatingFilterProxy 过滤器声明如下:


<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>

复制代码


Servlet 容器将获取此过滤器并进行初始化,并在发出请求时调用 DelegatingSpringSecurity 的 doFilter()方法。 通过调用 init()方法初始化过滤器。 DelegatingFilterProxy 扩展了 GenricFilterBean,它实现了 init(FilterConfig filterConfig)方法,并回调 initFilterBean()以允许子类执行初始化。


默认情况下,DelegatingFilterProxy 类将在初始化期间查找 Spring 应用程序上下文 ,如下面 Spring 安全 JAR 文件中 DelegatingFilterProxy 类的代码所示。


 protected void initFilterBean() throws ServletException {   synchronized (this.delegateMonitor)  { if (this.delegate == null) { // If no target bean name specified, use filter name.   if (this.targetBeanName == null)  {  this.targetBeanName = getFilterName(); } // Fetch Spring root application context and initialize the  // delegate early, if possible.  // If the root application context will be started  // after this filter proxy, we'll have to resort to lazy  // initialization.   WebApplicationContext wac = findWebApplicationContext();   if (wac != null)  { this.delegate = initDelegate(wac); } }   } 
}

复制代码


如果要自定义此行为,可以通过 FilterConfig 对象使用 targetBeanName 提供 bean 类的名称。 如果没有,那么这将使用 filter-name,即“springSecurityFilterChain”。 然后,它在 applicationContext.xml 和 applicationContext-Security.xml 文件中搜索此 bean 以进行初始化。


但是,当您在配置文件中搜索此内容时,很可能由于而无法找到它,因为使用默认值会隐藏了复杂性。 您可以进一步加入学习Spring Security 4 基础实践,以了解有关如何在 Spring 安全性中委派过滤器代理以及如何自定义和配置其行为的更多信息。 

 以下是在 Java Web 应用程序中加载 Spring 安全性配置文件的方法:


<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> 
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml /WEB-INF/applicationContext-security.xml </param-value> </context-param>

复制代码


应该把配置文件 applicationContext-security.xml 放在 WEB-INF 目录中。


以下是 Spring 安全配置文件的示例:


<?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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 
<security:http auto-config="true">
<security:intercept-url pattern="/admin" access="ROLE_ADMIN" /> </security:http> <security:authentication-manager> <security:authentication-provider>
<security:user-service> <security:user authorities="ROLE_ADMIN" name="admin" password="admin" /> <security:user authorities="ROLE_ADMIN" name="root" password="root" /> </security:user-service> </security:authentication-provider> </security:authentication-manager> </beans>
复制代码


通过这种简单的配置,现在可以保护所有以 /admin 结尾的 URL 只能使用用户名和密码进行访问。 有权访问此权限的两个用户 admin 和 root。 简而言之,您不需要对任何 Spring 安全过滤器或 bean 进行编码,所有这些都是通过使用现有的 Spring 安全过滤器和标记来完成的。


顺便说一句,如果你想深入理解这个配置,比如每个标签的含义以及它们与 Java 类的关系,我建议你看看 Eugen 的Spring Security Master课,它针对 Spring Security 5 进行了更新。


现在,如果您点击 URL 登录 admin,将要求您输入用户名和密码,并且只有在正确的凭据之后才允许您访问受保护的部分。


在模式的顺序声明此非常重要,它们必须按最具体到最不具体的顺序声明,因为这些模式按照它们在 spring 安全应用程序上下文文件中声明的顺序进行计算 找到匹配项,不执行其他模式。


这就是如何在 Java Web 应用程序中启用或激活 Spring Security。 虽然配置和使用非常简单,但功能非常强大,为您提供了许多实现复杂安全约束的可选项。 我们没有编写任何代码,我们只是进行了一些 XML 配置,以便将不安全的 Web 应用程序设置为安全的,很神奇。


原文链接:


https://javarevisited.blogspot.com/2017/05/how-to-enable-spring-security-in-java-web-application.html


2019-07-08 10:013900
用户头像
王文刚 Instagram 营销专家

发布了 37 篇内容, 共 24.5 次阅读, 收获喜欢 55 次。

关注

评论

发布
暂无评论
发现更多内容

15款国产大模型透明度测评,腾讯混元评分稳居榜首

Geek_2d6073

如何在中心化交易所上币?

区块链项目一站式包装孵化

AI智能体的开发框架

北京木奇移动技术有限公司

AI教育 AI智能体 软件外包公司

WebGL 开发医疗模型的流程

北京木奇移动技术有限公司

数字孪生 软件外包公司 webgl开发

网站IPv6网站支持度是什么?IPv6检测指标有哪些?

国科云

绝不能错失的MES攻略,攻克生产难关!

积木链小链

制造业 mes

What Makes QCN9074 Stand Out in the WiFi 6E Landscape?

wallyslilly

Logic Pro X for mac(mac苹果版音乐创作软件)v 11.1.1 直装免激活版

小玖_苹果Mac软件

阿里P8面试官不小心泄露了25年春招进大厂必备的Java面试场景题!

程序员高级码农

Java 编程 程序员 java面试 Java面试题

3分钟完成产品网页搭建

cloud studio AI应用

webgl开发3D模型的优化

北京木奇移动技术有限公司

数字孪生 软件外包公司 webgl开发

Smooze Pro for Mac(鼠标增强工具专业版)v2.1.20激活版

小玖_苹果Mac软件

Illustrator 2024 for Mac(AI2024领先的矢量图形软件)v28.6.0中文激活版

小玖_苹果Mac软件

中国人工智能大赛成果发布会 | 代码安全智能体让研发安全又高效

百度安全

智源线虫登上Nature子刊封面,具身元年结尾意味深长

智源研究院

AI 助力游戏开发中的常用算法实现

cloud studio AI应用

Project Office X Pro for Mac(项目管理软件)v1.1.11中文激活版

小玖_苹果Mac软件

淘宝商品详情数据的获取渠道与稳定API的保障

代码忍者

淘宝API接口 淘宝评论API

阿里P8面试官重磅首发2025年Java岗春招面试总结!

程序员高级码农

Java 后端 java程序员 java面试 Java面试题

文档解析技术发展回顾与路径思考

合合技术团队

人工智能 科技 AIGC 文档解析

提升测试效率:2024 年值得关注的 UI 自动化测试工具推荐

测试人

软件测试

Desktop Goose for Mac(抖音桌面宠物鹅)v0.22激活版

小玖_苹果Mac软件

Beyond Compare 4 for Mac(文件同步对比软件)v4.3.7(25118)中文修复版

小玖_苹果Mac软件

全球人形机器人领域深度调研和行业前瞻2025

机器人头条

大模型 人形机器人 具身智能

夜莺监控 v8 第一个版本来了,开始做有意思的功能了

巴辉特

夜莺监控 开源监控

全闪时代已至,您在担心 SSD 寿命吗?

XSKY星辰天合

#全闪存储

MacCleanse for Mac(mac系统清理垃圾软件)v13.0.1激活版

小玖_苹果Mac软件

KaiwuDB 与开放原子开源基金会完成 KWDB 项目捐赠签约

KaiwuDB

数据库

ACDSee Photo Studio for Mac(老牌数字图像处理软件)v9.3.2859激活版

小玖_苹果Mac软件

Smooze Pro for Mac(鼠标增强工具专业版)v2.1.20激活版

小玖_苹果Mac软件

如何在Java Web应用程序中启用Spring Security?_文化 & 方法_javin paul_InfoQ精选文章