繁体   English   中英

Spring Boot-如何使用Cors和Spring Security修复Session Bean

[英]Spring Boot - How to fix Session bean with cors and spring security

TL; DR
每当localhost:4200(通过cors过滤器)向localhost:8080发出http请求时,它都会丢失保留了身份验证的sessionscope bean,这基本上使它无法通过403进行所有调用。不包括第一个http请求(这不落后于spring安全)

我有一个在localhost:8080上运行良好的spring boot应用程序。 我们正在其中创建一个有角度的iframe,它也可以很好地工作(在localhost:8080上部署时)

但是当我们在localhost:4200(ng serve)上执行此操作时,它将无法正常工作

它开始抱怨cors,所以除了我添加的关于cors的所有内容外,我都具有以下配置。

@Configuration
@Profile({"dev"})
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class springDevConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception{
    http.csrf().disable();
    http.headers().frameOptions().sameOrigin();
    http.headers().cacheControl().disable();
    http.cors().configurationSource(corsConfigurationSource())
    .and().authorizeRequests().anyRequest().permitAll();
  }

  @Bean
  public CorsConfigurationSource corsConfigurationSource(){
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

    CorsConfiguration configuration = new CorsConfiguration();

    configuration.setAllowedOrigins(
    ImmutableList.of("http://localhost:4200"));

    configuration.setAllowedMethods(Arrays.asList(
    RequestMethod.GET.name(),
    RequestMethod.POST.name(),
    RequestMethod.OPTIONS.name(),
    RequestMethod.DELETE.name(),
    RequestMethod.PUT.name()));

    source.registerCorsConfiguration("/**", configuration);

    return source;
  }
}

我的会话bean非常简单

@Component
@SessionScope
public class UserSession {
   //Holds the Authorities for the prePostEnabled
   User user; 
   ...
}

要初始化用户,我向某个端点( 不受保护的 )发出请求,并在代码中执行类似的操作

...
User user = new User(id, "", authorities);

Authentication auth = new UsernamePasswordAuthenticationToken(
user, null,authorities));

SecurityContextHolder.getContext().setAuthentication(auth);

Usersession.setUser(user);
...

当我在localhost:8080上发出http请求时,后续的http请求具有相同的session

但是,当我尝试从localhost:4200向localhost:8080发出请求时, 每个请求似乎都获取了一个不同的UserSession /也许打开了一个新会话?
(在所有受保护的端点上给我403)

到底发生了什么?为什么每次调用localhost:8080进行新会话时localhost:4200? (以及应该更改哪些配置来解决此问题?)

附录1 :如果我评论

@EnableGlobalMethodSecurity(prePostEnabled = true)

该代码对于localhost:4200效果很好(我的意思是他不再拥有403代码),但是在每个请求中可能仍在使用另一个会话作用域bean

附录2º
现在可以使用
我要做的就是将ssl放在ng serve配置中(它在localhost:8080,但不是4200),JSessionId开始工作!

您能否显示更多有关如何嵌入iframe以及为原始页面提供服务的内容?

似乎正在发生的事情是您正在有效地提出跨域请求而没有意识到。 8080和4200是不同的域,如果页面的一部分是从一个页面加载的,而部分(即iframe)是从另一个页面加载的,则它构成了一个跨域请求,并且一个域发送的cookie不会应用于对页面的请求。其他,因此不共享会话范围。 另外,如果您要向非原始页面加载域的域发出Ajax请求,则除非您设置了CORS,否则默认情况下将拒绝它们。 这解释了为什么您必须这样做。

您必须确保页面的所有部分(包括iframe和Ajax请求)都从同一个域加载,或者您可以使用其他方法将会话附加到请求。 通常,JSESSIONID cookie用于此目的。 此Cookie是否会在初始响应中发送?

通常,您有一个为前端服务的应用程序,包括初始页面(例如4200上的应用程序),以及一个仅响应API调用的应用程序(例如8080上的应用程序),并且配置了CORS。 这样,对后端的所有调用都通过同一域完成,并且cookie可以正常应用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM