spring-security-oauth2 configured authentication services only need to inherit the class in the configuration and re-configuration method to three
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.oauth2.config.annotation.builders.InMemoryClientDetailsServiceBuilder; import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore; @Configuration @EnableAuthorizationServer public class MyAuthenticationServerConfig extends AuthorizationServerConfigurerAdapter{ @Autowired private RedisConnectionFactory redisConnectionFactory; @Autowired private UserDetailsService userDetailsService; @Autowired private AuthenticationManager authenticationManager; @Autowired private TokenStore redisTokenStore; @Bean publicRedisTokenStore TokenStore () { return new new RedisTokenStore (redisConnectionFactory); } / ** * security authentication service configuration information, such as addresses authentication, access access_token address * / @Override public void Configure (AuthorizationServerSecurityConfigurer Security) throws Exception { Super .configure (Security ); } / ** * configuration ClientDetailsServiceConfigurer, * after rewriting the configuration information to configure the system reads the default configuration file will fail * / @Override public void configure (ClientDetailsServiceConfigurer Clients) throws Exception { List<Map<String, Object>> oauth2ConfigList = new ArrayList<>(); Map<String, Object> map0 = new HashMap<String, Object>(); map0.put("clientId", "dj0"); map0.put("clientSecret", "dj0"); map0.put("validitySeconds", 7200); map0.put("grantTypes", new String[] {"refresh_token","password"}); map0.put("scopes", new String[] {"all", "read","write"}); oauth2ConfigList.add(map0); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("clientId", "dj1"); map1.put("clientSecret", "dj1"); map1.put("validitySeconds", 3600); map1.put("grantTypes", new String[] {"refresh_token", "password", "authorization_code"}); map1.put("scopes", new String[] {"read","write"}); oauth2ConfigList.add(map1); InMemoryClientDetailsServiceBuilder clientDetailsServiceBuilder = clients.inMemory(); for(The Map <String, Object> Map: oauth2ConfigList) { clientDetailsServiceBuilder .withClient (String.valueOf (as map.get ( "clientId"))) // configuration clientId .secret (String.valueOf (as map.get ( "clientSecret")) ) // configure clientSecret .accessTokenValiditySeconds (Integer.valueOf (as map.get ( "validitySeconds"). toString ())) // configuration access_token effective time .authorizedGrantTypes ((String []) as map.get ( "grantTypes")) / / configuration authorization type .scopes ((String []) as map.get ( "Scopes")); // after scope configuration, this configuration, the request can not be coupled with the scope parameter } } / ** * This configuration can configure some security features of non-terminal server, * such as token storage, custom token, the user information processing logic, etc. * / @Override public void Configure (AuthorizationServerEndpointsConfigurer Endpoints) throws Exception { Endpoints .tokenStore (redisTokenStore) // set the token stored in redis .authenticationManager (the authenticationManager) // set the authentication manager .userDetailsService (userDetailsService); // fetch logic setting the user information } }