栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Spring Boot】Spring Boot @EnableOAuth2Sso | 启用 OAuth2 单点登录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Spring Boot】Spring Boot @EnableOAuth2Sso | 启用 OAuth2 单点登录

文章目录
  • 演示工具版本
  • Maven 依赖
  • 使用 @EnableOAuth2Sso
  • OAuth2 配置
  • 登出
  • 完整示例
  • 输出
  • 参考文献
  • 源码下载

本页将介绍Spring Security OAuth2 @EnableOAuth2Sso注解的例子。

@EnableOAuth2Sso注解可以启用OAuth2单点登录(SingleSignOn,SSO)。默认情况下,所有的路径都是需要安全的。

我们可以在Spring Security的Java配置中使用WebSecurityConfigurerAdapter来定制它。我们可以使用application.properties或application.yml或以命令行方式配置Spring Security OAuth2。

这里我们将使用GitHub创建Spring Boot OAuth2应用程序。

演示工具版本
  1. Java 11
  2. Spring 5.1.7.RELEASE
  3. Spring Boot 2.1.5.RELEASE
  4. Maven 3.5.2
Maven 依赖

找到OAuth2的Maven依赖。


	org.springframework.security.oauth.boot
	spring-security-oauth2-autoconfigure
	2.1.5.RELEASE
 

在Spring Boot应用程序中,上述依赖关系在类路径上的可用性为我们提供了自动配置OAuth2的优势。

使用 @EnableOAuth2Sso

要在我们的应用程序中使用@EnableOAuth2Sso,请在Spring Security配置中用@Configuration对其进行注释。

@Configuration
@EnableOAuth2Sso
public class SecurityConfiguration {
} 

现在所有的URL都是需要安全验证的。我们可以使用WebSecurityConfigurerAdapter来自定义这一行为。假设我们想使用一些不进行安全验证的URL,如主页和错误页面等,如下进行配置。

SecurityConfiguration.java

package com.concretepage;
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;

@Configuration
@EnableOAuth2Sso
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
	   @Override
	    protected void configure(HttpSecurity http) throws Exception {
	        http
	            .authorizeRequests()
	            .antMatchers("/", "/error**").permitAll()
                    .anyRequest().authenticated()
                    .and().logout().logoutUrl("/logout")
		    .logoutSuccessUrl("/");

	    }
} 
OAuth2 配置

在Spring Boot应用程序中,我们可以使用application.properties或application.yml或以命令行方式配置安全OAuth2客户端、资源和sso属性。

在我们的例子中,我们使用的是GitHub OAuth。

application.yml

security:
  oauth2:
   client:
     clientId: 
     clientSecret: 
     accessTokenUri: https://github.com/login/oauth/access_token
     userAuthorizationUri: https://github.com/login/oauth/authorize
     clientAuthenticationScheme: form
   resource:
     userInfoUri: https://api.github.com/user
   sso:
     login-path: /login 

你需要在上述YML文件中输入你的GitHub的clientId 和 clientSecret。

clientId: 这是OAuth客户端的ID,OAuth提供商通过它来识别客户端。

clientSecret: 与资源关联的客户端密钥。

要获得GitHub的OAuth2客户端ID和客户端密钥,请通过该链接。

登出

要注销Spring Security应用程序,请在Spring Security Java配置文件中配置注销URL,默认为/logout,然后创建一个表单并以POST方式提交给注销URL。用Thymeleaf查找示例表单。

完整示例

这里我们将提供我们的演示程序的完整代码。文章中已经给出了SecurityConfiguration.java和application.yml这两个文件。查找其余的代码。

pom.xml


	org.springframework.boot
	spring-boot-starter-parent
	2.1.5.RELEASE
	


	spring-app
	11


	
		org.springframework.boot
		spring-boot-starter-security
	
	
		org.springframework.boot
		spring-boot-starter-web
	
	
		org.springframework.boot
		spring-boot-starter-thymeleaf
	
	
		org.springframework.security.oauth.boot
		spring-security-oauth2-autoconfigure
		2.1.5.RELEASE
	
 

AppController.java

package com.concretepage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AppController {
	@GetMapping("hello")
	public ModelAndView welcome() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("welcome");
		return mav;
	}

	@GetMapping("error")
	public ModelAndView error() {
		ModelAndView mav = new ModelAndView();
		return mav;
	}
} 

index.html




  Spring Security


   

Login with GitHub

welcome.html




    Welcome


   Welcome  [[${#httpServletRequest.remoteUser}]]  

error.html




  Spring Security


   

Error

An error occurred.

Main.java

package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
} 
输出

下载该项目,并在application.yml文件中输入你的GitHub clientId和clientSecret。

然后使用命令提示符从项目的根文件夹中运行以下命令。

mvn spring-boot:run 

访问网址

http://localhost:8080/ 

点击GitHub链接进行登录。你将被重定向到GitHub登录页面。登录成功后,你将被重定向到你的应用程序并看到欢迎页面。

参考文献

【1】OAuth2 Boot
【2】OAuth 2 Developers Guide

源码下载

提取码:mao4

spring-boot-enableoauth2sso.zip

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/331697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号