I'm trying to create a sample authorization server using the spring security oauth2 framework. The tutorials are confusing compared to any other spring related examples.
Update: If you are looking for a working solution, go to my answer. Ignore the code below.
When I invoked the token issue endpoint, the following error was thrown
{
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource"
}
Here's my setup (using Groovy). I'm using spring-security-oauth2:2.3.4.RELEASE, spring-cloud-security:2.0.1.RELEASE, and boot:2.1.1.RELEASE.
@Configuration
@CompileStatic
class OAuth2ClientSpringConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
AuthenticationManager authenticationManager
@Autowired
UserDetailsService userDetailsService
// register clients
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient('clientone')
.secret('secret')
.authorizedGrantTypes('password')
.scopes('one', 'two')
}
//use default auth manager and user details service
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService)
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.allowFormAuthenticationForClients()
security.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()")
.allowFormAuthenticationForClients() //<--- update
}
}
Static user credentials
@Configuration
@CompileStatic
class UserCredentialsSpringConfig extends WebSecurityConfigurerAdapter {
@Bean
AuthenticationManager authenticationManagerBean() {
super.authenticationManagerBean()
}
@Bean
UserDetailsService userDetailsServiceBean() {
super.userDetailsServiceBean()
}
protected void configure(AuthenticationManagerBuilder auth) {
auth.inMemoryAuthentication()
.withUser('user1').password('pwd1').roles('USER')
.and()
.withUser('admin').password('pwd1').roles('ADMIN')
}
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/oauth/token").permitAll()
.anyRequest().authenticated()
}
}
Postman setup
- Added the client credentials in both places - Authorization header and in a normal header.
- Used the body form params to send user credentials
Ran into the following error when I hit http://localhost:8080/auth/oauth/token
I've looked into different tutorials and not figured it out. Any inputs would be helpful.