Я пытался реализовать и понять oauth2 при весенней загрузке, но даже в простых реализациях с inMemory () и inMemoryAuthentication (), как это, я всегда получаю 401 несанкционированный в oauth / token. Может ли кто-нибудь помочь мне? Я уже теряю надежду, хе-хе
В настоящее время использую Spring Boot 2.2.5.RELEASE
AuthorizationConfigurerAdapter:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfigurations extends AuthorizationServerConfigurer {
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
@Autowired
public void configure(ClientDetailsServiceConfigurer client) throws Exception {
client.inMemory().withClient("client")
.secret("secret")
.authorizedGrantTypes("password","authorization_code", "refresh_token")
.scopes("read");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoint) throws Exception {
endpoint.authenticationManager(authenticationManager);
}
}
WebSecurityConfigurerAdapter:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
@Autowired
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication()
.withUser("john").password(passwordEncoder().encode("123")).roles("USER");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean()
throws Exception {
return super.authenticationManagerBean();
}
}
Main:
@SpringBootApplication
@EnableAuthorizationServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Я посылаю в Почтальон или Бессонницу следующий запрос:
В основном c авторизация:
ИМЯ ПОЛЬЗОВАТЕЛЯ = ПАРОЛЬ клиента = секрет
В теле (форма):
grant_type = пароль username = john password = 123
и получить
{
"timestamp": "2020-02-29T04:26:43.930+0000",
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/oauth/token"
}