Spring Security OAuth2 SSO 单点登录

SpringCloud+SpringBoot+SSO单点登录之OAuth2.0登录认证(1) 之前写了很多关于spring cloud的文章,今天我们对OAuth2.0的整合方式做一下笔记,首先我从网上找了一些关于OAuth2.0的一些基础知识点,帮助大家回顾一下知识点: 一、oauth中的角色 client:调用资源服务器API的应用 Oauth 2.0 Provider:包括Authorization Server和Resource Server (1)Authorization Server:认证服务器,进行认证和授权 (2)Resource Server:资源服务器,保护受保护的资源 use 阅读详情

基于 Spring Security OAuth2 SSO 单点登录系统

SSO简介

单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自独立的软件系统,提供访问控制的属性。当拥有这项属性时,当用户登录时,就可以获取所有系统的访问权限,不用对每个单一系统都逐一登录。这项功能通常是以轻型目录访问协议(LDAP)来实现,在服务器上会将用户信息存储到LDAP数据库中。相同的,单一退出(single sign-off)就是指,只需要单一的退出动作,就可以结束对于多个系统的访问权限。

Spring Security OAuth

Spring Security OAuth使用标准的Spring和Spring Security编程模型和配置惯例,为使用Spring Security with OAuth(1a)和OAuth2提供支持。OAuth协议

案例介绍

此工程分为三个模块:授权服务器(sso-auth-server)、web应用a(sso-client-a)、web应用b(sso-client-b),想达到的目的是:某一个用户在a系统登陆后在跳往b系统后不用在重复登录。

  • sso-auth-server:

    • pom:
    <dependencies>
        <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.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
        </dependency>
    </dependencies>
    
    • yml:
    server:
      port: 8082
      context-path: /auth_server
    
    • SsoServerApplication.java
    /**
     * @author Leone
     * @since 2018-05-07
     **/
    @SpringBootApplication
    public class SsoServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SsoServerApplication.class, args);
        }
    
        /**
         * 为测试环境添加相关的 Request Dumper information,便于调试
         *
         * @return
         */
        @Profile("!cloud")
        @Bean
        RequestDumperFilter requestDumperFilter() {
            return new RequestDumperFilter();
        }
    
    }
    
    • userDetailsService.java
    /**
     * @author Leone
     * @since 2018-05-07
     **/
    @Component
    public class SsoUserDetailsService implements UserDetailsService {
    
        @Autowired
        private PasswordEncoder passwordEncoder;
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            return new User(username, passwordEncoder.encode("admin"), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
        }
    }
    
    • SsoSecurityConfig.java
    /**
     * @author Leone
     * @since 2018-05-07
     **/
    @Configuration
    public class SsoSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private UserDetailsService userDetailsService;
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.formLogin()
                    .and().authorizeRequests()
                    .antMatchers("/**/*.js", "/**/*.css", "/**/*.jpg", "/**/*.png")
                    .permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .csrf().disable();
            //  http.formLogin().and().authorizeRequests().anyRequest().authenticated();
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
        }
    }
    
    • SsoAuthServerConfig.java
    /**
     * @author Leone
     * @since 2018-05-07
     **/
    @Configuration
    @EnableAuthorizationServer
    public class SsoAuthServerConfig extends AuthorizationServerConfigurerAdapter {
    
        /**
         * 客户端一些配置
         *
         * @param clients
         * @throws Exception
         */
        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.inMemory()
                    .withClient("client1")
                    .secret("secret1")
                    .authorizedGrantTypes("authorization_code", "refresh_token")
                    .scopes("all", "read", "write")
                    .autoApprove(true)
                    .and()
                    .withClient("client2")
                    .secret("secret2")
                    .authorizedGrantTypes("authorization_code", "refresh_token")
                    .scopes("all", "read", "write")
                    .autoApprove(true);
        }
    
        /**
         * 配置jwtTokenStore
         *
         * @param endpoints
         * @throws Exception
         */
        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints.tokenStore(jwtTokenStore()).accessTokenConverter(jwtAccessTokenConverter());
        }
    
        /**
         * springSecurity 授权表达式
         *
         * @param security
         * @throws Exception
         */
        @Override
        public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
            security.tokenKeyAccess("isAuthenticated()");
        }
    
        /**
         * JwtTokenStore
         *
         * @return
         */
        @Bean
        public TokenStore jwtTokenStore() {
            return new JwtTokenStore(jwtAccessTokenConverter());
        }
    
        /**
         * 生成JTW token
         *
         * @return
         */
        @Bean
        public JwtAccessTokenConverter jwtAccessTokenConverter() {
            JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
            converter.setSigningKey("andy");
            return converter;
        }
    }
    
  • sso-client-a

    • pom:
    <dependencies>
        <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.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
        </dependency>
    
    </dependencies>
    
    • yml:
    server:
      port: 8080
      context-path: /clienta
    security:
      oauth2:
        client:
          clientId: client1
          clientSecret: secret1
          access-token-uri: http://127.0.0.1:8082/auth_server/oauth/token    #请求令牌的地址
          user-authorization-uri: http://127.0.0.1:8082/auth_server/oauth/authorize    #请求认证的地址
        resource:
          jwt:
            key-uri: http://127.0.0.1:8082/auth_server/oauth/token_key   #解析jwt令牌所需要密钥的地址
    
    
    • index.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>sso-client-A</title>
    </head>
    <body>
        <h1>sso demo client-A</h1>
        <a href="http://127.0.0.1:8081/clientb/index.html">访问client-b</a>
    </body>
    </html>
    
    • SsoClientA.java
    /**
     * @author Leone
     * @since 2018-05-07
     **/
    @EnableOAuth2Sso
    @SpringBootApplication
    public class SsoClientA {
        public static void main(String[] args) {
            SpringApplication.run(SsoClientA.class, args);
        }
    }
    
  • sso-client-b

    • pom:
    <dependencies>
        <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.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
        </dependency>
    
    </dependencies>
    
    • yml:
    server:
      port: 8081
      context-path: /clientb
    security:
      oauth2:
        client:
          clientId: client2
          clientSecret: secret2
          access-token-uri: http://127.0.0.1:8082/auth_server/oauth/token
          user-authorization-uri: http://127.0.0.1:8082/auth_server/oauth/authorize
        resource:
          jwt:
            key-uri: http://127.0.0.1:8082/auth_server/oauth/token_key
    
    
    • index.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>sso-client-B</title>
    </head>
    <body>
        <h1>sso demo client-B</h1>
        <a href="http://127.0.0.1:8080/clienta/index.html">访问client-a</a>
    </body>
    </html>
    
    • SsoClientA.java
    /**
     * @author Leone
     * @since 2018-05-07
     **/
    @RestController
    @EnableOAuth2Sso
    @SpringBootApplication
    public class SsoClientB {
    
        @Autowired
        private OAuth2RestTemplate oAuth2RestTemplate;
    
        public static void main(String[] args) {
            SpringApplication.run(SsoClientB.class, args);
        }
    
        @GetMapping("/user")
        public Authentication user(Authentication user) {
            return user;
        }
    
        @Bean
        public OAuth2RestTemplate oAuth2RestTemplate(OAuth2ClientContext oAuth2ClientContext, OAuth2ProtectedResourceDetails details){
            return new OAuth2RestTemplate(details,oAuth2ClientContext);
        }
    }
    

