SpringSecurity Oauth2 整合JWT

153 阅读1分钟

整合 JWT

拿之前Spring Security Oauth2的完整代码进行修改添加配置文件JwtTokenStoreConfig.java
/**
 * @author yiming
 * @description
 */
@Configuration
public class JwtTokenStoreConfig {

    @Bean
    public TokenStore jwtTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /**
     * 将生成的access token 转成jwttoken
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        // 设置jwt密钥 !!
        jwtAccessTokenConverter.setSigningKey("test_key");
        return jwtAccessTokenConverter;
    }

    @Bean
    public JwtTokenEnhancer jwtTokenEnhancer() {
        return new JwtTokenEnhancer();
    }
}
在认证服务器配置中指定令牌的存储策略为JWT
/**
 * @author yiming
 * @description 配置授权服务器
 */
@Configuration
@EnableAuthorizationServer // 开启授权服务器
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserService userService;

    @Autowired
    @Qualifier("jwtTokenStore")
    private TokenStore tokenStore;

    @Autowired
    private JwtAccessTokenConverter jwtAccessTokenConverter;

    @Autowired
    private JwtTokenEnhancer tokenEnhancer;

    /**
     * 密码模式
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        // 设置jwt增强内容
        TokenEnhancerChain chain = new TokenEnhancerChain();
        List<TokenEnhancer> delegates = new ArrayList<>();
        delegates.add(jwtAccessTokenConverter);
        delegates.add(jwtAccessTokenConverter);
        chain.setTokenEnhancers(delegates);

        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userService)
                // accessToken转成了JWTtoken
                .tokenStore(tokenStore)
                .accessTokenConverter(jwtAccessTokenConverter)
                .tokenEnhancer(chain);
    }

    /**
     * 授权码模式
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // 客户端放在内存中
        clients.inMemory()
                // 客户端id
                //配置client_id
                .withClient("client")
                // 密钥
                //配置client-secret
                .secret(passwordEncoder.encode("112233"))
                // accessToken失效时间
                //配置访问token的有效期
                .accessTokenValiditySeconds(60)
                //配置刷新token的有效期
                .refreshTokenValiditySeconds(864000)
                // 重定向地址: 拿授权码
                //配置redirect_uri,用于授权成功后跳转
                .redirectUris("http://localhost:8081/login")
                //配置申请的权限范围
                // 授权范围
                .scopes("all")
                // 自动授权
                .autoApprove(true)
                // 授权类型
                // authorization_code 授权码模式
                // password 密码模式
                // refresh_token 刷新令牌
                //配置grant_type,表示授权类型
                .authorizedGrantTypes("authorization_code", "password", "refresh_token");
    }


    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        // 必须要身份认证,配置单点登录必须要配置
        security.tokenKeyAccess("isAuthenticated()");
    }
}