iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springbootoauth2实现单点登录实例
  • 548
分享到

springbootoauth2实现单点登录实例

2024-04-02 19:04:59 548人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

    我们见过的很多网站,容许使用第三方账号登录,他不需要关注用户信息,只需要用户拿到授权码就可以访问。     oauth2是用

    我们见过的很多网站,容许使用第三方账号登录,他不需要关注用户信息,只需要用户拿到授权码就可以访问。

    oauth2是用来做三方登录的,他的授权方式有好几种,授权码模式、密码模式、隐式模式、客户端模式。

    oauth2认证的过程如下:一般我们请求一个需要登录的网站A,会提示我们使用第三方网站C的用户登录,我们登录,这时候需要我们授权,就是authorize,授权之后,会得到一个token,我们拿到这个token就可以访问这个网站A了。A网站不关心C网站的用户信息。

    springsecurity结合oauth2可以做这个功能,这里给出一个SpringBoot+security+oauth2的实例,这个实例很直观,就是我们有两个服务A,C,A是需要用户登录的网站,而C是授权服务器。当我们访问A的资源:Http://localhost:8000/hello,他会跳到C的服务器上登录,登录完成,授权,就直接调回来A网站,这里使用了单点登录功能。

    构建项目:我这里构建了一个父级项目securityoauth2,然后加入了两个模块:auth-server,client-1。

    项目依赖:

    securityoauth2->pom.xml

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
	</parent>
  <dependencies>
  	  <dependency>
  	  	<groupId>org.springframework.boot</groupId>
  	  	<artifactId>spring-boot-starter-WEB</artifactId>
  	  </dependency>
  	  <dependency>
  	  	<groupId>org.springframework.boot</groupId>
  	  	<artifactId>spring-boot-starter-security</artifactId>
  	  </dependency>
  	  <dependency>
  	  	<groupId>org.springframework.security.oauth</groupId>
  	  	<artifactId>spring-security-oauth2</artifactId>
  	  	<version>2.3.4.RELEASE</version>
  	  </dependency>
  	  <dependency>
  		<groupId>org.springframework.security.oauth.boot</groupId>
  		<artifactId>spring-security-oauth2-autoconfigure</artifactId>
  		<version>2.3.2.RELEASE</version>
  	</dependency>
  </dependencies>
  <modules>
  	<module>auth-server</module>
  	<module>client-1</module>
  </modules>

    ***********auth-server***************************************

    AuthServerConfiguration.java

package com.xxx.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
@EnableAuthorizationServer
public class AuthServerConfiguration extends AuthorizationServerConfigurerAdapter{
	@Autowired
	private PasswordEncoder passwordEncoder;
	
	@Override
	public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
		clients.inMemory()
			   .withClient("client")
			   .secret(passwordEncoder.encode("secret"))
			   .autoApprove(true)
			   .redirectUris("http://localhost:8000/login","http://localhost:8001/login")
			   .scopes("user")
			   .accessTokenValiditySeconds(7200)
			   .authorizedGrantTypes("authorization_code");
	}
}

    授权服务配置这里,主要设置client_id,以及secret,这里设置了自动授权autoApprove(true),就是我们输入用户名和密码登录之后,不会出现一个需要我们手动点击authorize的按钮进行授权。另外配置了redirect_uri,这个是必须的。最后还设置了授权类型,这里选择的是授权码模式。 

    SecurityConfiguration.java

package com.xxx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.httpsecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@Order(1)
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
	
	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}
	
	@Override
	protected void configure(AuthenticationManagerBuilder builder) throws Exception {
		builder.inMemoryAuthentication()
			   .withUser("admin")
			   .password(passwordEncoder().encode("admin"))
			   .roles("admin");
	}
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.requestMatchers()
			.antMatchers("/login")
			.antMatchers("/oauth/authorize")
			.and()
			.authorizeRequests().anyRequest().authenticated()
			.and()
			.fORMLogin()
			.and()
			.csrf().disable();
	}
}

     Security是做权限管理的,他需要配置用户和密码,这里采用内存保存的方式,将用户名和密码保存在内存中,而不是数据库或者Redis中,关于保存在哪里,对于了解oauth2来说,放在内存是最简单的,省去了建表,做数据库查询的麻烦。

    TestController.java

