The main thing is to make separate endpoints for the auth server and resource-server so that they can serve them separately, each of them. As shown below, "/ user / getEmployeesListRole / **" -access through the auth server, "/ user / getEmployeesListOAuth2 / **" is access through the resource server using the token generated by the aouth2 server. Also note that auth-server and oauth2-server have the same auth manager
Configuring Spring-boot aouth2-server, resource-server, auth-server in a single spring-boot application
1. Entry point:
@SpringBootApplication
@EnableDiscoveryClient
@EnableGlobalMethodSecurity (prePostEnabled = true)
public class AuthApplication {
public static void main (String [] args) {
SpringApplication.run (AuthApplication.class, args);
}}
2. Configuration of aouth2 server:
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {
private TokenStore tokenStore = new InMemoryTokenStore ();
@Autowired
@Qualifier ("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Autowired
@Qualifier ("userDetailsServiceBean")
private UserDetailsService userDetailsService;
@Override
public void configure (ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory ()
.withClient ("browser")
.authorizedGrantTypes ("password", "refresh_token")
.scopes ("ui", "read: ui", "write: ui");
}
@Override public void configure (AuthorizationServerEndpointsConfigurer endpoints) throws an exception {endpoints.tokenStore (tokenStore) .authenticationManager (authenticationManager) .userDetailsService (userDetailsService);
@Override public void configure (AuthorizationServerSecurityConfigurer oauthServer) {oauthServer.tokenKeyAccess("allowAll()").checkTokenAccess("isAuthenticated()").passwordEncoder(NoOpPasswordEncost.In;
@Override public void configure (AuthorizationServerSecurityConfigurer oauthServer) {oauthServer.tokenKeyAccess("allowAll()").checkTokenAccess("isAuthenticated()").passwordEncoder(NoOpPasswordEncost.In;
2.1 aouth2-server authentication request [basic authentication message]:
http://localhost:5000/uaa/oauth/token?grant_type=password&scope=ui write:ui&username=user&password=123456&client_id=browser
3. Configuring the resource server:
@Configuration
@EnableResourceServer
class ResourceServer extends ResourceServerConfigurerAdapter {
// Here we specify to allow the request to the
// url / user / getEmployeesList with valid access token and scope read
@Override
public void configure (HttpSecurity http) throws Exception {
http.requestMatchers ()
.antMatchers ("/ user / getEmployeesList / ** ")
.antMatchers ("/ user / getEmployeesListOAuth2 / ** ")
.and (). authorizeRequests (). anyRequest (). access ("# oauth2.hasScope ('ui')");
}}
4. Config auth server:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/resources/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/user/getEmployeesListRole/**").access("hasAuthority('WRITE_DATA') && hasAuthority('READ_DATA')").anyRequest().permitAll().and().formLogin().permitAll().and().logout().permitAll().and().csrf().disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("admin").password("admin").authorities("WRITE_DATA", "READ_DATA"); } @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override @Bean public UserDetailsService userDetailsServiceBean() throws Exception { return super.userDetailsServiceBean(); } } @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/resources/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/user/getEmployeesListRole/**").access("hasAuthority('WRITE_DATA') && hasAuthority('READ_DATA')").anyRequest().permitAll().and().formLogin().permitAll().and().logout().permitAll().and().csrf().disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("admin").password("admin").authorities("WRITE_DATA", "READ_DATA"); } @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override @Bean public UserDetailsService userDetailsServiceBean() throws Exception { return super.userDetailsServiceBean(); } }