密码模式需要注入一个AuthenticationManager
,于是咱们接着上篇授权码模式的文档的代码添加一点东西即可。
1.配置AuthenticationManager
Bean
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.builder()
.username("gaoxi")
.password("{noop}123456")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}
2.注入并设置AuthenticationManager
@Configuration
@EnableAuthorizationServer
public class AuthServerConfig implements AuthorizationServerConfigurer {
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// @formatter:off
clients.inMemory().withClient("gaoxi")
.secret("{noop}123456")
.scopes("read","write")
.authorizedGrantTypes("password","refresh_token",
"authorization_code","implicit","client_credentials")
.redirectUris("http://www.baidu.com");
// @fomatter:on
}
@Resource
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
}
3.Postman请求localhost:8080/oauth/token?grant_type=password&username=gaoxi&password=123456
注意Authorization中内容。