Я пытаюсь узнать о типе предоставления пароля, но самая простая демонстрация не удалась по странной причине:
o.s.web.servlet.PageNotFound: Метод запроса 'POST' не поддерживается
примечание: логгер pageNotFoundLogger
в DispatcherServlet.java
Я пытался обновить свои зависимости, чтобы избежать этой проблемы, но не смог.
build.gradle, который я использовал:
buildscript {
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.6.RELEASE"
}
repositories {
mavenCentral()
}
}
repositories {
mavenCentral()
}
allprojects {
apply plugin: "java"
apply plugin: "io.spring.dependency-management"
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-netflix:2.0.1.RELEASE'
}
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.4.RELEASE'
compile 'org.springframework.cloud:spring-cloud-starter-oauth2:2.0.0.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2
compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.3.3.RELEASE'
}
}
И основной класс:
@RestController
@SpringBootApplication
public class OAuth2App {
private static final Logger LOG = LoggerFactory.getLogger(OAuth2App.class);
public static void main(String[] args) {
SpringApplication.run(OAuth2App.class, args);
}
@GetMapping
public String whoIam(Principal principal) {
LOG.info("I'm {}", principal.getName());
return principal.getName();
}
@Configuration
public static class PasswordEncoderConfig {
@SuppressWarnings("deprecation")
@Bean
public PasswordEncoder passwordEncoder() {
// To avoid IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
return NoOpPasswordEncoder.getInstance();
}
}
@Configuration
public static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final PasswordEncoder passwordEncoder;
@Autowired
public WebSecurityConfig(PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.eraseCredentials(true)
.inMemoryAuthentication()
.passwordEncoder(passwordEncoder)
.withUser("user")
.password(passwordEncoder.encode("password"))
.roles("USER");
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
@Configuration
@EnableAuthorizationServer
public static class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
private final AuthenticationManager authenticationManager;
@Autowired
public AuthorizationServerConfig(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
super.configure(clients);
clients.inMemory()
.withClient("client")
.secret("secret") // not enhanced by password encoder yet
.authorizedGrantTypes("password")
.scopes("scope");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
super.configure(endpoints);
endpoints.authenticationManager(authenticationManager);// To enable 'password' grant_type
}
}
}
И когда я использую post man для проверки POST http :: // client: secret @ localhost: 8080 / oauth / token? Grant_type = пароль & username = user & password = пароль, он отвечает
{
"timestamp": "2018-08-27T08:11:09.396+0000",
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'POST' not supported",
"path": "/oauth/token"
}
Что не так?