puzhibing
2024-01-30 80b3ea5587ff7ec20541d9ca7c6c28739e4d615b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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);
    }
 
 
}