package cn.mb.cloud.auth.config; import cn.mb.cloud.auth.service.UserDetailsServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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.crypto.password.PasswordEncoder; 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.provider.token.AuthorizationServerTokenServices; import org.springframework.security.oauth2.provider.token.DefaultTokenServices; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; /** * 授权服务器 * @author pzb * @Date 2022/11/2 19:56 */ @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private PasswordEncoder passwordEncoder; @Autowired private AuthenticationManager authenticationManager; @Autowired private UserDetailsServiceImpl userDetailsServiceImpl; @Autowired private JwtAccessTokenConverter jwtAccessTokenConverter; @Autowired private TokenStore jwtTokenStore; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory()//内存中 .withClient(SecurityEnum.clinet_id)//客户端ID .secret(passwordEncoder.encode(SecurityEnum.secret_key))//秘钥 .redirectUris("https://www.bilibili.com")//重定向到的地址 .scopes(SecurityEnum.scopes_all)//授权范围 .authorizedGrantTypes(SecurityEnum.password, SecurityEnum.refresh_token)//密码模式和刷新令牌模式 .accessTokenValiditySeconds(SecurityEnum.token_effective_time)//令牌有效期 .refreshTokenValiditySeconds(SecurityEnum.refresh_token_effective_time);//刷新令牌有效期 } //密码模式需要配置 @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager) .userDetailsService(userDetailsServiceImpl) .tokenStore(jwtTokenStore) .accessTokenConverter(jwtAccessTokenConverter); } }