国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解

Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解

2021-07-17 12:36WeYunx Java教程

這篇文章主要介紹了Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

overview

最近在弄單點(diǎn)登錄,踩了不少坑,所以記錄一下,做了個(gè)簡(jiǎn)單的例子。

目標(biāo):認(rèn)證服務(wù)器認(rèn)證后獲取 token,客戶端訪問(wèn)資源時(shí)帶上 token 進(jìn)行安全驗(yàn)證。

可以直接看源碼

關(guān)鍵依賴(lài)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.1.2.release</version>
    <relativepath/>
</parent>
 
<dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-security</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupid>org.springframework.security</groupid>
      <artifactid>spring-security-test</artifactid>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupid>org.springframework.security.oauth.boot</groupid>
      <artifactid>spring-security-oauth2-autoconfigure</artifactid>
      <version>2.1.2.release</version>
    </dependency>
</dependencies>

認(rèn)證服務(wù)器

認(rèn)證服務(wù)器的關(guān)鍵代碼有如下幾個(gè)文件:

Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解

authserverapplication:

?
1
2
3
4
5
6
7
8
@springbootapplication
@enableresourceserver
public class authserverapplication {
  public static void main(string[] args) {
    springapplication.run(authserverapplication.class, args);
  }
 
}

authorizationserverconfiguration 認(rèn)證配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@configuration
@enableauthorizationserver
class authorizationserverconfiguration extends authorizationserverconfigureradapter {
  @autowired
  authenticationmanager authenticationmanager;
 
  @autowired
  tokenstore tokenstore;
 
  @autowired
  bcryptpasswordencoder encoder;
 
  @override
  public void configure(clientdetailsserviceconfigurer clients) throws exception {
    //配置客戶端
    clients
        .inmemory()
        .withclient("client")
        .secret(encoder.encode("123456")).resourceids("hi")
        .authorizedgranttypes("password","refresh_token")
        .scopes("read");
  }
 
  @override
  public void configure(authorizationserverendpointsconfigurer endpoints) throws exception {
    endpoints
        .tokenstore(tokenstore)
        .authenticationmanager(authenticationmanager);
  }
 
 
  @override
  public void configure(authorizationserversecurityconfigurer oauthserver) throws exception {
    //允許表單認(rèn)證
    oauthserver
        .allowformauthenticationforclients()
        .checktokenaccess("permitall()")
        .tokenkeyaccess("permitall()");
  }
}

代碼中配置了一個(gè) client,id 是 client,密碼 123456authorizedgranttypespasswordrefresh_token 兩種方式。

securityconfiguration 安全配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@configuration
@enablewebsecurity
public class securityconfiguration extends websecurityconfigureradapter {
  @bean
  public tokenstore tokenstore() {
    return new inmemorytokenstore();
  }
 
  @bean
  public bcryptpasswordencoder encoder() {
    return new bcryptpasswordencoder();
  }
 
  @override
  protected void configure(authenticationmanagerbuilder auth) throws exception {
    auth.inmemoryauthentication()
        .passwordencoder(encoder())
        .withuser("user_1").password(encoder().encode("123456")).roles("user")
        .and()
        .withuser("user_2").password(encoder().encode("123456")).roles("admin");
  }
 
  @override
  protected void configure(httpsecurity http) throws exception {
    // @formatter:off
    http.csrf().disable()
        .requestmatchers()
        .antmatchers("/oauth/authorize")
        .and()
        .authorizerequests()
        .anyrequest().authenticated()
        .and()
        .formlogin().permitall();
    // @formatter:on
  }
 
  @override
  @bean
  public authenticationmanager authenticationmanagerbean() throws exception {
    return super.authenticationmanagerbean();
  }
}

上面在內(nèi)存中創(chuàng)建了兩個(gè)用戶,角色分別是 useradmin。后續(xù)可考慮在數(shù)據(jù)庫(kù)或者 redis 中存儲(chǔ)相關(guān)信息。

authuser 配置獲取用戶信息的 controller:

?
1
2
3
4
5
6
7
8
@restcontroller
public class authuser {
    @getmapping("/oauth/user")
    public principal user(principal principal) {
      return principal;
    }
 
}

application.yml 配置,主要就是配置個(gè)端口號(hào):

?
1
2
3
4
5
6
7
8
---
spring:
 profiles:
  active: dev
 application:
  name: auth-server
server:
 port: 8101

客戶端配置

客戶端的配置比較簡(jiǎn)單,主要代碼結(jié)構(gòu)如下:

Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解

application.yml 配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
---
spring:
 profiles:
  active: dev
 application:
  name: client
 
server:
 port: 8102
