Spring - Nieskończone wywoływanie logowania BasicAuth - Spring Security

0

Krótka historia, zmarnowany kolejny weekend i wciąż niedziałająca aplikacja springowa. Nie wiem jak to się dzieje, że spring jest taki popularny, a w internecie 0 aktualnych poradników, całe dwa monitory w ikonach projektów pobranych z githuba i ani jeden się nie odpalił :(
Koniec płakania i do rzeczy. Udało mi się ulepić projekt z kilku stron w języku z takimi znakami: "লেখা উৎসব-২০১৮" w całość, a przynajmniej tak mi się wydaje.

po zapytaniu

http://localhost:8080/oauth/token?grant_type=password

i ustawieniu basic auth z danymi admin/admin w postman (mam takie konto w MySQL)
powinnam otrzymac token, jednak strona cały czas prosi mnie o logowanie do BasicAuth w nieskończoność.

Jestem pewna że to dla kogoś drobnostka, jednak jest to moja 1 aplikacja w springu i nie mam znajomych programistów javy aby się zapytać, co tu jest źle, lub czego brakuje.

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/**").authenticated();
    }
}
@Service
public class CustomUserDetailsService implements UserDetailsService {

    private final UserController userController;

    @Autowired
    public CustomUserDetailsService(UserController userController) {
        this.userController = userController;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return this.userController.find(username);
    }
}
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()")
                .allowFormAuthenticationForClients();
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("android-client")
                .authorizedGrantTypes("client-credentials", "password", "refresh_token")
                .authorities("ROLE_CLIENT", "ROLE_ANDROID_CLIENT")
                .scopes("read", "write", "trust")
                .resourceIds("oauth2-resource")
                .accessTokenValiditySeconds(5000)
                .secret("android-secret").refreshTokenValiditySeconds(50000);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)
                .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
    }
}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

Oprócz tego mam jeszcze oczywiście główną klase która odpala aplikacje, klase User - model użytkownika z haslem i loginem która implementuje UserDetails + UserRepository rozszerzającą JpaRepository i UserController która ma metody CRUD i adnotację RequestMapping do wystawienia api.

Jeżeli chcecie zobaczyć kod z innej klasy, dajcie znać, nie chciałam wklejać wszystkiego, bo post byłby za duży :)

1

A to nie jest czasami tak, że dostęp do /oauth/token jest zablokowany poprzez poniższą konfigurację?

	.authorizeRequests()
	.antMatchers("/**").authenticated();

Edit: Tutaj jest tutorial w którym jest podobny kod:
https://jugbd.org/2017/09/19/implementing-oauth2-spring-boot-spring-security/

Jak widać dostęp jest chroniony tylko do jakiegoś api które chcemy wystawić:

   .antMatchers("/api/v1/**").authenticated();

Kolejna sprawa to czy w requeście wysyłasz nazwę użytkownika i hasło i odpowiedni typ metody (POST)? W tutorialu masz tak:

Url: http://localhost:8080/oauth/token (POST)
Params:
grant_type = password
username = users email
password = users password
Authorization Type: Basic

Edit2: jeszcze jedno przychodzi mi do głowy - nie masz połączonego UserDetailsService z AuthenticationManager, zrób. np tak

@Autowired
public void authenticationManager(AuthenticationManagerBuilder builder) throws Exception {
    builder.userDetailsService(userDetailsService);
}
0

@Seti87:

po zmianie:

@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
  @Override
  public AuthenticationManager authenticationManagerBean() throws Exception {
      return super.authenticationManagerBean();
  }

na

@Autowired
    CustomUserDetailsService customUserDetailsService;
    
    @Autowired
    public void authenticationManager(AuthenticationManagerBuilder builder) throws Exception {
        builder.userDetailsService(customUserDetailsService);
    }

zmienna "authenticationManager" w AuthorizationManager jest podkeślona z informacją "Could not autowire. No beans of "AuthenticationManager" type found.

1 użytkowników online, w tym zalogowanych: 0, gości: 1