spring-security-oauth2认证服务配置只需要在配置类中继承并重新三个configuration方法即可
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 public TokenStore redisTokenStore() { return new RedisTokenStore(redisConnectionFactory); } /** * 配置安全认证服务信息,如认证地址,获取access_token地址等 */ @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { super.configure(security); } /** * 配置ClientDetailsServiceConfigurer, * 重写这个配置方法之后系统默认的读取配置文件中的配置信息将会失效 */ @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(Map<String, Object> map : oauth2ConfigList) { clientDetailsServiceBuilder .withClient(String.valueOf(map.get("clientId"))) //配置clientId .secret(String.valueOf(map.get("clientSecret"))) //配置clientSecret .accessTokenValiditySeconds(Integer.valueOf(map.get("validitySeconds").toString())) //配置access_token有效时间 .authorizedGrantTypes((String[]) map.get("grantTypes")) //配置授权类型 .scopes((String[]) map.get("scopes")); //配置scope,配置了这个之后,请求中不能再加scope参数 } } /** * 这个配置方法可以配置终端服务器的一些非安全性的特性, * 如token存储、自定义token、用户信息处理逻辑等 */ @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints .tokenStore(redisTokenStore) //设置token存储在redis中 .authenticationManager(authenticationManager) //设置认证管理器 .userDetailsService(userDetailsService); //设置处理用户信息获取逻辑 } }
1-2 spring-security-oauth2认证服务配置
原文:https://www.cnblogs.com/programmlover/p/11374743.html