使用Spring-boot-starter将Spring Security中的HttpBasic和FormLogin混合使用

 中青33期_840 发布于 2023-02-07 11:29

我使用spring-boot-starter 0.5.0.M6和spring security来构建我的应用程序,其中包含:

    "/ admin / "**:任何人都可以访问角色ADMIN,基于表单的登录

    "/ api / "**:任何人都应该可以访问角色API,http基本登录

我的第一次尝试是:

@Override
protected void configure(HttpSecurity http) throws Exception {
      http
        .authorizeRequests()
          .antMatchers("/resources/**").permitAll()
          .antMatchers("/admin/**").hasRole("ADMIN")
        .and()
          .formLogin()
          .defaultSuccessUrl("/admin/home")
          .loginPage("/login")
          .permitAll()
        .and()
          .logout()
          .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
          .permitAll();  
      http
        .authorizeRequests()
          .antMatchers("/api/**").hasRole("API")
        .and()
          .httpBasic();
}

用这种方法:

    所有"/ admin/"和"/ api / "都可以使用基本和基于表单的登录进行身份验证.这不是一个关键问题.

    发生任何安全问题时,例如:身份验证失败或授权失败,将显示登录表单.这是一个关键问题,我想如果/ api/**认证失败或授权失败,它会显示带有401/403状态代码的基本认证弹出窗口.

然后我尝试使用解决方案https://github.com/spring-projects/spring-security-javaconfig/blob/master/samples-web.md#sample-multi-http-web-configuration,但我只能确保其中任何一个/api/**,/admin/**但不是两个,取决于我注释的那个@Order.

请帮我一把

非常感谢

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有