• Spring Security(三):直观体验OAuth2.0


    Spring Security(三):直观体验OAuth2.0​

    活动地址:CSDN21天学习挑战赛

    单点登录:Single Sign On(简称SSO)
    以前单系统时代,所有功能都在一起,登录一次就可以访问所有功能,随着业务的发展,系统越来越庞大,为了对合理的利用资源以及减少模块这间的耦合度,现在一般会把系统拆成不同的服务,或拆分成各个子系统。为了方便,各个子系统会互相授权,也就是只要在一个子系统上登录,那么在访问其它子系统时,就不需要在登录了。也就是说用户只要登录一次,就可以访问各个子系统,这就是单点登录。

    代码来源胖哥:https://gitee.com/felord/spring-security-oauth2-tutorial

    首先看一下项目结构

    在这里插入图片描述

    引入相关依赖

    • spring-boot-starter-oauth2-client:通过该依赖进行第三方授权
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-oauth2-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    • 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

    配置相关项

    application.yaml
    在这里插入图片描述
    application-multi.yaml

    • client-id和client-secret可以在你的gitee的设置->第三方应用->创建应用,创建完后会出现这个应用的client-id和client-secret
      在这里插入图片描述

    配置用户名和密码

    /**
     * @author felord.cn
     */
    @EnableWebSecurity(debug = true)
    public class UserDetailsServiceConfiguration {
        /**
         * 这里虚拟一个用户 felord 123456  随机密码
         *
         * @return UserDetailsService
         */
        @Bean
        UserDetailsService userDetailsService() {
            return username -> User.withUsername("felord")
                    .password("123456")
                    .authorities("ROLE_ADMIN", "ROLE_USER")
                    .build();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Api

    /**
     * 测试OAuth2的控制器
     *
     * @author felord.cn
     */
    @RestController
    public class FooController {
    
        /**
         * 获取当前的OAuth2 Client对象实例{@link OAuth2AuthorizedClient}
         * 和当前认证对象实例{@link Authentication}
         *
         * @param giteeOauth2client the gitee Oauth2 client
         * @return the map
         */
        @GetMapping("/foo/hello")
        public Map<String, Object> foo(@RegisteredOAuth2AuthorizedClient
                                               OAuth2AuthorizedClient giteeOauth2client) {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Map<String, Object> map = new HashMap<>(2);
            map.put("giteeOauth2client", giteeOauth2client);
            map.put("authentication", authentication);
            return map;
        }
    
        /**
         * 默认登录成功跳转页为 /  防止404状态
         *
         * @return the map
         */
        @GetMapping("/")
        public Map<String, String> index() {
            return Collections.singletonMap("msg", "oauth2Login success!");
        }
    }
    
    • 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

    结果

    授权成功后返回结果,也可以在你gitee的第三方应用中看到,已授权的应用
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    不知道不 OK!53 个 Python 经典面试题详解
    mac免费杀毒软件哪个好用?如何清理mac系统需要垃圾
    已解决 Java Error: Exception in thread ‘main‘ java.lang.ClassNotFoundException
    Flink Watermark 机制
    C++面试题(丝)-计算机网络部分(1)
    汉兰达汽车发动机怠速抖动故障诊断方案设计
    Docker安装部署,拉取镜像-详细过程
    2023国赛数学建模C题思路代码 - 蔬菜类商品的自动定价与补货决策
    【mysql体系结构】B+树索引
    PMAL: Open Set Recognition via Robust Prototype Mining
  • 原文地址:https://blog.csdn.net/u013010499/article/details/126207138