欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在SpringSecurity中利用OAuth2實(shí)現(xiàn)一個(gè)單點(diǎn)登錄功能

怎么在Spring Security中利用OAuth2實(shí)現(xiàn)一個(gè)單點(diǎn)登錄功能?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

成都創(chuàng)新互聯(lián)從2013年開始,先為深州等服務(wù)建站,深州等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為深州企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

1.概述

使用三個(gè)單獨(dú)的應(yīng)用程序:

?授權(quán)服務(wù)器 - 這是中央身份驗(yàn)證機(jī)制
?兩個(gè)客戶端應(yīng)用程序:使用SSO的應(yīng)用程序

非常簡單地說,當(dāng)用戶試圖訪問客戶端應(yīng)用程序中的安全頁面時(shí),他們將被重定向到首先通過身份驗(yàn)證服務(wù)器進(jìn)行身份驗(yàn)證。

我們將使用OAuth3中的授權(quán)代碼授權(quán)類型來驅(qū)動(dòng)身份驗(yàn)證委派。

2.客戶端應(yīng)用程序

讓我們從客戶端應(yīng)用程序開始;當(dāng)然,我們將使用Spring Boot來最小化配置:

2.1。 Maven依賴

首先,我們需要在pom.xml中使用以下依賴項(xiàng):

<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-oauth3-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配置

接下來,最重要的部分,我們的客戶端應(yīng)用程序的Security配置:

@Configuration
@EnableOAuth3Sso
public class UiSecurityConfig extends WebSecurityConfigurerAdapter {
  
 @Override
 public void configure(HttpSecurity http) throws Exception {
  http.antMatcher("/**")
   .authorizeRequests()
   .antMatchers("/", "/login**")
   .permitAll()
   .anyRequest()
   .authenticated();
 }
}

當(dāng)然,這種配置的核心部分是我們用于啟用單點(diǎn)登錄的@ EnableOAuth3Sso注釋。

請注意,我們需要擴(kuò)展WebSecurityConfigurerAdapter - 如果沒有它,所有路徑都將受到保護(hù) - 因此用戶將在嘗試訪問任何頁面時(shí)重定向以登錄。在我們的例子中,首頁和登錄頁面是唯一可以在沒有身份驗(yàn)證的情況下訪問的頁面。

最后,我們還定義了一個(gè)RequestContextListener bean來處理請求范圍。

application.yml:
server:
 port: 8082
 servlet:
  context-path: /ui
 session:
  cookie:
  name: UISESSION
security:
 basic:
 enabled: false
 oauth3:
 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

一些快速說明:

?我們禁用了默認(rèn)的基本身份驗(yàn)證
?accessTokenUri是獲取訪問令牌的URI
?userAuthorizationUri是用戶將被重定向到的授權(quán)URI
?userInfoUri用戶端點(diǎn)的URI,用于獲取當(dāng)前用戶詳細(xì)信息

另請注意,在我們的示例中,我們推出了授權(quán)服務(wù)器,但當(dāng)然我們也可以使用其他第三方提供商,如Facebook或GitHub。

2.3。前端

現(xiàn)在,讓我們來看看客戶端應(yīng)用程序的前端配置。我們不會(huì)在這里專注于此,主要是因?yàn)槲覀円呀?jīng)在網(wǎng)站上介紹過。
 我們的客戶端應(yīng)用程序有一個(gè)非常簡單的前端;這是index.html:

<h2>Spring Security SSO</h2>
<a href="securedPage" rel="external nofollow" >Login</a>

和securedPage.html:

<h2>Secured Page</h2>
Welcome, <span th:text="${#authentication.name}">Name</span>

securedPage.html頁面需要對用戶進(jìn)行身份驗(yàn)證。如果未經(jīng)身份驗(yàn)證的用戶嘗試訪問securedPage.html,則會(huì)首先將其重定向到登錄頁面。

3. Auth服務(wù)器

現(xiàn)在讓我們在這里討論我們的授權(quán)服務(wù)器。

3.1。 Maven依賴

首先,我們需要在pom.xml中定義依賴項(xiàng):

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
 <groupId>org.springframework.security.oauth</groupId>
 <artifactId>spring-security-oauth3</artifactId>
 <version>2.3.3.RELEASE</version>
</dependency>

3.2。 OAuth配置

重要的是要理解我們將在這里一起運(yùn)行授權(quán)服務(wù)器和資源服務(wù)器,作為單個(gè)可部署單元。

讓我們從資源服務(wù)器的配置開始 :

@SpringBootApplication
@EnableResourceServer
public class AuthorizationServerApplication extends SpringBootServletInitializer {
 public static void main(String[] args) {
  SpringApplication.run(AuthorizationServerApplication.class, args);
 }
}

然后,我們將配置我們的授權(quán)服務(wù)器:

@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"); 
 }
}

請注意我們?nèi)绾蝺H使用authorization_code grant類型啟用簡單客戶端。

另外,請注意autoApprove如何設(shè)置為true,以便我們不會(huì)被重定向并手動(dòng)批準(zhǔn)任何范圍。

3.3。Security配置

首先,我們將通過application.properties禁用默認(rèn)的基本身份驗(yàn)證:

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

現(xiàn)在,讓我們轉(zhuǎn)到配置并定義一個(gè)簡單的表單登錄機(jī)制:

@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(); 
 }
}

請注意,我們使用簡單的內(nèi)存中身份驗(yàn)證,但我們可以簡單地將其替換為自定義userDetailsService。

3.4。用戶端

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

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

看完上述內(nèi)容,你們掌握怎么在Spring Security中利用OAuth2實(shí)現(xiàn)一個(gè)單點(diǎn)登錄功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

標(biāo)題名稱:怎么在SpringSecurity中利用OAuth2實(shí)現(xiàn)一個(gè)單點(diǎn)登錄功能
標(biāo)題來源:http://chinadenli.net/article8/gisoip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站Google、企業(yè)建站用戶體驗(yàn)、網(wǎng)站策劃手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作