java - 使用 spring-boot 对 Rest 服务进行访问控制

标签 java spring mongodb spring-security spring-boot

我玩了一下 spring-boot 安全性。我使用 mongodb、spring-boot-starter-data-rest、spring-boot-starter-security 和 spring-boot-starter-web。我使用了自动暴露存储库(如 REST 服务)的可能性。我有两个存储库用户和客户。用户存储库用于帐户。 像这样扩展 WebSecurityConfigurerAdapter 后:

@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http.authorizeRequests()
    .antMatchers("/users/**")
    .hasRole("ADMIN").antMatchers("/", "/customers")
    .hasRole("USER")
    .anyRequest().authenticated()
    .and()
    .formLogin().permitAll();
}

A 无法控制具有 ADMIN 角色的用户对/users/页面的访问。我在访问服务时获得了身份验证,但每个角色的每个用户都可以访问任何地方。还应该配置什么?

最佳答案

这听起来你需要看看 Method Security Expressions Spring安全的特点。特别是注解@PreAuthorize/@PostAuthorize

关于java - 使用 spring-boot 对 Rest 服务进行访问控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39910658/

相关文章:

java - 在执行 .toString() 后如何取回对象?

java - 如何为 ListView 中的项目设置图像?

c# - 从azure函数连接到MongoDB

node.js - 在 Express 服务器重新启动时管理 WebSocket 连接

java - 使用 LibGDX 创建空心圆柱体

java - 如何将 url 参数绑定(bind)到 jax-rs 中的 Controller 参数对象

java - 如何将任务分配给小组?

java - 使用 cargo maven 插件将 spring 项目部署到远程服务器

node.js - 从node.js中的mongoDB数据生成JSON树

java - 用 Java 将 Vector 写入和读取到序列化文件?