900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 【Spring Boot】Spring Boot @EnableOAuth2Sso | 启用 OAuth2 单点登录

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

时间:2019-03-04 21:55:09

相关推荐

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

文章目录

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

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

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

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

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

演示工具版本

Java 11Spring 5.1.7.RELEASESpring Boot 2.1.5.RELEASEMaven 3.5.2

Maven 依赖

找到OAuth2Maven依赖。

<dependency><groupId>org.springframework.security.oauth.boot</groupId><artifactId>spring-security-oauth2-autoconfigure</artifactId><version>2.1.5.RELEASE</version></dependency>

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

使用 @EnableOAuth2Sso

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

@Configuration@EnableOAuth2Ssopublic 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@EnableOAuth2Ssopublic class SecurityConfiguration extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/", "/error**").permitAll().anyRequest().authenticated().and().logout().logoutUrl("/logout").logoutSuccessUrl("/");}}

OAuth2 配置

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

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

application.yml

security:oauth2:client:clientId: <your_github_clientId>clientSecret: <your_github_clientSecret>accessTokenUri: /login/oauth/access_tokenuserAuthorizationUri: /login/oauth/authorizeclientAuthenticationScheme: formresource:userInfoUri: /usersso:login-path: /login

你需要在上述YML文件中输入你的GitHubclientIdclientSecret

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

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

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

登出

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

<form th:action="@{/logout}" method="POST"><input type="submit" value="Logout"/></form>

完整示例

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

pom.xml

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath /></parent><properties><context.path>spring-app</context.path><java.version>11</java.version></properties><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.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.security.oauth.boot</groupId><artifactId>spring-security-oauth2-autoconfigure</artifactId><version>2.1.5.RELEASE</version></dependency></dependencies>

AppController.java

package com.concretepage;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic 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

<!doctype html><html><head><title>Spring Security</title></head><body><h3>Login with <a href="/hello">GitHub</a></h3></body></html>

welcome.html

<!doctype html><html lang="en"><head><title>Welcome</title></head><body>Welcome <b th:inline="text" > [[${#httpServletRequest.remoteUser}]] </b> <br/><br/><form th:action="@{/logout}" method="POST"><input type="submit" value="Logout"/></form></body></html>

error.html

<!doctype html><html><head><title>Spring Security</title></head><body><h3>Error</h3><p thif="${param.error}">An error occurred.</p></body></html>

Main.java

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

输出

下载该项目,并在application.yml文件中输入你的GitHubclientIdclientSecret

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

mvn spring-boot:run

访问网址

http://localhost:8080/

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

参考文献

【1】OAuth2 Boot

【2】OAuth 2 Developers Guide

源码下载

提取码:mao4

spring-boot-enableoauth2sso.zip

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。