zoukankan      html  css  js  c++  java
  • 【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中了

  • 相关阅读:
    Documentum之基础(1)
    tomcat部署项目,war包问题[转]
    AD与LDAP区别
    Microsoft Visual Studio2010--此时无足够的可用内存,无法满足操作的预期要求,可能是由于虚拟地址空间碎片造成的。请稍后重试。
    Oracle 11g服务器安装详细步骤
    Oracle 11g 密码永不过期设置
    oracle 11g dbf数据文件从C盘迁移到D盘
    Swift 简介
    0Day 密码重置dede阿帕奇强制安装
    PS抠图教程_抠图小技巧_常用抠图小技巧_图片结构与配色
  • 原文地址:https://www.cnblogs.com/mindzone/p/13750701.html
Copyright © 2011-2022 走看看