package com.xxx.web;
import java.security.Principal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/test")
public class TestController {
	@GetMapping("/user")
	public Principal currentUser(Principal principal) {
		return principal;
	}
}

    这个controller配置,不是用来测试访问他的,而是用来做一个接口,给client-1使用,client-1配置文件中有个配置:security.oauth2.resource.user-info-uri就是配置的这个接口。 

    App.java

package com.xxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
@SpringBootApplication
@EnableResourceServer
public class App {
    public static void main( String[] args ){
    	SpringApplication.run(App.class, args);
    }
}

    application.yaml   //无配置,可以略

    ***********client-1*********************************************************

     SecurityConfiguration.java

package com.xxx.config;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@SuppressWarnings("deprecation")
@Configuration
@EnableOAuth2Sso
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests().anyRequest().authenticated().and().csrf().disable();
	}
}

    TestController.java

package com.xxx.web;
import java.util.Arrays;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
	@GetMapping("/hello")
	public String hello() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		return authentication.getName()+Arrays.toString(authentication.getAuthorities().toArray());
	}
}

    App.java

package com.xxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
    public static void main( String[] args ){
    	SpringApplication.run(App.class, args);
    }
}

   application.yaml

server:
  port: 8000
  servlet:
    session:
      cookie:
        name: s1
security:
  oauth2:
    client:
      client-id: client
      client-secret: secret
      user-authorization-uri: http://localhost:8080/oauth/authorize
      access-token-uri: http://localhost:8080/oauth/token
    resource:
      user-info-uri: http://localhost:8080/api/test/user

    以上都是代码部分,下面主要是测试,测试过程很简单,就是我们启动两个服务,打开浏览器访问client-1的http://localhost:8000/hello接口,这时候因为需要登录,会跳转到auth-server服务的http://localhost:8080/login,登录成功,会接着访问授权接口,授权成功,会跳转到http://localhost:8000/login,然后跳转到http://localhost:8000/hello,貌似很复杂,我们先看看效果:

    

    值得注意的是,如图所示的2、3顺序,在这个结果页面是这样的,但是在登录页面并不是这样,而是先3后2:

    可以这么解释:client-1的接口http://localhost:8000/hello需要权限才能访问,这时候,需要登录自己的系统http://localhost:8000/login,而自己的系统支持oauth2,他通过封装了client_id,response_type,redirect_uri,state等参数的授权码模式请求接口,向auth-server服务发起授权请求http://localhost:8080/oauth/authorize?client_id=client&redirect_uri=http://localhost:8000/login&response_type=code&state=PphcA2,请求会跳转auth-server的登录页面 http://localhost:8080/login。

    这里因为使用了单点登录,先从client-1跳转到了auth-server,最后拿到了code之后,跳回了client-1,访问成功。而页面上显示的内容并不是一开始就是这样子的,他会先跳转auth-server登录页面: 

    以上代码有了,结果也验证了,但是个人还是不是很了解这个登录授权的原理和过程,也是在学习中,这个所谓的单点登陆,在代码上就用了一个@EnableOAuth2Sso注解在client-1项目的SecurityConfiguration类上,该类也是继承自WebSecurityConfigurerAdapter类,然后覆盖了configure(HttpSecurity http)方法。 

到此这篇关于springboot oauth2实现单点登录实例的文章就介绍到这了,更多相关springboot oauth2单点登录内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: springbootoauth2实现单点登录实例

