java - httpBasic() 和authorizeRequest() 之间的区别

标签 java spring-boot security spring-security

好吧,我正在学习 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/

相关文章:

java - 第一个订阅者阅读后删除 MQTT 消息

java - Jersey 2.x 的 InjectionResolver——资源被调用两次

java - EntityManager.remove() 不会生成删除查询并且不会删除实体

java - Hibernate不允许保存具有相似id的数据

javascript - 如何防止 html 元素转义父元素? (安全)

java - 制作一个健壮的、可调整大小的 Swing 棋图形用户界面

java - Spring 启动 : RestTemplate postForObject 400 bad request

java - 我可以使用 @MockBean 在我的测试类的一种方法中模拟 bean 吗?

api - 如何保护API?

php - 交响乐 5 : how to check if user is banned or not before login