oidc auth2.0

“我喜欢编写身份验证和授权代码。” 〜从来没有Java开发人员。 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证。

Spring Security不仅是一个功能强大且可高度自定义的身份验证和访问控制框架,它还是保护基于Spring的应用程序的实际标准。 从前,Spring Security需要使用大量的XML来配置所有内容,但是那段日子已经过去了。 如今,Spring Security通过Spring的JavaConfig提供了更简单的配置。 如果您看一下我最近写的JHipster OIDC示例中的`SecurityConfiguration.java`类,您会发现它少于100行代码!

Spring Security 5.0可以解析400多个票证,并且具有许多新功能 :

  • OAuth 2.0登录
  • React性支持: @EnableWebFluxSecurity , @EnableReactiveMethodSecurity和WebFlux测试支持
  • 现代化的密码编码

今天,我将向您展示如何在Okta中使用OAuth 2.0登录支持。 我还将向您展示如何通过OpenID Connect(OIDC)检索用户的信息。

您知道Okta提供免费的开发人员帐户 ,每月最多有7,000个活跃用户,对吗? 这应该足以使您的杀手级应用破土动工。

Spring Security使使用OAuth 2.0进行身份验证变得非常容易。 它还提供了通过OIDC获取用户信息的功能。 请按照以下步骤了解更多信息!

什么是OIDC? 如果您不熟悉OAuth或OIDC,建议您阅读OAuth到底是什么 。 Open ID Connect流涉及以下步骤:

  1. 发现OIDC元数据
  2. 执行OAuth流以获取ID令牌和访问令牌
  3. 获取JWT签名密钥,并可以选择动态注册客户端应用程序
  4. 根据内置日期和签名在本地验证JWT ID令牌
  5. 根据需要使用访问令牌获取其他用户属性

创建一个Spring Boot应用

在浏览器中打开start.spring.io 。 Spring Initialzr是一个站点,可让您快速轻松地创建新的Spring Boot应用程序。 将Spring Boot版本(在右上角)设置为2.0.0.M7 。 输入组和工件名称。 从下面的屏幕快照中可以看到,我选择了com.okta.developeroidc 。 对于依赖项,选择WebReactive WebSecurityThymeleaf

单击“ 生成项目” ,下载zip,在硬盘上展开,然后在您喜欢的IDE中打开项目。 使用./mvnw spring-boot:run运行该应用程序, ./mvnw spring-boot:run将提示您登录。

Spring Security 4.x通过基本身份验证而不是登录表单提示您,因此这与Spring Security 5有所不同。

Spring Security启动程序会创建一个默认用户,其用户名为“ user”,并且密码每次启动应用程序时都会更改。 您可以在终端中找到该密码,类似于以下密码。

Using default security password: 103c55b4-2760-4830-9bca-a06a87d384f9

在表单中,为“用户”输入“ user”,并为“密码”输入生成的密码。 下一个屏幕将是404,因为您的应用没有为/路径配置默认路由。

在Spring Boot 1.x中,您可以更改用户的密码,因此每次都通过在src/main/resources/application.properties添加以下内容来更改密码。

security.user.password=spring security is ph@!

但是,这是Spring Boot 2.0中不推荐使用的功能。 好消息是,此更改可能会在GA发布之前恢复 。

同时,您可以将打印的密码复制到控制台,并与HTTPie一起使用 。

$ http --auth user:'bf91316f-f894-453a-9268-4826cdd7e151' localhost:8080
HTTP/1.1 404
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Sun, 03 Dec 2017 19:11:50 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=65283FCBDB9E6EF1C0679290AA994B0D; Path=/; HttpOnly
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

响应也将是404。

{"error": "Not Found","message": "No message available","path": "/","status": 404,"timestamp": "2017-12-03T19:11:50.846+0000"
}

您可以通过在与OidcApplication.javasrc/main/java/com/okta/developer/oidc )相同的目录中创建MainController.java来摆脱404。 创建一个home()方法,该方法映射到/并返回用户名。

