前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

作者头像
全栈程序员站长
发布2022-09-27 16:05:45
2.8K0
发布2022-09-27 16:05:45
举报

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多个票证,并且具有许多新功能

今天,我将向您展示如何在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. 根据需要使用访问令牌获取其他用户属性
oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」
oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

创建一个Spring Boot应用

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

oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」
oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

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

oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」
oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

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

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

代码语言:javascript
复制
Using default security password: 103c55b4-2760-4830-9bca-a06a87d384f9

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

oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」
oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

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

代码语言:javascript
复制
security.user.password=spring security is ph@!

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

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

代码语言:javascript
复制
$ 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。

代码语言:javascript
复制
{
   "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()方法,该方法映射到/并返回用户名。

代码语言:javascript
复制
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

代码语言:javascript
复制
$ 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=block

Hello 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 ,并使用以下内容进行填充。

代码语言:javascript
复制
spring:
 thymeleaf:
   cache: false
 security:
   oauth2:
     client:
       registration:
         okta:
           client-id: {clientId}
           client-secret: {clientSecret}
       provider:
         okta:
           authorization-uri: https://{yourOktaDomain}.com/oauth2/default/v1/authorize
           token-uri: https://{yourOktaDomain}.com/oauth2/default/v1/token
           user-info-uri: https://{yourOktaDomain}.com/oauth2/default/v1/userinfo
           jwk-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配置才能正确初始化。

代码语言:javascript
复制
<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登录。

oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」
oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

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

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

oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」
oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

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

oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」
oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

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

使用OIDC获取用户信息

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

代码语言:javascript
复制
/*
* 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 Clients
           userAttributes = 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的支持,根据用户的身份验证状态显示/隐藏页面的不同部分。

代码语言:javascript
复制
<!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上创建另一个模板以显示用户的属性。

代码语言:javascript
复制
<!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>

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

oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」
oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

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

oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」
oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

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

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

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

如果您对此帖子有任何疑问,请在下面发表评论。 您还可以使用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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/184384.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建一个Spring Boot应用
  • 使用Okta添加身份验证
    • 创建一个OpenID Connect应用
      • 使用OIDC获取用户信息
      • 了解有关Spring Security和OIDC的更多信息
      相关产品与服务
      访问管理
      访问管理(Cloud Access Management,CAM)可以帮助您安全、便捷地管理对腾讯云服务和资源的访问。您可以使用CAM创建子用户、用户组和角色,并通过策略控制其访问范围。CAM支持用户和角色SSO能力,您可以根据具体管理场景针对性设置企业内用户和腾讯云的互通能力。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档