spring - Spring Security 并没有像我想象的那样做
问题描述
我试图让它只有管理员可以查看/数据。邮递员允许用户和管理员访问它。想知道我做错了什么。
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin").password("{noop}pw").roles("ADMIN").and()
.withUser("user").password("{noop}pw").roles("USER");
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests()
.antMatchers("**/data*").hasRole("ADMIN")
.and().httpBasic();
httpSecurity.csrf().disable();
}
}
管理员应该能够访问数据/data
而用户不应该。
解决方案
尝试将 antMatchers 中的 URL 更改为从斜杠开始,然后重试。
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests()
.antMatchers("/**/data").hasRole("ADMIN")
.and().httpBasic();
httpSecurity.csrf().disable();
}
推荐阅读
- javascript - 为什么即使我刚刚推送了一个新数组,嵌套数组的长度仍返回未定义?
- java - 如何在 PDF 文件的框中选择文本?
- c++ - 如何将包含一个数字的字符串转换为 C++ 中的向量,其中所有数字都是向量的元素
- c# - 如何在代码 C# .net 5 中获取当前使用的 openapi 版本
- r - 按组在多列上汇总唯一值
- linux - 使用 at 命令(预定脚本)运行 bash 脚本不起作用
- vue.js - 将 Formstack 嵌入 Vuejs
- groovy - 如何从 groovy 元素访问 Camel 的属性 CamelLoopIndex?
- java - 我想通过 Java Rest api 在 Google Duo 上创建一个用户。遵循文档但出现错误
- colors - highlightcolor 与 monokai for atom 不同,与 sublime 文本中的不同