如果您使用的是 grant_type =“ password” ,则必须:
在自己的
WebSecurityConfigurerAdapter类中创建以下bean
@Override@Beanpublic AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean();}注入
AuthorizationServerConfigurerAdapter课堂
@Autowiredprivate AuthenticationManager authenticationManager;
在
configure(AuthorizationServerEndpointsConfigurer endpoints)方法中使用
@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) { endpoints.authenticationManager(authenticationManager);}完整示例:
@Configurationpublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Bean @Override protected UserDetailsService userDetailsService(){ InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); manager.createUser(User.withUsername("a").password("123456").authorities("USER").build()); return manager; }}@Configuration@EnableAuthorizationServerpublic class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { private AuthenticationManager authenticationManager; @Autowired public AuthorizationServerConfig(AuthenticationManager authenticationManager) { this.authenticationManager = authenticationManager; } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) { endpoints.authenticationManager(authenticationManager); } @Override public void configure(AuthorizationServerSecurityConfigurer security) { security.tokenKeyAccess("permitAll()") .checkTokenAccess("isAuthenticated()") .allowFormAuthenticationForClients(); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("CLIEN_ID").secret("CLIENT_SECRET") .authorizedGrantTypes("password", "refresh_token") .authorities("CLIENT") .scopes("read"); }}测试:
curl -i -X POST -d "username=a&password=123456&grant_type=password&client_id=CLIENT_ID&client_secret=CLIENT_SECRET" http://localhost:8080/oauth/token



