欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Spring Security OAuth2实现单点登录

程序员文章站 2023-08-16 15:22:57
1.概述 在本教程中,我们将讨论如何使用spring security oauth和spring boot实现sso - 单点登录。 我们将使用三个单独的应用程序:...

1.概述

在本教程中,我们将讨论如何使用spring security oauth和spring boot实现sso - 单点登录。

我们将使用三个单独的应用程序:

•授权服务器 - 这是*身份验证机制
•两个客户端应用程序:使用sso的应用程序

非常简单地说,当用户试图访问客户端应用程序中的安全页面时,他们将被重定向到首先通过身份验证服务器进行身份验证。

我们将使用oauth2中的授权代码授权类型来驱动身份验证委派。

2.客户端应用程序

让我们从客户端应用程序开始;当然,我们将使用spring boot来最小化配置:

2.1。 maven依赖

首先,我们需要在pom.xml中使用以下依赖项:

<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.boot</groupid>
 <artifactid>spring-security-oauth2-autoconfigure</artifactid>
 <version>2.0.1.release</version>
</dependency>
<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-thymeleaf</artifactid>
</dependency>
<dependency>
 <groupid>org.thymeleaf.extras</groupid>
 <artifactid>thymeleaf-extras-springsecurity4</artifactid>
</dependency>

2.2。security配置

接下来,最重要的部分,我们的客户端应用程序的security配置:

@configuration
@enableoauth2sso
public class uisecurityconfig extends websecurityconfigureradapter {
  
 @override
 public void configure(httpsecurity http) throws exception {
  http.antmatcher("/**")
   .authorizerequests()
   .antmatchers("/", "/login**")
   .permitall()
   .anyrequest()
   .authenticated();
 }
}

当然,这种配置的核心部分是我们用于启用单点登录的@ enableoauth2sso注释。

请注意,我们需要扩展websecurityconfigureradapter - 如果没有它,所有路径都将受到保护 - 因此用户将在尝试访问任何页面时重定向以登录。在我们的例子中,首页和登录页面是唯一可以在没有身份验证的情况下访问的页面。

最后,我们还定义了一个requestcontextlistener bean来处理请求范围。

application.yml:
server:
 port: 8082
 servlet:
  context-path: /ui
 session:
  cookie:
  name: uisession
security:
 basic:
 enabled: false
 oauth2:
 client:
  clientid: sampleclientid
  clientsecret: secret
  accesstokenuri: http://localhost:8081/auth/oauth/token
  userauthorizationuri: http://localhost:8081/auth/oauth/authorize
 resource:
  userinfouri: http://localhost:8081/auth/user/me
spring:
 thymeleaf:
 cache: false

一些快速说明:

•我们禁用了默认的基本身份验证
•accesstokenuri是获取访问令牌的uri
•userauthorizationuri是用户将被重定向到的授权uri
•userinfouri用户端点的uri,用于获取当前用户详细信息

另请注意,在我们的示例中,我们推出了授权服务器,但当然我们也可以使用其他第三方提供商,如facebook或github。

2.3。前端

现在,让我们来看看客户端应用程序的前端配置。我们不会在这里专注于此,主要是因为我们已经在。
 我们的客户端应用程序有一个非常简单的前端;这是index.html:

<h1>spring security sso</h1>
<a href="securedpage" rel="external nofollow" >login</a>

和securedpage.html:

<h1>secured page</h1>
welcome, <span th:text="${#authentication.name}">name</span>

securedpage.html页面需要对用户进行身份验证。如果未经身份验证的用户尝试访问securedpage.html,则会首先将其重定向到登录页面。

3. auth服务器

现在让我们在这里讨论我们的授权服务器。

3.1。 maven依赖

首先,我们需要在pom.xml中定义依赖项:

<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-web</artifactid>
</dependency>
<dependency>
 <groupid>org.springframework.security.oauth</groupid>
 <artifactid>spring-security-oauth2</artifactid>
 <version>2.3.3.release</version>
</dependency>

3.2。 oauth配置

重要的是要理解我们将在这里一起运行授权服务器和资源服务器,作为单个可部署单元。

让我们从资源服务器的配置开始 :

@springbootapplication
@enableresourceserver
public class authorizationserverapplication extends springbootservletinitializer {
 public static void main(string[] args) {
  springapplication.run(authorizationserverapplication.class, args);
 }
}

然后,我们将配置我们的授权服务器:

@configuration
@enableauthorizationserver
public class authserverconfig extends authorizationserverconfigureradapter {
 @autowired
 private bcryptpasswordencoder passwordencoder;
 @override
 public void configure(
  authorizationserversecurityconfigurer oauthserver) throws exception {
  oauthserver.tokenkeyaccess("permitall()")
   .checktokenaccess("isauthenticated()");
 }
 @override
 public void configure(clientdetailsserviceconfigurer clients) throws exception {
  clients.inmemory()
   .withclient("sampleclientid")
   .secret(passwordencoder.encode("secret"))
   .authorizedgranttypes("authorization_code")
   .scopes("user_info")
   .autoapprove(true) 
   .redirecturis("http://localhost:8082/ui/login","http://localhost:8083/ui2/login"); 
 }
}

请注意我们如何仅使用authorization_code grant类型启用简单客户端。

另外,请注意autoapprove如何设置为true,以便我们不会被重定向并手动批准任何范围。

3.3。security配置

首先,我们将通过application.properties禁用默认的基本身份验证:

server.port=8081
server.servlet.context-path=/auth

现在,让我们转到配置并定义一个简单的表单登录机制:

@configuration
@order(1)
public class securityconfig extends websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) throws exception {
  http.requestmatchers()
   .antmatchers("/login", "/oauth/authorize")
   .and()
   .authorizerequests()
   .anyrequest().authenticated()
   .and()
   .formlogin().permitall();
 }
 
 @override
 protected void configure(authenticationmanagerbuilder auth) throws exception {
  auth.inmemoryauthentication()
   .withuser("john")
   .password(passwordencoder().encode("123"))
   .roles("user");
 }
  
 @bean
 public bcryptpasswordencoder passwordencoder(){ 
  return new bcryptpasswordencoder(); 
 }
}

请注意,我们使用简单的内存中身份验证,但我们可以简单地将其替换为自定义userdetailss​​ervice。

3.4。用户端

最后,我们将创建我们之前在配置中使用的用户端:

@restcontroller
public class usercontroller {
 @getmapping("/user/me")
 public principal user(principal principal) {
  return principal;
 }
}

当然,这将使用json表示返回用户数据。

4。结论

在本快速教程中,我们专注于使用spring security oauth2spring boot实现单点登录。

与往常一样,可以在github上找到完整的源代码

总结

以上所述是小编给大家介绍的使用spring security oauth2实现单点登录,希望对大家有所帮助