欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Spring Security OAuth2實現單點登錄

 更新時間:2019年06月03日 10:41:33   作者:程序猿Knight  
在本教程中,我們將討論如何使用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。用戶端

最后,我們將創(chuàng)建我們之前在配置中使用的用戶端:

@RestController
public class UserController {
 @GetMapping("/user/me")
 public Principal user(Principal principal) {
  return principal;
 }
}

當然,這將使用JSON表示返回用戶數據。

4。結論

在本快速教程中,我們專注于使用Spring Security Oauth2Spring Boot實現單點登錄。

與往常一樣,可以在GitHub上找到完整的源代碼。

總結

以上所述是小編給大家介紹的使用Spring Security OAuth2實現單點登錄,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

相關文章

  • java正則表達式實現提取需要的字符并放入數組【ArrayList數組去重復功能】

    java正則表達式實現提取需要的字符并放入數組【ArrayList數組去重復功能】

    這篇文章主要介紹了java正則表達式實現提取需要的字符并放入數組,即基于正則的ArrayList數組去重復功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2017-01-01
  • Spring中@Transactional注解關鍵屬性和用法小結

    Spring中@Transactional注解關鍵屬性和用法小結

    在Spring框架中,@Transactional 是一個注解,用于聲明事務性的方法,它提供了一種聲明式的事務管理方式,避免了在代碼中直接編寫事務管理相關的代碼,本文給大家介紹@Transactional 注解的一些關鍵屬性和用法,感興趣的朋友一起看看吧
    2023-12-12
  • 解決SpringBoot整合ElasticSearch遇到的連接問題

    解決SpringBoot整合ElasticSearch遇到的連接問題

    這篇文章主要介紹了解決SpringBoot整合ElasticSearch遇到的連接問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java 非對稱加密算法DH實現詳解

    java 非對稱加密算法DH實現詳解

    這篇文章主要介紹了java 非對稱加密算法DH實現詳解 ,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • Spring的異常處理@ExceptionHandler注解解析

    Spring的異常處理@ExceptionHandler注解解析

    這篇文章主要介紹了Spring的異常處理@ExceptionHandler注解解析,當一個Controller中有方法加了@ExceptionHandler之后,這個Controller其他方法中沒有捕獲的異常就會以參數的形式傳入加了@ExceptionHandler注解的那個方法中,需要的朋友可以參考下
    2023-12-12
  • SpringBoot整合Web之AOP配置詳解

    SpringBoot整合Web之AOP配置詳解

    面向切面編程(aspect-oriented programming,AOP)主要實現的目的是針對業(yè)務處理過程中的切面進行提取,諸如日志、事務管理和安全這樣的系統(tǒng)服務,從而使得業(yè)務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率
    2022-08-08
  • Spring注解@Configuration與@Bean注冊組件的使用詳解

    Spring注解@Configuration與@Bean注冊組件的使用詳解

    這篇文章主要介紹了SpringBoot中的注解@Configuration與@Bean注冊組件的使用,具有很好的參考價值,希望對大家有所幫助
    2022-06-06
  • java和 javaw 及 javaws的區(qū)別解析

    java和 javaw 及 javaws的區(qū)別解析

    這篇文章主要介紹了java和 javaw 及 javaws的區(qū)別解析,本文通過實例給大家詳細介紹,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Spring Boot實現分布式鎖的自動釋放的示例代碼

    Spring Boot實現分布式鎖的自動釋放的示例代碼

    在實際開發(fā)中,我們可以使用 Redis、Zookeeper 等分布式系統(tǒng)來實現分布式鎖,本文將介紹如何使用 Spring Boot 來實現分布式鎖的自動釋放,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • Java之源文件的執(zhí)行全過程

    Java之源文件的執(zhí)行全過程

    這篇文章主要介紹了Java之源文件的執(zhí)行全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論