项目源码:git@github.com:janlle/sso-server.git

spring authorization server sso android 登录演示 spring authorization server 0.3.1 sso Android端的登录演示 阅读详情

相关推荐

微服务安全——OAuth2.1详解、授权码模式、SpringAuthorizationServer实战、SSO单点登录、Gateway整合OAuth2

Spring Authorization Server 是一个框架,它提供了OAuth 2.1和规范以及其他相关规范的实现。它建立在 Spring Security 之上,为构建 OpenID Connect 1.0 身份提供者和 OAuth2 授权服务器产品提供了一个安全、轻量级和可定制的基础。说白了,Spring Authorization Server 就是一个认证(授权)服务器。

web15117360223的博客 2425

spring authorization server 0.3.1 sso

技术栈spring Authorization server、arco、midway、redis等,前后端分离客户端、纯前端客户端实现的sso示例

何虎军 1443

Spring Security OAuth2 SSO单点登录实战案例

在当今的网络应用中,安全性是一个不断演进的话题,尤其在用户数据保护和系统安全方面。Spring Security OAuth2 是一个强大的框架,用于构建认证和授权服务,它提供了全面的安全解决方案。本章将为您概述Spring Security OAuth2,包括它的核心功能、主要组件以及如何在应用程序中使用OAuth2协议来加强安全性能。

weixin_42584586的博客 910

SpringBoot3集成Spring Authorization Server实现SSO单点登录

在之前的文章中介绍过SpringBoot集成OAuth2老版本的方案,随着Spring Authorization Server框架的成熟和SpringBoot版本的更新,新项目必然会采用新的技术和框架,本文将使用最新的SpringBoot版本和JDK17实现SSO单点登录

liu320yj的博客 2958

CST周期边界仿真:揭秘频率选择表面(FSS)单元设计全流程

本文详细解析了CST周期边界仿真在频率选择表面(FSS)单元设计中的应用全流程。从理论基础到工程实践,涵盖了求解器选择、几何建模、边界设置、端口优化等关键步骤,并提供了常见问题排查指南。通过CST仿真技术,工程师能够高效预测FSS的电磁特性,为实际应用提供可靠的设计依据。

weixin_30571465的博客 360

