如何在新的 oauth2.0 服务器中配置没有 formlogin 的 SecurityConfig?

问题描述 投票:0回答:1

我的 spring 授权服务器依赖项:-

   <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-authorization-server</artifactId>
        <version>1.0.0</version>
    </dependency>

授权服务器的SecurityConfig类:-

公共类 SecurityConfig {

@Autowired
private JwtAuthenticationEntryPoint authenticationEntryPoint;


@Bean
@Order(1)
public SecurityFilterChain asSecurityFilterChain(HttpSecurity http) throws Exception {
    OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);

    http.getConfigurer(OAuth2AuthorizationServerConfigurer.class)
            .authorizationEndpoint(a -> a.authenticationProviders(getAuthorizationEndPoints()))
            .oidc(Customizer.withDefaults());

    http.exceptionHandling(exception -> exception
            .authenticationEntryPoint(authenticationEntryPoint));

    return http.build();
}

@Bean
@Order(2)
public SecurityFilterChain appSecurityFilterChain(HttpSecurity http) throws Exception {
    http.csrf().disable();
    http.cors(c -> c.configurationSource(
            request -> {
                CorsConfiguration config = new CorsConfiguration();
                config.addAllowedOrigin(request.getHeader("Origin"));
                config.setAllowedMethods(List.of("GET", "POST", "DELETE", "LINK", "UNLINK", "PATCH", "PUT", "OPTIONS"));
                config.setAllowedHeaders(List.of("Content-Type, Accept, X-Requested-With, remember-me"));
                config.setAllowCredentials(true);
                config.setExposedHeaders(List.of("Authorization"));
                config.setMaxAge(3600L);
                return config;
            }
    ));

    http
            .httpBasic().disable()
            .formLogin().disable()
            .authorizeHttpRequests()
            .requestMatchers("/authenticate").permitAll()
            .anyRequest().authenticated();

    return http.build();
}

}

问题:-

  1. 当我调用以下 API 时 http://localhost:7171/oauth2/authorize?response_type=code&client_id=client&scope=openid&redirect_uri=http://localhost:3000

它抛出

2023-05-16T15:31:51.127+05:30 TRACE 63279 --- [nio-7171-exec-4] o.s.s.w.a.ExceptionTranslationFilter:发送 AnonymousAuthenticationToken [Principal=anonymousUser,Credentials=[PROTECTED],Authenticated=true,Details= WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=null], Granted Authorities=[ROLE_ANONYMOUS]] 到身份验证入口点,因为访问被拒绝

org.springframework.security.access.AccessDeniedException:拒绝访问 在 org.springframework.security.web.access.intercept.AuthorizationFilter.doFilter(AuthorizationFilter.java:98) ~[spring-security-web-6.0.3.jar:6.0.3]

  1. 当我使用 formlogin() 时,它用于重定向到 HTTP://localhost:7171/login 并询问用户名和密码并返回 authorization_code 需要在下面输入“http://localhost:7171/oauth2 /token " 给出 OAuth2 令牌。 如何将它与 UI 集成。

  2. 如何编写自定义身份验证来进行身份验证并提供 oauth2 令牌?

spring-security angular-oauth2-oidc
1个回答
0
投票

您的授权服务器应配置表单登录(它负责对用户进行身份验证),并且您的体系结构中的 OAuth2 客户端应该使用

Authorization code
流程(使用 PKCE)在涉及用户时获取令牌。

您的前端不应访问用户凭据(包含登录名和密码的表单)。出于安全原因,

password
implicit
流程均已弃用。

OAuth2 客户端应该使用一个库来存储令牌并处理您遇到的所有重定向问题。

目前至少有 2 个选项来定义什么是 OAuth2 客户端:

  • 前端本身:移动应用程序或浏览器中的应用程序,使用基于 Javascript 的框架(Angular、React、Vue 等)编写,并使用 lib 将其配置为 Oauth2 公共客户端(移动应用程序和浏览器应用程序都不能保留一个秘密)。 angular-auth-oidc-client 是一个文档齐全且功能齐全的客户端库的示例(如果使用 Angular 以外的其他东西,请为您的框架找到一个等效项)
  • a Backend For Frontend,服务器上的中间件:
    • 配置为 OAuth2 客户端
    • 使用会话(不是访问令牌)保护与前端的通信
    • 负责OAuth2登录(处理授权码流)和注销
    • 在会话中存储令牌
    • 在将请求从前端转发到资源服务器之前,用包含
      Authorization
      访问令牌的
      Bearer
      标头替换会话 cookie

现在经常首选第二个选项,因为:

  • OAuth2 令牌对浏览器隐藏,更重要的是对其中运行的 Javascript 代码隐藏(令牌存储在 BFF 会话中,会话 cookie 应标记为
    HttpOnly
  • BFF 运行在您信任的服务器上,它可以保密并配置为机密客户端(您可以确信令牌是发给您认识的客户端的)

spring-cloud-gateway
可以配置为 BFF:使用
spring-boot-starter-oauth2-client
并使用
TokenRelay
过滤器。我写了一个教程那里.

© www.soinside.com 2019 - 2024. All rights reserved.