zoukankan      html  css  js  c++  java
  • springboot oauth 鉴权之——password鉴权相当于jwt鉴权模式

    近期一直在研究鉴权方面的各种案例,这几天有空,写一波总结及经验。

    第一步:什么是 OAuth鉴权

           OAuth2是工业标准的授权协议。OAuth2取代了在2006创建的原始OAuthTM协议所做的工作。OAuth 2.0关注客户端开发人员的简单性,同时为Web应用程序、桌面应用程序、移动电话和客厅设备提供特定的授权流。

         参考理解:  Oauth2.0     理解OAuth 2.0     QQ授权      微信授权

    第二步:什么是密码模式

           密码模式(Resource Owner Password Credentials Grant)中,用户向客户端提供自己的用户名和密码。客户端使用这些信息,向"服务商提供商"索要授权。

    第三步:密码权流程

    交互步骤: 
    (A)用户向客户端提供用户名和密码。 (B)客户端将用户名和密码发给认证服务器,向后者请求令牌。 (C)认证服务器确认无误后,向客户端提供访问令牌。

    第四步:密码模式实践

           源码地址:

                   github: https://github.com/GitHubZhangCom/spring-security-oauth-example/

                   码云:https://gitee.com/region/spring-security-oauth-example/

    第五:密码模式的核心代码唯一和授权码模式的区别在于,将授权码模式改为密码模式就ok。

           

    package com.auth.server.config;
    
    import javax.sql.DataSource;
    
    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.security.authentication.AuthenticationManager;
    import org.springframework.security.core.userdetails.UserDetailsService;
    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.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
    import org.springframework.security.oauth2.provider.token.TokenStore;
    import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
    
    /**
     * 授权配置
     * @author wb0024
     *
     */
    @Configuration
    @EnableAuthorizationServer
    public class ServerConfig extends AuthorizationServerConfigurerAdapter {
        @Autowired
        private AuthenticationManager authenticationManager;
    
    
        @Qualifier("myUserDetailsService")
        @Autowired
        private UserDetailsService userDetailsService;
    
    //    @Autowired
    //    @Qualifier("dataSource")
    //    private DataSource dataSource;
    
        @Override
        public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
            // 配置token获取和验证时的策略
            security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
        }
    
        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.inMemory()
                    .withClient("client")
                    // secret密码配置从 Spring Security 5.0开始必须以 {加密方式}+加密后的密码 这种格式填写
                    
                    /* *   当前版本5新增支持加密方式:
                     *   bcrypt - BCryptPasswordEncoder (Also used for encoding)
                     *   ldap - LdapShaPasswordEncoder
                     *   MD4 - Md4PasswordEncoder
                     *   MD5 - new MessageDigestPasswordEncoder("MD5")
                     *   noop - NoOpPasswordEncoder
                     *   pbkdf2 - Pbkdf2PasswordEncoder
                     *   scrypt - SCryptPasswordEncoder
                     *   SHA-1 - new MessageDigestPasswordEncoder("SHA-1")
                     *   SHA-256 - new MessageDigestPasswordEncoder("SHA-256")
                     *   sha256 - StandardPasswordEncoder*/
                     
                    .secret("{noop}secret")
                    .scopes("all")
                    .authorizedGrantTypes("password", "refresh_token")//password模式
                    //.authorizedGrantTypes("authorization_code", "password", "refresh_token")//授权码模式和password模式
                    //.authorizedGrantTypes("authorization_code", "refresh_token")//授权码模式
                    .autoApprove(true);
        }
    
        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    //        // 配置tokenStore,保存到redis缓存中
    //        endpoints.authenticationManager(authenticationManager)
    //                .tokenStore(new MyRedisTokenStore(redisConnectionFactory))
    //                // 不添加userDetailsService,刷新access_token时会报错
    //                .userDetailsService(userDetailsService);
    
            // 使用最基本的InMemoryTokenStore生成token
            endpoints.authenticationManager(authenticationManager).tokenStore(memoryTokenStore());
    
        }
    
        // 使用最基本的InMemoryTokenStore生成token
        @Bean
        public TokenStore memoryTokenStore() {
            return new InMemoryTokenStore();
        }
    }

    详情请看源码!!!

  • 相关阅读:
    jquery 学习(五)
    iOS: 学习笔记, Swift名字空间
    iOS: 学习笔记, Swift与C指针交互(译)
    iOS: 学习笔记, Swift运算符定义
    iOS: 学习笔记, Swift与Objective-C混用总结
    iOS: 学习笔记, Swift与Objective-C混用简明教程(转载)
    股票查询接口: 腾讯
    iOS: 学习笔记, swift扩展
    iOS:Swift界面实例1, 简单界面
    iOS: 学习笔记, 用代码驱动自动布局实例(swift)
  • 原文地址:https://www.cnblogs.com/haoliyou/p/9606036.html
Copyright © 2011-2022 走看看