本文链接: https://www.lsjlt.com/news/161565.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • springbootoauth2实现单点登录实例
        我们见过的很多网站,容许使用第三方账号登录,他不需要关注用户信息,只需要用户拿到授权码就可以访问。     oauth2是用...
    99+
    2022-11-12
  • CAS实现单点登录实例源码
    修改server.xml文件,如下: 注意: 这里使用的是https的认证方式,需要将这个配置放开,并做如下修改:port="8443" protocol="org.apache.coyote.htt...
    99+
    2023-06-05
  • springboot简单实现单点登录的示例代码
    什么是单点登录就不用再说了,今天通过自定义sessionId来实现它,想了解的可以参考https://www.xuxueli.com/xxl-sso/ 讲一下大概的实现思路吧:这里有...
    99+
    2022-11-12
  • 若依ruoyi实现单点登录
    系统说明(两个系统数据库用户信息username是同步的,都是唯一的) 第三方平台 若依系统(ruoyi分离版) 登录需求: 我登录到第三方平台,第三方平台嵌入我们的若依,所以在跳若依的管理页面时不想再登录了。但是验证是需要把第三方平台的t...
    99+
    2023-10-02
    java 用户登录
  • java单点登录(SSO)的实现
    单点登录(SSO):SSO是指在多个应用系统中个,用户只需要登陆一次就可以访问所有相互信任的应用系统。它包括可以将这次主要的登录映射到其他应用中用于同一用户的登陆的机制。 SSO的实...
    99+
    2022-11-12
  • NodeJS怎么实现单点登录
    这篇文章主要讲解了“NodeJS怎么实现单点登录”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“NodeJS怎么实现单点登录”吧!什么是单点登录随着公司业务的增多,必然会产生各个不同的系统,如...
    99+
    2023-06-30
  • laravel单点登录怎么实现
    这篇“laravel单点登录怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“laravel单点登录怎么实现”文章吧。单...
    99+
    2023-07-02
  • SpringBoot单点登录怎么实现
    这篇文章主要介绍了SpringBoot单点登录怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot单点登录怎么实现文章都会有所收获,下面我们一起来看看吧。1.具体实现步骤添加拦截器,设置U...
    99+
    2023-07-04
  • SpringSecurityOAuth2单点登录和登出的实现
    目录1. 单点登录1.1 使用内存保存客户端和用户信息1.2 使用数据库保存客户端和用户信息1.3 单点登录流程1.3 JWT Token2. 单点登出3. 总结参考:Spring ...
    99+
    2022-11-13
  • 基于ASP.NET实现单点登录(SSO)的示例代码
    目录背景逻辑分析代码实现Service总结背景 先上个图,看一下效果: SSO英文全称Single Sign On(单点登录)。SSO是在多个应用系统中,用户只需要登录一次就可以访...
    99+
    2022-11-13
  • SpringBoot整合Keycloak实现单点登录的示例代码
    目录1. 搭建Keycloak服务器2. 配置权限2.1. 登陆2.2. 创建Realm2.3. 创建用户2.4. 创建客户端2.5. 创建角色2.6. 配置用户角色关系2.7. 配...
    99+
    2022-11-13
  • Java编程实现springMVC简单登录实例
    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Sp...
    99+
    2023-05-30
    springmvc 登录 ava
  • 单点登录原理及实现方式
    一、什么是单点登录 单点登录的英文名叫做:Single Sign On(简称SSO),指在同一帐号平台下的多个应用系统中,用户只需登录一次,即可访问所有相互信任的系统。简而言之,多个系统,统一登陆。 为什么需要做单点登录系统呢?在一些互联网...
    99+
    2023-09-25
    java 分布式
  • 基于Node如何实现单点登录
    本文小编为大家详细介绍“基于Node如何实现单点登录”,内容详细,步骤清晰,细节处理妥当,希望这篇“基于Node如何实现单点登录”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。什么是单点登录随着公司业务的增多,必然...
    99+
    2023-07-04
  • Redis实现Session共享与单点登录
    首先,导包。 在pom.XML文件里面加入以下: <dependency> <groupId>org.springframework.boot</groupId> <...
    99+
    2022-07-12
    RedisSession共享 RedisSession单点登录
  • 如何使用JWT实现单点登录
    本篇内容介绍了“如何使用JWT实现单点登录”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、故事起源说起 ...
    99+
    2022-10-19
  • vue页面怎么实现单点登录
    在vue页面中实现单点登录的方法有:1.通过Cookie作为凭证媒介实现;2.通过页面重定向方式实现;3.通过JSONP实现;具体方法如下:通过Cookie作为凭证媒介实现单点登录可以在vue中利用cookie作为媒介,用于存放用户凭证,当...
    99+
    2022-10-17
  • NodeJS实现单点登录原理解析
    目录什么是单点登录单点登录原理NodeJS 演示三个不同的服务首次访问跳转至登录页应用A判断登录态,跳转到SSO认证服务器认证服务器判断登录态,渲染登录页校验用户信息,创建令牌从认证...
    99+
    2022-11-13
  • nodejs如何实现单点登录系统
    这篇文章主要讲解了“nodejs如何实现单点登录系统”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“nodejs如何实现单点登录系统”吧!单点登录SSO(Single Sign On),就是把...
    99+
    2023-07-05
  • node如何实现单点登录系统
    今天小编给大家分享一下node如何实现单点登录系统的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一. 基础知识1.1 同源策...
    99+
    2023-07-04
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作