【Spring-Security】Re11 Oauth2协议 P2 Redis存储 密码模式令牌

一、Redis配置

需要的依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

yml配置信息:

spring:
  redis:
    host: localhost

Redis的配置类:

package cn.zeal4j.configuration;

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.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * @author Administrator
 * @file Spring-Security + Oauth2
 * @create 2020 09 29 17:16
 */
@Configuration
public class RedisConfiguration {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public TokenStore getRedisTokenStore() {
        return new RedisTokenStore(redisConnectionFactory);
    }

}

注入到授权的密码模式方法中:

package cn.zeal4j.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
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.TokenStore;

/**
 * @author Administrator
 * @file Spring-Security + Oauth2
 * @create 2020 09 29 11:48
 * @description 授权服务器配置
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;
    @Qualifier("customUserDetailsServiceImpl")
    @Autowired
    private UserDetailsService userDetailsService;

    @Qualifier("getRedisTokenStore")
    @Autowired
    private TokenStore tokenStore;
    
    /**
     * 使用密码模式需要的配置方法
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.
                authenticationManager(authenticationManager).
                userDetailsService(userDetailsService).
                tokenStore(tokenStore);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.
                inMemory().
                withClient("admin").
                secret(passwordEncoder.encode("112233")).
                // accessTokenValiditySeconds(3600). // 令牌有效时间 一小时
                redirectUris("http://www.baidu.com"). // 授权成功的跳转
                scopes("all").  // 所有范围
                // authorizedGrantTypes("authorization_code");     // 授权类型:授权码模式
                authorizedGrantTypes("password");     // 授权类型:密码模式
    }
}

二、使用

还是使用密码模式授权

{
    "access_token": "ce5a8425-411a-4de7-8387-917d2ea6b2f6",
    "token_type": "bearer",
    "expires_in": 43199,
    "scope": "all"
}

这个时候可以打开Redis客户端查看:

Administrator@DESKTOP-D3S5169 MINGW64 ~/Desktop
$ redis-cli
127.0.0.1:6379> keys *
1) "uname_to_access:admin:admin"
2) "access:ce5a8425-411a-4de7-8387-917d2ea6b2f6"
3) "client_id_to_access:admin"
4) "auth_to_access:413f0c776eb9223fe9f8c47e020774ed"
5) "auth:ce5a8425-411a-4de7-8387-917d2ea6b2f6"
127.0.0.1:6379>

这个Token已经存到了Redis中了

 

posted @ 2020-09-29 17:26  emdzz  阅读(258)  评论(0编辑  收藏  举报