package com.okta.developer.oidc;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.security.Principal;@RestController
public class MainController {@GetMapping("/")String home(Principal user) {return "Hello " + user.getName();}
}

重新启动服务器,使用user和生成的密码登录,您应该看到Hello user

$ http --auth user:'d7c4138d-a1cc-4cc9-8975-97f37567594a' localhost:8080
HTTP/1.1 200
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Length: 10
Content-Type: text/plain;charset=UTF-8
Date: Sun, 03 Dec 2017 19:26:54 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=22A5A91051B7AFBA1DC8BD30C0B53365; Path=/; HttpOnly
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=blockHello user

使用Okta添加身份验证

在上一教程中 ,我向您展示了如何使用Spring Security OAuth为您的应用程序提供SSO。 您可以在Spring Security 5中执行相同的操作,但是您现在还可以指定多个提供程序,而以前是做不到的。 Spring Security 5有一个OAuth 2.0登录示例 ,以及有关所有工作原理的文档 。

创建一个OpenID Connect应用

要与Okta集成,您需要在developer.okta.com上注册一个帐户 。 确认电子邮件并登录后,导航至应用程序 > 添加应用程序 。 单击Web ,然后单击下一步 。 给应用程序起一个您将记住的名称,将http://localhost:8080指定为基本URI,并将http://localhost:8080/login/oauth2/code/okta为登录重定向URI。

src/main/resources/application.properties重命名为src/main/resources/application.yml ,并使用以下内容进行填充。

spring:thymeleaf:cache: falsesecurity:oauth2:client:registration:okta:client-id: {clientId}client-secret: {clientSecret}provider:okta:authorization-uri: https://{yourOktaDomain}.com/oauth2/default/v1/authorizetoken-uri: https://{yourOktaDomain}.com/oauth2/default/v1/tokenuser-info-uri: https://{yourOktaDomain}.com/oauth2/default/v1/userinfojwk-set-uri: https://{yourOktaDomain}.com/oauth2/default/v1/keys

将客户端ID和密码从OIDC应用程序复制到application.yml文件中。 对于{yourOktaDomain} ,请使用您的域的值(例如dev-158606.oktapreview.com )。 请确保它包括-admin在里面。

您需要将一些依赖项添加到pom.xml ,Spring Security 5的OAuth配置才能正确初始化。

<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId>
</dependency>
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-oauth2-client</artifactId>
</dependency>
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
<dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

重新启动您的应用程序,然后再次导航到http://localhost:8080 。 您会看到一个链接,单击该链接可以使用Okta登录。

注意:如果您想学习如何自定义Spring Security显示的登录屏幕,请参阅其OAuth 2.0登录页面文档 。

单击链接后,您应该会看到一个登录屏幕。

输入用于创建帐户的凭据,登录后,您应该会看到类似以下的屏幕。

注意:可以更改某些内容,以便Principal#getName()返回不同的值。 但是, Spring Boot 2.0.0.M7中存在一个错误 ,阻止了配置属性的工作。

使用OIDC获取用户信息

更改您的MainController.java使其具有以下代码。 这段代码添加了一个/userinfo映射,该映射使用Spring WebFlux的WebClient从用户信息端点获取用户信息。 我从Spring Security 5的OAuth 2.0登录示例复制了以下代码。