security:
 oauth2:
  client:
   client-id: client
   client-secret: 123456
   access-token-uri: http://localhost:8101/oauth/token
   user-authorization-uri: http://localhost:8101/oauth/authorize
   scope: read
   use-current-uri: false
  resource:
   user-info-uri: http://localhost:8101/oauth/user

這里主要是配置了認(rèn)證服務(wù)器的相關(guān)地址以及客戶端的 id 和 密碼。user-info-uri 配置的就是服務(wù)器端獲取用戶信息的接口。

hellocontroller 訪問(wèn)的資源,配置了 admin 的角色才可以訪問(wèn):

?
1
2
3
4
5
6
7
8
@restcontroller
public class hellocontroller {
  @requestmapping("/hi")
  @preauthorize("hasrole('admin')")
  public responseentity<string> hi() {
    return responseentity.ok().body("auth success!");
  }
}

websecurityconfiguration 相關(guān)安全配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@configuration
@enableoauth2sso
@enableglobalmethodsecurity(prepostenabled = true)
class websecurityconfiguration extends websecurityconfigureradapter {
 
  @override
  public void configure(httpsecurity http) throws exception {
 
    http
        .csrf().disable()
        // 基于token,所以不需要session
       .sessionmanagement().sessioncreationpolicy(sessioncreationpolicy.stateless)
        .and()
        .authorizerequests()
        .anyrequest().authenticated();
  }
 
 
}

其中 @enableglobalmethodsecurity(prepostenabled = true) 開(kāi)啟后,spring security 的 @preauthorize,@postauthorize 注解才可以使用。

@enableoauth2sso 配置了單點(diǎn)登錄。

clientapplication

?
1
2
3
4
5
6
7
8
@springbootapplication
@enableresourceserver
public class clientapplication {
  public static void main(string[] args) {
    springapplication.run(clientapplication.class, args);
  }
 
}

驗(yàn)證

啟動(dòng)項(xiàng)目后,我們使用 postman 來(lái)進(jìn)行驗(yàn)證。

首先是獲取 token:

Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解

選擇 post 提交,地址為驗(yàn)證服務(wù)器的地址,參數(shù)中輸入 username,password,grant_typescope ,其中 grant_type 需要輸入 password

然后在下面等 authorization 標(biāo)簽頁(yè)中,選擇 basic auth,然后輸入 client 的 id 和 password。

?
1
2
3
4
5
6
7
{
  "access_token": "02f501a9-c482-46d4-a455-bf79a0e0e728",
  "token_type": "bearer",
  "refresh_token": "0e62dddc-4f51-4cb5-81c3-5383fddbb81b",
  "expires_in": 41741,
  "scope": "read"
}

此時(shí)就可以獲得 access_token 為: 02f501a9-c482-46d4-a455-bf79a0e0e728。需要注意的是這里是用 user_2 獲取的 token,即角色是 admin

然后我們?cè)龠M(jìn)行獲取資源的驗(yàn)證:

Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解

使用 get 方法,參數(shù)中輸入 access_token,值輸入 02f501a9-c482-46d4-a455-bf79a0e0e728

點(diǎn)擊提交后即可獲取到結(jié)果。

如果我們不加上 token ,則會(huì)提示無(wú)權(quán)限。同樣如果我們換上 user_1 獲取的 token,因 user_1 的角色是 user,此資源需要 admin 權(quán)限,則此處還是會(huì)獲取失敗。

簡(jiǎn)單的例子就到這,后續(xù)有時(shí)間再加上其它功能吧,謝謝~

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://segmentfault.com/a/1190000018323304

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 激情一区二区 | 在线欧美 | 97久久精品午夜一区二区 | 亚洲欧美另类在线 | 亚洲在线一区二区 | 免费av在线网站 | 婷婷在线视频 | 奇米二区 | 日韩激情网 | 黄色直接看| 天天干天天操天天射 | 久久久久久九九 | 欧美午夜一区二区三区免费大片 | 国产一区二区三区在线观看免费 | 日韩欧美亚洲精品 | 91午夜理伦私人影院 | 欧美精品不卡 | 久久久国产精品视频 | 中文字幕亚洲一区 | 亚洲成人福利 | 在线精品亚洲 | 99在线免费观看 | 欧美在线a | 中文字幕视频 | 色接久久 | 午夜a区 | 成人av福利 | 97在线观看视频 | 可以免费看黄的网站 | 天天操天天操 | 欧美性猛片 | 欧美区国产 | 亚洲免费a | 综合久久亚洲 | 国产精品亲子伦av一区二区三区 | 91中文字幕在线观看 | 日本在线视频免费观看 | 亚洲一区免费观看 | 最新黄色网址在线播放 | 玖玖综合网| 九九精品视频在线观看 |