首页 > 解决方案 > 使用 spring security oauth2 保护 Rest-API(Spring Security + OAuth2 + Spring Boot + STATELESS)

问题描述

我有一个 Spring-Boot 后端,它应该是一个 REST-API。我想支持社交登录。

如果我使用 Spring-Security Oauth2 Client,该类HttpSessionOAuth2AuthorizationRequestRepository将用于在会话中存储授权请求。

 http.cors();
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    http.csrf().disable();
    http.formLogin().disable();
    http.httpBasic().disable();
    http.addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    http.authorizeRequests().antMatchers("/auth/**").authenticated();
    http.exceptionHandling().authenticationEntryPoint(new RestAuthenticationEntryPoint());
    http.oauth2Login().authorizationEndpoint().baseUri("/oauth2/authorization")
            

有一些解决方案使用AuthorizationRequestRepository基于 cookie 的自定义。例如这个

然而 jgrandja 在他的评论中提到,使用 aHttpCookieRequestRepository不会使其完全无状态,因为在 Auth-Code-Flow 期间几乎没有其他机制正在使用HttpSession. 出于该目的使用 cookie 也可能存在一些安全漏洞。

  1. 如果我HttpSessionOAuth2AuthorizationRequestRepository用于此登录目的,API-Backend 是否仍被视为 REST?
  2. 替代方案会是什么样子?

标签: spring-bootrestspring-securityoauth-2.0spring-security-oauth2

解决方案


推荐阅读