当前位置: 首页 > 面试题库 >

Spring Webflux API的自定义身份验证

邵伟
2023-03-14
问题内容

我正在为Angular 5应用程序创建API。我想使用JWT进行身份验证。
我想使用Spring Security提供的功能,以便我可以轻松地使用角色。

我设法禁用基本身份验证。但是使用时http.authorizeExchange().anyExchange().authenticated();我仍然会收到登录提示。
我只想输入403而不是提示。因此,通过检查Authorization令牌标题的“事物”(是否是过滤器?)来覆盖登录提示。

我只想在将返回JWT令牌的控制器中进行登录。但是我应该使用哪种Spring Security Bean来检查用户凭证?我可以构建自己的服务和存储库,但是我想尽可能多地使用Spring Security提供的功能。

这个问题的简短版本是:
如何自定义Spring Security的身份验证?
我必须创建什么豆?
我必须在哪里放置配置?(我现在有个SecurityWebFilterChain


问题答案:

经过大量搜索和尝试,我认为我找到了解决方案:

你需要一个SecurityWebFilterChain包含所有配置的bean 。
这是我的:

@Configuration
public class SecurityConfiguration {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private SecurityContextRepository securityContextRepository;

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        // Disable default security.
        http.httpBasic().disable();
        http.formLogin().disable();
        http.csrf().disable();
        http.logout().disable();

        // Add custom security.
        http.authenticationManager(this.authenticationManager);
        http.securityContextRepository(this.securityContextRepository);

        // Disable authentication for `/auth/**` routes.
        http.authorizeExchange().pathMatchers("/auth/**").permitAll();
        http.authorizeExchange().anyExchange().authenticated();

        return http.build();
    }
}

我已禁用httpBasic,formLogin,csrf和注销,因此可以进行自定义身份验证。

通过设置AuthenticationManager和,SecurityContextRepository我将覆盖默认的spring安全配置,以检查用户是否已针对请求进行了身份验证/授权。

认证管理器:

@Component
public class AuthenticationManager implements ReactiveAuthenticationManager {

    @Override
    public Mono<Authentication> authenticate(Authentication authentication) {
        // JwtAuthenticationToken is my custom token.
        if (authentication instanceof JwtAuthenticationToken) {
            authentication.setAuthenticated(true);
        }
        return Mono.just(authentication);
    }
}

我不确定身份验证管理器的用途,但我想进行最终身份验证,因此请authentication.setAuthenticated(true);在一切正确的时候进行设置。

SecurityContextRepository:

@Component
public class SecurityContextRepository implements ServerSecurityContextRepository {

    @Override
    public Mono<Void> save(ServerWebExchange serverWebExchange, SecurityContext securityContext) {
        // Don't know yet where this is for.
        return null;
    }

    @Override
    public Mono<SecurityContext> load(ServerWebExchange serverWebExchange) {
        // JwtAuthenticationToken and GuestAuthenticationToken are custom Authentication tokens.
        Authentication authentication = (/* check if authenticated based on headers in serverWebExchange */) ? 
            new JwtAuthenticationToken(...) :
            new GuestAuthenticationToken();
        return new SecurityContextImpl(authentication);
    }
}

在负载中,我将根据中的标头检查serverWebExchange用户是否已通过身份验证。



 类似资料:
  • 问题内容: 这是我的情况: 一个Web应用程序对许多应用程序执行某种SSO 登录的用户,而不是单击链接,该应用就会向正确的应用发布包含用户信息(名称,pwd [无用],角色)的帖子 我正在其中一个应用程序上实现SpringSecurity以从其功能中受益(会话中的权限,其类提供的方法等) 因此,我需要开发一个 自定义过滤器 -我猜想-能够从请求中检索用户信息,通过自定义 DetailsUserSe

  • 问题内容: 我可以使用Google帐户在AppEngine中对用户进行身份验证的方式非常好。 但是,我需要使用 自定义的身份验证登录系统 。 我将有一个AppUsers表,其中包含用户名和加密密码。 我阅读了有关gae会话的内容,但在启动应用安全性方面需要帮助。 如何跟踪经过身份验证的用户会话?设置cookie? 初学者。 问题答案: 您可以使用cookie来做到这一点……其实并不难。您可以使用C

  • 我在spring MVC项目中实现了一个自定义身份验证提供程序。在我自己的重载authenticate()方法中,我实现了自己的身份验证,其中我构造了自己的UserPasswordAuthenticationToken()并返回对象。 现在,上述对象“UserPasswordAuthentictionToken”中的用户ID被匿名化,密码为null,权限设置为授予该用户的权限。 问题: 这是否会导

  • 我正在为Angular 5应用程序创建API。我希望使用JWT进行身份验证。 我希望使用spring security提供的特性,以便能够轻松地处理角色。 我只想在返回JWT令牌的控制器中进行的登录。但是我应该使用什么Spring Securitybean来检查用户凭据呢?我可以构建自己的服务和存储库,但我希望尽可能使用spring security提供的特性。 这个问题的简短版本只是: 我如何自

  • 我们有一个Spring5,非SpringBoot应用程序,使用Springfox 2.9.2 Swagger UI。 我不知道如何保护 /api-docsendpoint:我希望它每次访问时都调用我的身份验证函数。我让它为swagger-ui.html工作,但没有成功 /api-docs.这是我得到的。 另一种选择是永久关闭对 /api-docs 的访问,只需直接调用从某个新终结点生成 JSON

  • Postman上有没有办法在执行请求A之前预先执行请求B? 上下文:我创建了一个Symfony应用程序来管理一组web服务。大多数路由只有在用户完全通过身份验证后才能被调用(对于熟悉框架的人使用form_login和防火墙) 因此,我必须先打电话: 邮政 /login 用户名 密码 在能够呼叫之前 获取/某些_安全_路线 (返回 JSON 响应) 当我在Postman构建器中连续手动调用路由时,这