packagecom.example.oauth.config;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Configuration;importorg.springframework.data.redis.connection.RedisConnectionFactory;importorg.springframework.http.HttpMethod;importorg.springframework.security.authentication.AuthenticationManager;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.http.SessionCreationPolicy;importorg.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;importorg.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;importorg.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;importorg.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;importorg.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;importorg.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;importorg.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;importorg.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;importorg.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;importorg.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;@ConfigurationpublicclassOAuth2ServerConfig{privatestaticfinalString DEMO_RESOURCE_ID ="order";// 资源服务器配置@Configuration@EnableResourceServerprotectedstaticclassResourceServerConfigurationextendsResourceServerConfigurerAdapter{@Overridepublicvoidconfigure(ResourceServerSecurityConfigurer resources){
resources.resourceId(DEMO_RESOURCE_ID).stateless(true);}@Overridepublicvoidconfigure(HttpSecurity http)throwsException{// @formatter:off
http
// Since we want the protected resources to be accessible in the UI as well we need// session creation to be allowed (it's disabled by default in 2.0.6).sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED).and().requestMatchers().anyRequest().and().anonymous().and().authorizeRequests()// .antMatchers("/product/**").access("#oauth2.hasScope('select') and hasRole('ROLE_USER')").antMatchers("/private/**")//配置访问控制,必须认证过后才可以访问.authenticated();// @formatter:on}}// oauth 2 配置信息@Configuration@EnableAuthorizationServerprotectedstaticclassAuthorizationServerConfigurationextendsAuthorizationServerConfigurerAdapter{@AutowiredAuthenticationManager authenticationManager;@AutowiredRedisConnectionFactory redisConnectionFactory;@AutowiredprivateBCryptPasswordEncoder bCryptPasswordEncoder;@Overridepublicvoidconfigure(ClientDetailsServiceConfigurer clients)throwsException{//配置两个客户端,模拟第三方应用
clients.inMemory()// 凭证方式 , 适用于没有前端的命令行应用.withClient("client_1").resourceIds(DEMO_RESOURCE_ID).authorizedGrantTypes("client_credentials","refresh_token").scopes("select").authorities("client").secret(bCryptPasswordEncoder.encode("123"))// 密码的方式去认证.and().withClient("client_2").resourceIds(DEMO_RESOURCE_ID).authorizedGrantTypes("password","refresh_token").scopes("select")//作用域(Scopes): 客户请求访问令牌时,有资源拥有者额外指定的细分权限(permission).authorities("client").secret(bCryptPasswordEncoder.encode("123"));}@Overridepublicvoidconfigure(AuthorizationServerEndpointsConfigurer endpoints)throwsException{
endpoints
// token 的保存方式.tokenStore(newRedisTokenStore(redisConnectionFactory))// 允许 GET、POST 请求获取 token,即访问端点:oauth/token.allowedTokenEndpointRequestMethods(HttpMethod.GET,HttpMethod.POST)//token里加点信息// .tokenEnhancer(tokenEnhancerChain).authenticationManager(authenticationManager);}@Overridepublicvoidconfigure(AuthorizationServerSecurityConfigurer oauthServer)throwsException{//允许表单认证
oauthServer.allowFormAuthenticationForClients();}}}