【问题标题】:disable basic auth on static content using spring security使用spring security禁用静态内容的基本身份验证
【发布时间】:2019-02-19 18:41:22
【问题描述】:

我有一个 Angular 应用程序作为 Spring Boot 应用程序的静态内容提供。角度应用程序位于 spring boot 应用程序的 target/classes/static/index.html 内。我还有一个从 spring boot 提供的 rest api,它需要启用基本身份验证。我已将我的安全配置配置如下

@Configuration
@EnableWebSecurity
public class SecrityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private AuthenticationEntryPoint authEntryPoint;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("john123").password("password").roles("USER");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests()
            .anyRequest().authenticated()
            .and().httpBasic()
            .authenticationEntryPoint(authEntryPoint);
}}      

基本身份验证对其余端点按预期工作。但是当我尝试从 localhost:8080/springbootappname/ 加载角度应用程序时,它会提示凭据。当我提供已配置的凭据时,正在加载 Angular 应用程序。

所以,我需要帮助禁用正在解压缩到 classes/static/ 中的 Angular 应用程序的基本身份验证

【问题讨论】:

    标签: spring angular spring-boot spring-security


    【解决方案1】:

    您可以通过多种方式管理它来服务器静态内容。

    1. 您可以覆盖静态内容的安全性。

      @Override
        public void configure(WebSecurity web) throws Exception {
          web
            .ignoring()
               .antMatchers("/target/classes/static/**");
        }
      
    2. 您甚至可以使用匹配的 antmacher 在 http 安全覆盖中对其进行管理。

      @Override
      protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable().authorizeRequests()
              .antMatchers("/target/classes/static/**").permitAll()
              .anyRequest().authenticated()
              .and().httpBasic()
              .authenticationEntryPoint(authEntryPoint);
      }
      
    3. 更好地管理资源中的静态内容。请参阅链接

    https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot

    【讨论】:

    • 嘿,我用上面的配置试过了,没用。基本上 /target/classes/static/** 不是路径对吗? spring boot 将生成一个 jar,当我提取 jar 时,角度应用程序将与其他静态资源一起出现在 BOOT-INF/classes/static/index.html 中。我需要在这里为静态资源禁用基本身份验证。
    • 是的,把实际路径放在这里 /target/classes/static/ 你有你的静态内容。
    • 您尝试了哪个选项,它仍然要求输入用户/密码或任何其他错误。
    • 我都试过了,都没有用。它仍在询问用户名和密码
    猜你喜欢
    • 2013-02-11
    • 2014-10-27
    • 2011-02-11
    • 2016-03-20
    • 1970-01-01
    • 2015-02-06
    • 2014-08-31
    • 2016-04-30
    • 2021-09-19
    相关资源
    最近更新 更多