/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.okta.developer.oidc;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;import java.util.Collections;
import java.util.Map;/**
* @author Joe Grandja
*/
@Controller
public class MainController {private final OAuth2AuthorizedClientService authorizedClientService;public MainController(OAuth2AuthorizedClientService authorizedClientService) {this.authorizedClientService = authorizedClientService;}@RequestMapping("/")public String index(Model model, OAuth2AuthenticationToken authentication) {OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(authentication);model.addAttribute("userName", authentication.getName());model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());return "index";}@RequestMapping("/userinfo")public String userinfo(Model model, OAuth2AuthenticationToken authentication) {OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(authentication);Map userAttributes = Collections.emptyMap();String userInfoEndpointUri = authorizedClient.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri();if (!StringUtils.isEmpty(userInfoEndpointUri)) {    // userInfoEndpointUri is optional for OIDC ClientsuserAttributes = WebClient.builder().filter(oauth2Credentials(authorizedClient)).build().get().uri(userInfoEndpointUri).retrieve().bodyToMono(Map.class).block();}model.addAttribute("userAttributes", userAttributes);return "userinfo";}private OAuth2AuthorizedClient getAuthorizedClient(OAuth2AuthenticationToken authentication) {return this.authorizedClientService.loadAuthorizedClient(authentication.getAuthorizedClientRegistrationId(), authentication.getName());}private ExchangeFilterFunction oauth2Credentials(OAuth2AuthorizedClient authorizedClient) {return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {ClientRequest authorizedRequest = ClientRequest.from(clientRequest).header(HttpHeaders.AUTHORIZATION,"Bearer " + authorizedClient.getAccessToken().getTokenValue()).build();return Mono.just(authorizedRequest);});}
}

src/main/resources/templates/index.html创建Thymeleaf索引页面。 您可以使用Thymeleaf对Spring Security的支持,根据用户的身份验证状态显示/隐藏页面的不同部分。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head><title>Spring Security - OAuth 2.0 Login</title><meta charset="utf-8" />
</head>
<body>
<div style="float: right" th:fragment="logout" sec:authorize="isAuthenticated()"><div style="float:left"><span style="font-weight:bold">User: </span><span sec:authentication="name"></span></div><div style="float:none"> </div><div style="float:right"><form action="#" th:action="@{/logout}" method="post"><input type="submit" value="Logout" /></form></div>
</div>
<h1>OAuth 2.0 Login with Spring Security</h1>
<div>You are successfully logged in <span style="font-weight:bold" th:text="${userName}"></span>via the OAuth 2.0 Client <span style="font-weight:bold" th:text="${clientName}"></span>
</div>
<div> </div>
<div><a href="/userinfo" th:href="@{/userinfo}">Display User Info</a>
</div>
</body>
</html>

src/main/resources/userinfo.html上创建另一个模板以显示用户的属性。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head><title>Spring Security - OAuth 2.0 User Info</title><meta charset="utf-8" />
</head>
<body>
<div th:replace="index::logout"></div>
<h1>OAuth 2.0 User Info</h1>
<div><span style="font-weight:bold">User Attributes:</span><ul><li th:each="userAttribute : ${userAttributes}"><span style="font-weight:bold" th:text="${userAttribute.key}"></span>: <span th:text="${userAttribute.value}"></span></li></ul>
</div>
</body>
</html>

现在,当您登录时,您将看到一个显示用户信息的链接。

单击链接,您将看到从用户信息端点检索到的ID令牌的内容。

了解有关Spring Security和OIDC的更多信息

本文向您展示了如何使用OAuth 2.0和Spring Security 5实现登录。我还向您展示了如何使用OIDC检索用户信息。 本文开发的应用程序的源代码可以在GitHub上找到 。

这些资源提供了有关Okta和OIDC的其他信息:

  • Okta开发人员文档及其OpenID Connect API
  • 身份,声明和令牌– OpenID Connect入门,第1部分,共3部分
  • 行动中的OIDC – OpenID Connect入门,第2部分,共3部分
  • 令牌中有什么? – OpenID Connect入门,第3部分,共3部分
  • 使用Spring Security和Thymeleaf向您的应用程序添加基于角色的访问控制

如果您对此帖子有任何疑问,请在下面发表评论。 您还可以使用okta标签将其发布到Stack Overflow或使用我们的开发人员论坛 。

在Twitter上关注@OktaDev,以获取更多精彩内容!

“我喜欢编写身份验证和授权代码。” 〜从来没有Java开发人员。 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证。

Spring Security 5.0和OIDC入门最初于2017年12月18日发布在Okta开发人员博客上。

翻译自: https://www.javacodegeeks.com/2018/03/build-authentication-easy-way-spring-security-5-0-oidc.html

oidc auth2.0

oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证相关推荐