Abaqus 2023 保姆级安装避坑指南:从环境变量到许可证配置,一次搞定

本文提供Abaqus 2023的详细安装指南,涵盖环境变量设置、许可证配置等关键步骤,帮助用户避开常见安装陷阱。通过系统环境检查、目录结构规划、许可证服务器安装及Java环境配置等实用技巧,确保顺利完成安装并优化软件性能。

weixin_42524276的博客 426

使用Spring Security OAuth2实现单点登录(SSO)系统

一、单点登录SSO介绍   目前每家企业或者平台都存在不止一套系统,由于历史原因每套系统采购于不同厂商,所以系统间都是相互独立的,都有自己的用户鉴权认证体系,当用户进行登录系统时,不得不记住每套系统的用户名密码,同时,管理员也需要为同一个用户设置多套系统登录账号,这对系统的使用者来说显然是不方便的。我们期望的是如果存在多个系统,只需要登录一次就可以访问多个系统,只需要在其中一个系统执行注销登录操作,则所有的系统都注销登录,无需重复操作,这就是单点登录(Single Sign On 简称SSO)系统实现的功

一瓶绿茶三元钱 1万+

如何下载安装Qt各个版本?(2025-4-28更新)

本文将教您快速获取Qt各版本安装包!妈妈再也不用担心我找不到Qt安装包了!

程序区 5119

Spring Security基于Oauth2的SSO单点登录怎样做?一个注解搞定

一、说明 单点登录顾名思义就是在多个应用系统中,只需要登录一次,就可以访问其他相互信任的应用系统,免除多次登录的烦恼。本文主要介绍 同域 和 跨域 两种不同场景单点登录的实现原理,并使用 Spring Security 来实现一个最简单的跨域 SSO客户端 。 二、原理说明 单点登录主流都是基于共享 cookie 来实现的,下面分别介绍 同域 和 跨域 下的两种场景具体怎样实现共享 c...

weixin_36438989的博客 1052

GPD源码分析

gpd使用示例 先看下面一段代码,为标准gpd算法的使用方法,可以看到输入为点云,cloud_camera_; 输入:std::unique_ptr<gpd::util::Cloud> cloud_camera_ 输出: std::vector<std::unique_ptr<gpd::candidate::Hand>> grasps; // detect grasp poses gpd对象:std::unique_ptr<gpd::GraspDetect.

weixin_41003113的博客 2334

vscode+keil嵌入式软件开发全流程

vscode+keil嵌入式软件开发全流程

AspringQ的博客 6785

基于Spring Security OAuth2的SSO(单点登录)

基础理论 What is SSO? 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。 What is Spring Security? Spring Security官方解释 What is OAuth2? 首先简单了解一下用于验证及授权的OAuth协议, ...

Jake Weng 1万+

【100%通过率】华为OD机试真题 JS 实现【水库蓄水问题】【2023 Q1 | 100分】

给定一个长度为 n 的整数数组height。数组的元素表示山的高度,选择两个元素作为水库的边界,求蓄水量的最大值并输出蓄水量最大时的边界下标(蓄水量相同时输出下标较近的)。

MISAYAONE的博客 8173

基于Spring Security + OAuth2 的SSO单点登录(服务端)

理论知识 spring security OAuth2 JWT 传统登录 登录web系统后将用户信息保存在session中,sessionId写入浏览器的cookie中,每次访问系统,浏览器自动携带此cookie,服务端根据此sessionId取到相应的session,若为空则表示登录已失效,不为空则表示用户已登录,不需要用户再次输入用户名密码。 单点登录 单点登录是一种多站点...

Janche的博客 4248

5、数据库基础概念与数据建模全解析

本文深入解析了数据库的基础概念,包括函数依赖、决定因素和多值依赖等关键依赖关系,同时探讨了数据建模的重要方法论,如IDEF1X、信息工程和陈方法。此外,还介绍了SQL Server管理工作室的图表功能,帮助用户进行数据库的可视化设计。通过本文,读者将掌握数据库设计的核心知识,为构建高效、可扩展的数据库系统奠定基础。

81

万字数据仓库面试题及参考答案

星型模型由一个中心的事实表和多个外围的维度表组成,事实表存储度量值和事实,维度表存储描述性属性和维度。星型模型和雪花模型的区别在于雪花模型将维度表进一步细化,形成多层次的结构,而星型模型由一个中心事实表和多个维度表组成,适用于查询较为简单的情况。维度建模通常采用星型模型或雪花模型,将数据分为事实表和维度表,其中事实表存储度量值和事实,维度表存储描述事实的属性。三者之间的关系可以这样理解:数据仓库主要关注历史数据的存储和分析,数据中台则关注数据的服务化和资产化,而数据湖则关注原始数据的存储和多样性。

大模型大数据攻城狮的专栏 560

Spring Cloud Security:Oauth2 实现SSO单点登录

