AI实践哪家强?来 AICon, 解锁技术前沿,探寻产业新机! 了解详情
写点什么

Spring Security 4.0: WebSocket、Spring Data 和 Test Support

  • 2015-04-22
  • 本文字数:2219 字

    阅读完需:约 7 分钟

Spring Security 团队发布了 Spring Security 4.0.0 ,不但提供了更多缺少的安全性,还增加了几个新的特性。重要主题包括 WebSocket 安全性、Spring Data 集成、更好的测试支持,并引进了 Apache 许可的开源项目 Spring Session 。Spring Session 作为项目的 HttpSession 提供者,从而简化客户端的开发。这样开发人员就可以从任何环境中访问会话了,它支持集群环境,具有可插拔的 session-id 策略并支持 websockets。

WebSocket 安全性

Spring’s WebSocket 已支持 Spring 的安全性,但尚未针对 JSR-356 (Java API for WebSocket) 提供直接的支持。你可以使用如下 Spring 的 Java Configuration 在 websocket 通道上配置安全性。

复制代码
@Configuration
public class WebSocketSecurityConfig
extends AbstractSecurityWebSocketMessageBrokerConfigurer {
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
messages.simpDestMatchers("/user/*").authenticated();
}
}

Spring Data 集成

现在可以用 SpEL 在 Spring Data 查询语句内获取当前用户了。如何要使用这个特性,你需要定义一个 @Bean。

复制代码
@Bean
public SecurityEvaluationContextExtension securityEvaluationContextExtension(){
return new SecurityEvaluationContextExtension();
}

然后,你就可以在查询语句中引用 Spring Security 的当前用户了。示例如下:

复制代码
public interface BlogRepository extends JpaRepository<blog long=""> {
@Query("select blog from Blog blog where blog.user.login = ?#{principal.username}")
List<blog> findAllForCurrentUser();
}</blog></blog>

提升测试的支持

Spring Security 4.0 增加了许多的注解以简化需要认证的测试方法。例如,如果你有一个方法带有 @PreAuthorize(“已认证”),可以用以下的机制予以测试:

  • @WithMockUser: 把它增加到一个 @Test 方法里,该方法的用户名为“user”,密码为“password”,角色为“ROLE_USER”。你可以在注解中用具体的参数值覆盖这些参数:比如 @WithMockUser(username=“admin”,roles={“USER”,“ADMIN”})
  • @WithUserDetails: 与 @WithMockUser 类似,但是可以自定义认证,减少与 Spring Security 的耦合。
  • @WithSecurityContext: 提供了最大的灵活性,你可以用它创建自己定制的测试注解。

Spring Security 4.0 也可以和 Spring MVC Test (4.1.3+) 一起使用。如下示例演示了集成这两个框架要执行的所有设置。

复制代码
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class CsrfShowcaseTests {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
@Before
public void setup() {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
}

从 ****Spring Security 3.x 4.x**** 的迁移

Spring Security 团队发表了一篇《从 Spring Security 3.x 到 4.x 的迁移指南》。它包括 XML 配置文件和 Java Configuration 的迁移指令。甚至,它还在一份迁移示例中提供了一份差异列表,高亮显示了需要修改的内容:

Spring Security 4.0 Java Configuration

最基本的 Spring Security 是用 Java configuration 创建一个 Servlet Filter,它对所有的安全负责(URL 保护、证书验证、登录重定义等等)。它只有几行代码,而且有一半是类的导入。

复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.security.config.annotation.authentication.builders.*;
import org.springframework.security.config.annotation.web.configuration.*;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}

代码不多,却提供了许多的特性:

  • 应用中的每个 URL 都需要经过认证
  • 为你生成一个登录表单
  • 允许以用户密码的方式进行认证
  • 允许用户登出
  • 预防 CSRF 攻击
  • 会话固定保护
  • 安全标头整合
    • 针对安全请求的 HTTP Strict Transport Security
    • 整合 X-Content-Type-Options
    • 缓存控制
    • 整合 X-XSS-Protection
    • 整合 X-Frame-Options 以协助预防点击劫持
  • 集成 HttpServletRequest API 方法:getRemoteUser()、getUserPrinciple()、isUserInRole(role)、login(username, password) 和 logout()