  1. 使用Spring Security 5.0和OIDC轻松构建身份验证

    "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证. Spri ...

  2. 无状态Spring安全性第2部分:无状态身份验证

    Spring Stateless Security系列的第二部分是关于以无状态方式探索身份验证的方法. 如果您错过了CSRF的第一部分,可以在这里找到. 因此,在谈论身份验证时,其全部内容就是让客户端 ...

  3. Spring Security 5.0.0正式发布

    Pivotal正式发布Spring Security 5.0.0,是2015年3月发布4.0.0版本以来的第一个大版本. \\ Spring Security诞生于2004年,当时叫作Acegi,现在 ...

  4. Spring Security OAuth2.0认证授权知识概括

    Spring Security OAuth2.0认证授权知识概括 安全框架基本概念 基于Session的认证方式 Spring Security简介 SpringSecurity详解 分布式系统认证方 ...

  5. Spring Security OAuth2.0认证授权五:用户信息扩展到jwt

    历史文章 [Spring Security OAuth2.0认证授权一:框架搭建和认证测试] [Spring Security OAuth2.0认证授权二:搭建资源服务] [Spring Securi ...

  6. Spring Security 5.0的DelegatingPasswordEncoder详解

    本文参考自Spring Security 5.0.4.RELEASE 的官方文档,结合源码介绍了 DelegatingPasswordEncoder,对其工作过程进行分析并解决其中遇到的问题.包括 T ...

  7. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至: https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc ...

  8. Spring Security OAuth2.0认证授权三:使用JWT令牌

    历史文章 [Spring Security OAuth2.0认证授权一:框架搭建和认证测试] [Spring Security OAuth2.0认证授权二:搭建资源服务] 前面两篇文章详细讲解了如何基 ...

  9. 7. Spring Boot2.5 安全机制与 REST API 身份验证实战

    文章目录 Spring Boot2.5 安全机制与 RESTAPI 身份验证实战 一.Java Spring Boot 2.5 安全机制 Java Spring Boot 2.0 安全机制 安全漏洞 ...

最新文章

  1. 【Qt】Qt多屏编程,在指定显示屏上显示指定对话框
  2. Netty学习笔记(三)EventLoopGroup开篇
  3. 【代码笔记】iOS-实现网络图片的异步加载和缓存
  4. 冒充“老干妈”公司工作人员行骗三人被提起公诉
  5. supervisord的安装
  6. mysql alter before_MySQL之alter语句用法总结
  7. google translate - 谷歌翻译小工具
  8. java中1字节(8位)_Java中基本数据类型占几个字节多少位
  9. 关闭ntp服务的 monitor monlist,解决漏洞CVE-2013-5211
  10. 惯导标定国内外研究现状小结(删减版)
  11. 智能安全辅助驾驶系统 STM32——MQ3酒精传感器的应用(HAL库)
  12. 8句极易踩中买家雷点的口头禅,你中了哪几条?
  13. 文件路径问题解决方案
  14. 华为高性能服务器实验室,1000平米全球唯一!华为神秘实验室首公开
  15. 通过cib.xml重新启动pacemaker
  16. 大学生应该如何选择服务器
  17. P2P网络——网络模型概述
  18. 注意保护眼睛,用护眼色
  19. synchronized源码解析
  20. API采集接口源码电商采集工具接口

热门文章

  1. P4169-[Violet]天使玩偶/SJY摆棋子【CDQ分治】
  2. nssl1248-B【点分治,平衡树】
  3. 【状压DP】滚榜(P7519)
  4. 8、mybatis中的sql映射文件详解(3)
  5. 汇编语言(三十四)之输出中文
  6. 如何快速开发一个 Dubbo 应用
  7. DevOps通用及版本控制面试题
  8. Jsoup解析HTML实例及文档方法详解
  9. Oracle入门(十三A2)之单行函数
  10. ajax读取.txt文件出现乱码