好吧,我正在学习 Spring Security,我遇到了一些私有(private)代码,其配置如下所示。
httpSecurity.authorizeRequests().anyRequest().permitAll();
现在,我看到了 httpsecurity 方法的 javadoc,并遇到了 httpBasic()
httpSecurity.httpBasic();
这两行的输出是相同的。那么,有人可以帮助我理解其中的区别吗?
最佳答案
authorizeRequest()
authorizeRequest()
用于使用 RequestMatcher 实现(即通过 URL 模式)基于 HttpServletRequest 限制访问。
示例配置:
最基本的示例是将所有 URL 配置为需要角色“ROLE_USER”。下面的配置需要对每个 URL 进行身份验证,并将授予用户“admin”和“user”访问权限。
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests(authorizeRequests ->
authorizeRequests
.antMatchers("/**").hasRole("USER")
)
.formLogin(withDefaults());
}
httpBasic()
配置 HTTP 基本身份验证。 HTTP 基本身份验证实现是对 Web 资源实现访问控制的最简单技术,因为它不需要 cookie、 session 标识符或登录页面。默认领域是“Spring Security 应用程序”。
配置示例
下面的示例演示了如何为应用程序配置 HTTP 基本身份验证。
@Configuration
@EnableWebSecurity
public class HttpBasicSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").hasRole("USER").and().httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}
}
关于java - httpBasic() 和authorizeRequest() 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61123520/