要在 Spring Boot 1.2 项目中使用这个版本,你需要按如下写法覆盖 Spring Security 版本:

复制代码
<properties>
    <spring-security.version>4.0.0.RELEASE</spring-security.version>
</properties>

要了解更多的 Spring Security 4.0 信息,请查阅 Spring Security 领导 Rob Winch 在 InfoQ 的演讲:《 Spring Security 4.0 从零开始》。点击此处可获得本次演讲的幻灯片。

查看英文原文: Spring Security 4.0: WebSocket, Spring Data and Test Support

2015-04-22 08:547189

评论

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

CAD图纸如何进行版本转换?CAD高低版本转换教程来了

在路上

cad cad看图 cad软件 CAD看图王

让 AI 更懂你的需求!一文看懂如何在 Trae IDE 中巧用上下文

火山引擎开发者社区

AI Trae

京东商品详情数据接口抓取指南

tbapi

京东API接口 京东商品详情接口 京东数据采集

前端热更新:静默引擎驱动中国互联网数字化转型与生态重构

xuyinyin

火山引擎携手荣耀,助力智能终端智慧影像与办公场景全面升级

新消费日报

火山引擎x努比亚:真正“懂你”的手机AI交互体验

新消费日报

TinyPNG and TinyJPG for Mac(ps图像压缩插件) 详细安装教程

Rose

商品中心—商品考核系统的技术文档

电子尖叫食人鱼

架构 教程

MARI指南:研发效能度量的闭环方法论

思码逸研发效能

研发效能 研发效能度量 研发效能管理 思码逸

酷开携手途虎养车落地AI智能体车机,打破后装座舱智能化瓶颈

业界

域名锁定是什么意思?怎么解除域名锁定状态?

国科云

租房不看房!Trae 助我轻松打造租房神器

火山引擎开发者社区

火山引擎 Trae

革新移动办公体验,浩辰CAD看图王适配华为Pura 80

在路上

cad CAD看图王

AE插件中文版 Bodymovin 导出json工具-Mac/win

Rose

在禧玛诺工厂,一位工程师的零代码实践

NocoBase

开源 低代码 数字化转型 零代码 制造业

百度垂搜数据管理系统弹性调度优化实践

百度Geek说

大数据 云原生 搜索 弹性调度

SAM多物理场滤波器CST仿真实例

思茂信息

cst cst使用教程 CST软件 CST Studio Suite

火山引擎携手 vivo:锚定用户场景,加速智能终端 AI 应用体验进阶

新消费日报

专业的科学和工程分析WaveMetrics Igor Pro 9 for Mac安装教程 附激活码

Rose

玩转 MCP 第三弹|一文教会你如何用 Trae 创建你的专属“行程规划助手”

火山引擎开发者社区

Trae

尖叫青蛙Screaming Frog SEO Spider 激活码+安装教程-Mac/win

Rose

华为云亮相 KubeCon China 2025,开源生态引领 AI 时代技术跃迁

华为云原生团队

云计算 容器 云原生

火山引擎×OPPO:促进技术与场景深度结合,助力智能终端加速AI进化

新消费日报

火山引擎携手三星,以AI构建智能终端视觉生产新生态

新消费日报

AutoCAD 2024 Mac 支持M/intel 正版激活使用

Rose

trapcode particular插件怎么安装?ae粒子插件下载

Rose

Mac 电钢琴插件Applied Acoustics Systems Lounge Lizard EP-4

Rose

专为Mac用户设计的实时音乐分离插件 Acon Digital Remix

Rose

中文破解 Premiere Pro 2024从新手到高手教程合集

Rose

Agent 重磅升级!长对话记忆翻倍,工具调用更自主,决策快人一步!

火山引擎开发者社区

agent

PS插件:Infinite Panel Black & White for Mac(无限黑白面板插件)

Rose

Spring Security 4.0: WebSocket、Spring Data 和 Test Support_安全_Matt Raible_InfoQ精选文章