首页 > 解决方案 > 浏览器默认基本认证 spring-boot angular

问题描述

我知道有很多关于那里Angularbasic authentication外面的话题,我阅读了其中的大部分,但没有一个适用于我想要的行为。

我的应用程序有 2 个用例:

  1. 前端spring-boot作为静态资源打包在后端
  2. 前端分别在 localhost:4200 和后端分别运行在 localhost:8080

我的安全配置如下:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors()
            .and().csrf().disable()
                .httpBasic().authenticationEntryPoint(authEntryPoint)
            .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
                .authorizeRequests()
                    .accessDecisionManager(accessDecisionManager)
                    .expressionHandler(expressionHandler)
                    .antMatchers("/api/**").authenticated()
            .and()
                .logout().logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
            .and()
                .addFilterAfter(jwtTokenFilter, BasicAuthenticationFilter.class);
}

在第一个场景中,前端和后端都在同一个端口上提供服务,我导航到http://localhost:8080/context-path我得到浏览器的基本身份验证掩码。

基本认证

但是当我转到单独运行的前端时http://localhost:4200,我只会401 - not authenticated弹出一个没有基本身份验证的窗口。

401 错误

所以我的问题是,当前端和后端在不同的端口/网址上运行时,我怎样才能得到这个弹出窗口?或者这是不可能的?

标签: angularspring-bootbasic-authentication

解决方案


推荐阅读