- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经实现了 UserDetailsService
来为我的 Spring Boot REST 服务返回带有 SimpleGrantedAuthority
的 UserDetails
实例。我有 3 个价格页面,其中两个授予对特定端点的访问权限,第三个授予对所有端点的访问权限。
我已按如下方式配置我的 WebSecurityConfig
。这两个特定权限有效 - 只有它们可以用于访问这些端点。然而,应授予客户端访问所有端点的第三个权限会返回 403 禁止。
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.httpBasic().and()
.authorizeRequests().antMatchers(“/**/foo/**”).hasAuthority(“foo”).and()
.authorizeRequests().antMatchers(“/**/moo/**”).hasAuthority(“moo”).and()
.authorizeRequests().antMatchers(“/**/**”).hasAuthority(“admin”)
}
我尝试过重新排序定义,也尝试过 anyRequest
但它们不起作用。
最佳答案
HttpSecurity#authorizeRequests()
javadoc 说:
<小时/>Note that the matchers are considered in order. Therefore, the following is invalid because the first matcher matches every request and will never get to the second mapping:
http.authorizeRequests().antMatchers("/**").hasRole("USER").antMatchers("/admin/**")
.hasRole("ADMIN")
也许你想要:
protected void configure(final HttpSecurity http) throws Exception {
http.csrf().disable()
.httpBasic().and()
.authorizeRequests().antMatchers("/**/foo/**").hasAnyAuthority("admin", "foo").and()
.authorizeRequests().antMatchers("/**/moo/**").hasAnyAuthority("admin", "moo").and()
.authorizeRequests().antMatchers("/**").hasAuthority("admin");
}
或者,设置RoleHierarchy
:
protected void configure(final HttpSecurity http) throws Exception {
http.csrf().disable()
.httpBasic().and()
.authorizeRequests().antMatchers("/**/foo/**").hasAuthority("foo").and()
.authorizeRequests().antMatchers("/**/moo/**").hasAuthority("moo").and()
.authorizeRequests().antMatchers("/**").hasAuthority("admin");
}
@Bean
RoleHierarchyVoter roleVoter(final RoleHierarchy roleHierarchy) {
return new RoleHierarchyVoter(roleHierarchy);
}
@Bean
public RoleHierarchy roleHierarchy() {
final Map<String, List<String>> hierarchyMap = new HashMap<>();
hierarchyMap.put("admin", List.of("foo", "moo"));
final String roleHierarchy = RoleHierarchyUtils.roleHierarchyFromMap(hierarchyMap);
final RoleHierarchyImpl ret = new RoleHierarchyImpl();
ret.setHierarchy(roleHierarchy);
return ret;
}
关于java - WebSecurityConfigurerAdapter 配置重叠权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62300912/
我有这个问题使用 SpringBoot 和 SpringBoot-Security 实现自定义登录身份验证。我做了一个Bitbucket repository作为该线程的引用(在 CustomSecu
我已经实现了 UserDetailsService 来为我的 Spring Boot REST 服务返回带有 SimpleGrantedAuthority 的 UserDetails 实例。我有
我尝试将 Spring Security 添加到我的项目中,但没有成功。我仍然可以在没有任何登录的情况下访问我的网络应用程序。 主要配置 public class SpittrWebAppInitia
这是我的测试,我正在添加额外的 header 测试 after testing my filter @RunWith( SpringJUnit4ClassRunner.class ) @WebAppC
我们有一个看起来像这样的配置: @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {
我正在尝试为我的应用程序配置多种身份验证类型,为每种类型的身份验证使用单独的 WebSecurityConfigurerAdapter。 总体思路是使用 WebSecurityConfigurerAd
我的 build.gradle 包含: compile('org.springframework.security:spring-security-web:4.1.3.RELEASE') “刷新所有
我目前正在使用 Spring Security 进行 Oauth2 实现,并且我发现了许多使用 ResourceServerConfigurerAdapter 的文档。随着WebSecurityCon
过去几天我一直在玩 Spring,事情变得很有趣。我现在正在与安全部门合作,但遇到了一些小问题。基本上,我希望通过 API 调用而不是表单进行身份验证。有没有一种巧妙的方法来做到这一点? 我已经像这样
我有 SPNEGO 的 spring 安全配置,它正在“破解”。它看起来如下: @Configuration @EnableWebSecurity public class SpnegoConfig
正如他们对我们的描述 here ,WebSecurityConfigurerAdapter 将在一段时间内弃用。 由于我想实现 JWT 模式,我尝试使用 SecurityFilterChain 重构
我正在构建一个需要处理两种类型身份验证的应用程序,所以我这样做了 @Autowired UserService userService; @Autowired public void configAu
我正在尝试在扩展 WebSecurityConfigurerAdapter 的类中使用 Spring 的 Security 的 Java 配置。我使用的是 3.2.0.RELEASE 版本。 在 Ht
我正在使用WebSecurityConfigurerAdapter,我想知道是否有办法配置WebSecurityConfigurerAdapter 中的错误处理程序就像以前在 web.xml 中一样我
我使用 SpringBoot 从事一个学校项目,并在添加了一些在互联网上看到的安全功能(WebSecurityConfigurerAdapter)后,我想知道是否有可能提取当前登录用户的用户名,因为我
In Spring security when I am using WebSecurityConfigurerAdapter, throws the following error. Anyone
构建 Spring 应用程序后,我遇到了这个问题: .../WebSecurityConfigurerAdapter.class] cannot be opened because it does
这些类之间有什么区别?我知道 WebSecurityConfigurerAdapter 用于自定义我们应用程序的“安全性”。 我做了什么: public class SecurityConfig ex
在我的 Spring Boot 应用程序中,我有以下两个类: @EnableWebSecurity public class AppSecurityConfig extends WebSecurity
我想为我网站上的某个“/interface”URL 启用 POST 请求。我已经通过 Class[] getRootConfigClasses() 成功加载了类(class), HttpSecurit
我是一名优秀的程序员,十分优秀!