当前位置: 首页 > 知识库问答 >
问题:

如何使用spring security为特定endpoint添加HTTP基本身份验证?

郏稳
2023-03-14

我有一个Spring Boot应用程序与Spring Security。将配置新的终结点/Health,以便通过基本HTTP身份验证进行访问。当前的HttpSecurity配置如下:

@Override
protected void configure(HttpSecurity http) throws Exception {

http.requestMatchers()
    .antMatchers(HttpMethod.OPTIONS, "/**")
    .and()
    .csrf()
    .disable()
    .authorizeRequests()
    .anyRequest()
    .permitAll()
    .and()
    .sessionManagement()
    .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

}

如何为/Health添加base auth?我想我需要这样的东西,但我不认为这是完全正确的,我真的不明白到底在哪里添加它:

    .authorizeRequests()
    .antMatchers(
        // Health status
        "/health",
        "/health/"
    )
    .hasRole(HEALTH_CHECK_ROLE)
    .and()
    .httpBasic()
    .realmName(REALM_NAME)
    .authenticationEntryPoint(getBasicAuthEntryPoint())
    .and()
    .sessionManagement()
    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)

我发现这些资源很有帮助,但还不够:

  • http://www.baeldung.com/spring-security-basic-authentication
  • http://websystique.com/spring-security/secure-spring-rest-api-using-basic-authentication/

共有2个答案

仰雅昶
2023-03-14
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/health/**").hasRole("SOME_ROLE")
            .anyRequest().authenticated()
            .and()
            .httpBasic()
            .and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {


        auth
            .inMemoryAuthentication()
            .withUser("yourusername").password("yourpassword").roles("SOME_ROLE")

        ;
    }

}
史昊焱
2023-03-14

解决方案是实现多种配置,如下所述:https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#multiple-httpsecurity

编辑2021-12-11:

档案参考链接:https://web.archive.org/web/20210126145444/https://docs.spring.io/spring-安全性/站点/文档/当前/参考/html5/#多个httpsecurity

一个类似的问题链接到一个例子:https://stackoverflow.com/a/18864786/1568224

 类似资料:
  • 我想你可以想象家务控制器中有什么,但为了完整起见,这里有相关的部分: 下面是测试在输出中打印的请求: 那么,为什么我的测试会得到401的返回代码,我如何修复它?

  • 问题内容: 我有一个现有的angularjs代码,可以进行HTTP GET。下面提取的是控制器内部的一些相关代码。 我想将HTTP基本身份验证添加到HTTP GET。用户名为,密码为。如何才能做到这一点? 问题答案: 因为在基本身份验证中,用户名和密码由base64解码。您首先需要找到一个图书馆来完成这项工作。 https://github.com/ninjatronic/angular- bas

  • 问题内容: 如何在Golang网站上实施基本身份验证?这样,当有人访问某个页面时,他们的 浏览器 会提示他们登录。 问题答案: 为了强制用户进行身份验证/在用户浏览器中显示基本身份验证提示,请发送标头 因此,如果您有通话,您可以 这将导致浏览器打开您所指的提示。

  • 问题内容: 以下代码成功连接到我的Ruby on Rails API,并使用AFNetworking返回JSON。我需要做什么来进行编辑以传递用户名和密码,以便我的API可以使用HTTP基本身份验证? 我已经阅读了他们的文档,但是我对Objective-C和AFNetworking还是陌生的,目前还没有意义。 问题答案: AFNetworking 2.x的答案已更新 对于AFNetworking

  • 有没有办法对/oauth/tokenendpoint禁用HTTP基本身份验证?我希望在JSON主体或请求头中发送client_id和client_secret。 我想工作的卷曲例子: 或

  • 我尝试了angular.js,并从一个restful api的web-ui开始(使用超文本传输协议基本授权)。这真的很好,除了授权工作。 到目前为止,我使用的是设置密码,但大多数情况下浏览器会打开http basic auth的默认登录表单。另一个奇怪的行为是angular请求不包含授权头(选项和GET请求都不包含)。我还尝试使用header配置在每个请求上设置此header,但这也不起作用。 有