简体   繁体   English

如何使用ReactiveUserDetailsS​​ervice通过springboot通过Rest API登录

[英]How to login via Rest API using ReactiveUserDetailsService springboot

I m using reactive spring security with springboot, my front-end is built with Angular 6, Everything is working fine if i use springboot default login form. 我在springboot中使用反应式弹簧安全性,我的前端是用Angular 6构建的,如果我使用springboot默认登录表单,一切都可以正常工作。

I want to login with rest api. 我想使用rest api登录。

Rest login method: 休息登录方式:

@PostMapping(path = "/login", consumes = MediaType.APPLICATION_JSON_VALUE)
public Mono<UserDetails> login(@RequestBody User user) {
    Mono<UserDetails> isLoggedIn = Mono.empty();
    try{
        isLoggedIn = this.userDetailsService.findByUsername(user.getUsername());
    }catch (RuntimeException e){
        return isLoggedIn;
    }

    return isLoggedIn;
}

My SecurityConfig is following: 我的SecurityConfig如下:

@Configuration
@EnableWebFluxSecurity 
public class SecurityConfig {

    @Bean
    SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
    http.authorizeExchange().pathMatchers("/login","/signup","/api/users/login","/ui/**","/webjars/**").permitAll()
            .anyExchange().authenticated()
            .and()
            .httpBasic().disable()
            .formLogin().disable()
            .csrf().disable()
            .logout().disable();
    return http.build();
   }
}

My ReactiveUserDetailService as following: 我的ReactiveUserDetailService如下:

@Component
public class ServiceReactiveUserDetailsService implements ReactiveUserDetailsService {
     private UserRepo userRepo;

     public ServiceReactiveUserDetailsService(UserRepo userRepo) {
        this.userRepo = userRepo;
     }

     @Bean
     PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
     }

     @Override
     public Mono<UserDetails> findByUsername(String username) {
         return this.userRepo.findByUsername(username).map(CustomUser::new);
     }

    private class CustomUser extends User implements UserDetails {
        public CustomUser(User user) {
            super(user);
        }

       @Override
       public Collection<? extends GrantedAuthority> getAuthorities() {
          return AuthorityUtils.createAuthorityList("ROLE_USER");
       }

       @Override
       public boolean isAccountNonExpired() {
          return true;
       }

       @Override
       public boolean isAccountNonLocked() {
           return true;
       }

       @Override
       public boolean isCredentialsNonExpired() {
          return true;
       }

       @Override
       public boolean isEnabled() {
          return true;
       }
   }
}

I m new in spring world, Any help will be appreciated 我是春季世界的新手,将不胜感激

Maybe try using formLogin.loginPage method to point to your login method instead of disabling formLogin. 也许尝试使用formLogin.loginPage方法指向您的登录方法,而不是禁用formLogin。 I think the problem is in your security config. 我认为问题出在您的安全性配置中。

EDIT: these tutoials might help: https://dzone.com/articles/reactive-spring-security-for-webflux-rest-web-serv https://www.baeldung.com/spring-security-5-reactive 编辑:这些工具可能会有所帮助: https ://dzone.com/articles/reactive-spring-security-for-webflux-rest-web-serv https://www.baeldung.com/spring-security-5-reactive

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

相关问题 SpringBoot 登录 Spring 安全性 JWT 用于 REST ZDB974238714CA8DE634A7CE1D083A1 - SpringBoot login with Spring Security with JWT for REST API SpringSecurity 自定义登录功能不适用于 SpringBoot 和 REST api - SpringSecurity custom login functionality doesn't work with SpringBoot and REST api 使用 RESTTemplate springboot 使用公共 REST API - consuming public REST API using RESTTemplate springboot 如何在 SpringBoot REST Api 中调用重写的 handleMethodArgumentNotValid? - How to invoke an overridden handleMethodArgumentNotValid in a SpringBoot REST Api? 如何使用springboot创建mongodb通用rest api? - How to create mongodb generic rest api with springboot? 通过SpringBoot调用REST API时不支持POST异常 - POST not supported exception while invoking REST api via SpringBoot 404 Rest Api with springboot - 404 Rest Api with springboot 如何使用剩余部分将Chatbase与SpringBoot集成 - How to integrate Chatbase with SpringBoot, using rest 如何使用springboot使用bitbucket rest api获取所有存储库? - How can i use the bitbucket rest api to get all the repos using springboot? 如何在我们使用 springboot 的 Rest API 应用程序中将域或 IP 地址列入白名单? - How can I whitelist Domain or IP address in a Rest API application in which we are using springboot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM