Spring boot - autentizace/autorizace endpointu

vesterna12

  • ***
  • 124
  • byrokracie zabíjí kreativitu
    • Zobrazit profil
    • E-mail
Re:Spring boot - autentizace/autorizace endpointu
« Odpověď #15 kdy: 26. 04. 2023, 23:21:45 »
S tim si pak uz poradim, ale aktualne mam problem s autentizaci kdy se kazdy uzivatel muze autentizovat pouze jednou.
Pri druhem pokusu vidim hlasku

Kód: [Vybrat]
023-04-26T20:06:01.343+02:00  WARN 160351 --- [io-8080-exec-10] o.s.s.c.bcrypt.BCryptPasswordEncoder     : Empty encoded password
Kód: [Vybrat]
public class SecurityConfig {

    @Bean
    //authentication
    public UserDetailsService userDetailsService() {
        return new UserInfoUserDetailsService();
    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationProvider authenticationProvider(){
        DaoAuthenticationProvider authenticationProvider=new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(userDetailsService());
        authenticationProvider.setPasswordEncoder(passwordEncoder());
        return authenticationProvider;
    }


    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http.csrf().disable()
                .authorizeHttpRequests()
                .requestMatchers("/products/welcome","/products/new").permitAll()
                .and()
                .authorizeHttpRequests().requestMatchers("/products/**")
                .authenticated().and().formLogin().and().build();
    }
}


Kód: [Vybrat]
@Repository
public class UserDao {
    private final static List<UserDetails> APPLICATION_USERS = Arrays.asList(
            new User("email@email","$2a$12$rEGy9.W4cVmwftdHed74quwkJVfPRZt1TgAsF8le9isOWvOWdlscW", Collections.singleton(new SimpleGrantedAuthority("ROLE_ADMIN"))),
            new User("email2@email","$2a$12$rEGy9.W4cVmwftdHed74quwkJVfPRZt1TgAsF8le9isOWvOWdlscW", Collections.singleton(new SimpleGrantedAuthority("ROLE_VIEW"))),
            new User("email2@email","password2", Collections.singleton(new SimpleGrantedAuthority("ROLE_CLIENT")))
    );
    public UserDetails findUserByEmail(String email){
        System.out.println("search user");
        System.out.println(APPLICATION_USERS.stream().filter(u->u.getUsername().equals(email)).findFirst());
        return APPLICATION_USERS.stream().filter(u->u.getUsername().equals(email)).findFirst().orElseThrow(()->new UsernameNotFoundException("User not found"));
    }
}


Nasel jsem problem, reseni je v modifikaci tridy UserDao, ktera obsahuje testovaci seznam uzivatelu. V nasledujici modifikaci to funguje
Jen to jen test...
Kód: [Vybrat]
@Component
public class UserDao {
//
//    private final static List<UserDetails> APPLICATION_USERS = Arrays.asList(
//            new User("email@email","$2a$12$rEGy9.W4cVmwftdHed74quwkJVfPRZt1TgAsF8le9isOWvOWdlscW", Collections.singleton(new SimpleGrantedAuthority("ROLE_ADMIN"))),
//            new User("email2@email","$2a$12$rEGy9.W4cVmwftdHed74quwkJVfPRZt1TgAsF8le9isOWvOWdlscW", Collections.singleton(new SimpleGrantedAuthority("ROLE_VIEW"))),
//            new User("plain","plain", Collections.singleton(new SimpleGrantedAuthority("ROLE_VIEW")))
//    );


    public UserDetails findUserByEmail(String email){
        List<UserDetails> APPLICATION_USERS = Arrays.asList(
                new User("email@email","$2a$12$rEGy9.W4cVmwftdHed74quwkJVfPRZt1TgAsF8le9isOWvOWdlscW", Collections.singleton(new SimpleGrantedAuthority("ROLE_ADMIN"))),
                new User("email2@email","$2a$12$rEGy9.W4cVmwftdHed74quwkJVfPRZt1TgAsF8le9isOWvOWdlscW", Collections.singleton(new SimpleGrantedAuthority("ROLE_VIEW"))),
                new User("plain","plain", Collections.singleton(new SimpleGrantedAuthority("ROLE_VIEW")))
        );

        System.out.println("search user");
        System.out.println(APPLICATION_USERS.stream().filter(u->u.getUsername().equals(email)).findFirst());
        return APPLICATION_USERS.stream().filter(u->u.getUsername().equals(email)).findFirst().orElseThrow(()->new UsernameNotFoundException("User not found"));
    }
}

Ale netusim, proc to tak funguje....