Spring Cloud Security:Oauth2实现单点登录

Lyndon的专栏 2999

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

【Spring Security OAuth2】- App认证框架- 基于JWT实现SSO单点登录2

由于登录页面是在认证服务器上,所以修改认证服务器配置;其他的个性化配置,自己以后根据业务去细化@Autowired@Bean@Overridehttp.formLogin() // 更改为form表单登录.and()// 所有的请求都必须授权后才能访问;@Override自定义用户信息;无非就是之前讲过的自定义 userDetailsService/***/@Component@Autowired@Override。

smart_an的专栏 1204

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

SpringCloud微服务实战——搭建企业级开发框架(四十):使用Spring Security OAuth2实现单点登录(SSO)系统

一、单点登录SSO介绍   目前每家企业或者平台都存在不止一套系统,由于历史原因每套系统采购于不同厂商,所以系统间都是相互独立的,都有自己的用户鉴权认证体系,当用户进行登录系统时,不得不记住每套系统的用户名密码,同时,管理员也需要为同一个用户设置多套系统登录账号,这对系统的使用者来说显然是不方便的。我们期望的是如果存在多个系统,只需要登录一次就可以访问多个系统,只需要在其中一个系统执行注销登录操作,则所有的系统都注销登录,无需重复操作,这就是单点登录(Single Sign On 简称SSO)系统实现的功能

全栈程序猿的专栏 5154

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

【亲测免费】 Spring Security OAuth2.0 SSO单点登录Demo

Spring Security OAuth2.0 SSO单点登录Demo 【下载地址】SpringSecurityOAuth2.0SSO单点登录Demo 这是一个基于Spring Security和OAuth 2.0协议实现的SSO单点登录Demo,专为开发者提供标准解决方案参考。项目采用Spring Boot、Spri...

gitblog_06733的博客 349

spring security oauth2 配置不登陆也能访问_Spring Security Oauth2 SSO单点登录配置及原理深度剖析...

单点登录(SingleSignOn,SSO),就是通过用户的一次性鉴别登录。当用户在身份认证服务器上登录一次以后,即可获得访问单点登录系统中其他关联系统和应用软件的权限,同时这种实现是不需要管理员对用户的登录状态或其他信息进行修改的,这意味着在多个应用系统中,用户只需一次登录就可以访问所有相互信任的应用系统。随着企业各系统越来越多,如办公自动化(OA)系统,财务管理系统,档案管理系统,信...

weixin_31589331的博客 2622

JWT实战 & Spring Security Oauth2整合JWT & 整合SSO单点登录

文章目录一、JWT1.1 什么是JWT1.2 JWT组成头部(header)载荷(payload)签名(signature)如何应用1.3 JJWT快速开始创建tokentoken的验证解析token过期校验自定义claims1.4 Spring Security Oauth2整合JWT整合JWT扩展JWT中的存储内容解析JWT刷新令牌 一、JWT 1.1 什么是JWT JSON Web Token(JWT)是一个开放的行业标准(RFC 7519),它定义了一种简介的、自包含的协议格式,用于在通信双方传递j

沮丧的南瓜 4123

Spring Security Oauth2实践(3) - 单点登录(SSO)

本次分享基于Oauth2实现SSO单点登录的实践。这个场景经常用于企业开发,集成多方系统认证。分享CAS与Oauth2实现上的区别,给出Oauth2实现的一个demo,但在单点登出时,Oauth2服务端及客户需要基于过滤器方式定制开发实现。

奔跑的蜗牛 647

【SpringSecurityOAuth2】源码分析@EnableOAuth2Sso在Spring Security OAuth2 SSO单点登录场景下的作用

目录【SpringSecurityOAuth2】源码分析@EnableOAuth2Sso在Spring Security OAuth2 SSO场景下的作用一、从Spring Security OAuth2官方文档了解@EnableOAuth2Sso作用二、源码分析@EnableOAuth2Sso作用@EnableOAuth2ClientOAuth2SsoCustomConfiguration:OA...

Trust_FreeDom的专栏 1184

基于Spring Security Oauth2的SSO单点登录+JWT权限控制实践

理论知识在此之前需要学习和了解一些前置知识包括:Spring Security:基于 Spring实现的 Web系统的认证和权限模块OAuth2:一个关于授权(authorization)的开放网络标准单点登录 (SSO):在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统JWT:在网络应用间传递信息的一种基于 JSON的开放标准((RFC 7519),用于作为JSON对象在不同系统之间进行安全地信息传输。主要使用场景一般是用来在 身份提供者和服务提供者间传递被认证的用户身份信息要完成的目

Candyz7的博客 821
上一篇: Spring Cloud Admin 实战
下一篇: 微信、支付宝各种支付退款
exklin
博客等级 码龄9年 35粉丝 68原创
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值