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 :)