java - 如何在spring-security中为嵌套url设置不同的访问权限
问题描述
这是我的安全配置:
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("**/feedback")
.access("hasAnyAuthority('MANAGER','EMPLOYEE')")
.and().authorizeRequests().antMatchers("/api/**","/ticket/**")
.access("isAuthenticated()")
.and().authorizeRequests().antMatchers("**/create/**")
.access("hasAnyAuthority('MANAGER','EMPLOYEE')")
.and().formLogin().loginPage("/login").failureUrl("/login?error")
.and().formLogin().defaultSuccessUrl("/ticket/all", true)
.usernameParameter("email")
.passwordParameter("password")
.and().csrf().disable();
}
我希望每个人都有权查看票证“/ticket”,但我想禁止工程师访问 /ticket/create 和 /ticket/**/feedback。马上
antMatchers("/api/**","/ticket/**")
.access("isAuthenticated()")
允许访问所有人。我应该如何正确地做到这一点?多个 antMatchers 是否有某种覆盖规则?
解决方案
您需要添加以下代码:
.and().authorizeRequests().antMatchers("/ticket").permitAll()
代码将是:
http.authorizeRequests().antMatchers("**/feedback")
.access("hasAnyAuthority('MANAGER','EMPLOYEE')")
.and().authorizeRequests().antMatchers("/ticket")
.permitAll()
.and().authorizeRequests().antMatchers("/api/**","/ticket/**")
.access("isAuthenticated()")
.and().authorizeRequests().antMatchers("**/create/**")
.access("hasAnyAuthority('MANAGER','EMPLOYEE')")
.and().formLogin().loginPage("/login").failureUrl("/login?error")
.and().formLogin().defaultSuccessUrl("/ticket/all", true)
.usernameParameter("email")
.passwordParameter("password")
.and().csrf().disable();
我希望能帮助你。
推荐阅读
- flutter - 如何在 Flutter 中只加载一次异步 Stream?
- laravel - InvalidArgumentException: Action App\Http\Controllers\App\Http\Controllers\OrderController@store 未定义
- python - 使用“或”会导致错误的输出
- r - 子集后保留数据框列标签
- javascript - GSAP ScrollTrigger - 为每个部分创建时间线
- java - 使用 Spring-Reactor 的 changeRequestUri() 的正确方法是什么?
- linux - Jmeter:jmeter.sh和jmeter之间没有jmeter扩展文件的区别
- c# - 有条件地在列表初始化中添加一个项目
- mysql - 为什么选择查询中的 SQL RAND() 返回的值比我想要的要小?
- c - 打开命名管道以在 c 中写入时程序挂起 - 使用等待