【问题标题】:Spring security - what does authorizerequest(), anyRequest() and authenticated() do?Spring security - authorizerequest()、anyRequest() 和 authenticated() 做什么?
【发布时间】:2020-09-18 22:15:09
【问题描述】:
在下面的代码中,不同的链式方法有什么作用?
PUBLIC_URL 是一个包含公共 URL 的字符串数组。
protected void configure(HttpSecurity http ) throws Exception {
http.authorizeRequests()
.antMatchers(PUBLIC_URL).permitAll()
.anyRequest().authenticated();
}
【问题讨论】:
标签:
java
spring
spring-boot
rest
spring-security
【解决方案1】:
表示除了匹配PUBLIC_URL的请求外,所有请求都必须经过身份验证
【解决方案2】:
authorizeRequests() 允许使用RequestMatcher 实现基于HttpServletRequest 限制访问。
-
permitAll() 这将允许公共访问,即任何人都可以访问端点PUBLIC_URL 无需身份验证。
anyRequest().authenticated() 将限制对 PUBLIC_URL 以外的任何其他端点的访问,并且用户必须经过身份验证。
我们还可以根据权限配置访问,可以管理会话、HTTPS 通道等等。您可以从configure(HttpSecurity http)找到更多详细信息