激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解spring security之httpSecurity使用示例

詳解spring security之httpSecurity使用示例

2021-05-26 13:40一天不進步,就是退步 Java教程

這篇文章主要介紹了詳解spring security之httpSecurity使用示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

httpsecurity

類似于spring security的xml配置文件命名空間配置中的<http>元素。它允許對特定的http請求基于安全考慮進行配置。默認情況下,適用于所有的請求,但可以使用requestmatcher(requestmatcher)或者其它相似的方法進行限制。

使用示例:

最基本的基于表單的配置如下。該配置將所有的url訪問權限設定為角色名稱為"role_user".同時也定義了內存認證模式:使用用戶名"user"和密碼“password”,角色"role_user"來認證。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@configuration
@enablewebsecurity
public class formloginsecurityconfig extends websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) throws exception {
  http
   .authorizerequests()
    .antmatchers("/").hasrole("user")
    .and()
   .formlogin();
 }
 
 @override
 protected void configure(authenticationmanagerbuilder auth) throws exception {
  auth
   .inmemoryauthentication()
    .withuser("user")
      .password("password")
      .roles("user");
 }
}

 配置基于openid的認證方式

 basic示例,不使用attribute exchange

?
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
  @configuration
@enablewebsecurity
public class openidloginconfig extends websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) {
  http
   .authorizerequests()
    .antmatchers("/").hasrole("user")
    .and()
   .openidlogin()
    .permitall();
 }
 
 @override
 protected void configure(authenticationmanagerbuilder auth) throws exception {
  auth
    .inmemoryauthentication()
     // the username must match the openid of the user you are
     // logging in with
     .withuser("https://www.google.com/accounts/o8/id?id=lmkcn9xzpdsxvwg7pjymudgnndasfmobnkcrpawu")
      .password("password")
      .roles("user");
 }
}

下面展示一個更高級的示例,使用attribute exchange

?
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
43
44
45
46
47
48
49
50
51
52
53
54
55
@configuration
@enablewebsecurity
public class openidloginconfig extends websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) {
  http
   .authorizerequests()
    .antmatchers("/").hasrole("user")
    .and()
   .openidlogin()
    .loginpage("/login")
    .permitall()
    .authenticationuserdetailsservice(new autoprovisioninguserdetailsservice())
     .attributeexchange("https://www.google.com/.")
      .attribute("email")
       .type("http://axschema.org/contact/email")
       .required(true)
       .and()
      .attribute("firstname")
       .type("http://axschema.org/nameperson/first")
       .required(true)
       .and()
      .attribute("lastname")
       .type("http://axschema.org/nameperson/last")
       .required(true)
       .and()
      .and()
     .attributeexchange(".yahoo.com.")
      .attribute("email")
       .type("http://schema.openid.net/contact/email")
       .required(true)
       .and()
      .attribute("fullname")
       .type("http://axschema.org/nameperson")
       .required(true)
       .and()
      .and()
     .attributeexchange(".myopenid.com.")
      .attribute("email")
       .type("http://schema.openid.net/contact/email")
       .required(true)
       .and()
      .attribute("fullname")
       .type("http://schema.openid.net/nameperson")
       .required(true);
 }
}
 
public class autoprovisioninguserdetailsservice implements
  authenticationuserdetailsservice<openidauthenticationtoken> {
 public userdetails loaduserdetails(openidauthenticationtoken token) throws usernamenotfoundexception {
  return new user(token.getname(), "notused", authorityutils.createauthoritylist("role_user"));
 }
}

增加響應安全報文頭

默認情況下當使用websecuirtyconfigadapter的默認構造函數時激活。

僅觸發headers()方法而不觸發其它方法或者接受websecurityconfigureeradater默認的,等同于:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@configuration
  @enablewebsecurity
  public class csrfsecurityconfig extends websecurityconfigureradapter {
   @override
   protected void configure(httpsecurity http) throws exception {
    http
     .headers()
      .contenttypeoptions();
      .xssprotection()
      .cachecontrol()
      .httpstricttransportsecurity()
      .frameoptions()
      .and()
     ...;
   }
  }

取消安全報文頭,如下:

?
1
2
3
4
5
6
7
8
9
10
11
@configuration
@enablewebsecurity
public class csrfsecurityconfig extends websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) throws exception {
  http
   .headers().disable()
   ...;
 }
}

使用部分安全報文頭

觸發headers()方法的返回結果,例如,只使用headerconfigurer的cachecontroll()方法和headersconfigurer的frameoptions()方法.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@configuration
@enablewebsecurity
public class csrfsecurityconfig extends websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) throws exception {
  http
   .headers()
    .cachecontrol()
    .frameoptions()
    .and()
   ...;
 }
}

配置session管理

下面的配置展示了只允許認證用戶在同一時間只有一個實例是如何配置的。若一個用戶使用用戶名為"user"認證并且沒有退出,同一個名為“user”的試圖再次認證時,第一個用戶的session將會強制銷毀,并設置到"/login?expired"的url。

?
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
@configuration
@enablewebsecurity
public class sessionmanagementsecurityconfig extends
  websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) throws exception {
  http
   .authorizerequests()
    .anyrequest().hasrole("user")
    .and()
   .formlogin()
    .permitall()
    .and()
   .sessionmanagement()
    .maximumsessions(1)
    .expiredurl("/login?expired");
 }
 
 @override
 protected void configure(authenticationmanagerbuilder auth)
   throws exception {
  auth.
   inmemoryauthentication()
    .withuser("user")
     .password("password")
     .roles("user");
 }
}

當使用sessionmanagementconfigurer的maximumsessio(int)時不用忘記為應用配置httpsessioneventpublisher,這樣能保證過期的session能夠被清除。

在web.xml中可以這樣配置:

?
1
2
3
<listener>
 <listener-class>org.springframework.security.web.session.httpsessioneventpublisher</listener-class>;
</listener>

配置portmapper

允許配置一個從httpsecurity的getsharedobject(class)方法中獲取的portmapper。當http請求跳轉到https或者https請求跳轉到http請求時(例如我們和requireschanenl一起使用時),別的提供的securityconfigurer對象使用p誒賬戶的portmapper作為默認的portmapper。默認情況下,spring security使用portmapperimpl來映射http端口8080到https端口8443,并且將http端口的80映射到https的端口443.

配置示例如下,下面的配置將確保在spring security中的http請求端口9090跳轉到https端口9443 并且將http端口80跳轉到https443端口。

?
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
@configuration
@enablewebsecurity
public class portmappersecurityconfig extends websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) throws exception {
  http
   .authorizerequests()
    .antmatchers("/").hasrole("user")
    .and()
   .formlogin()
    .permitall()
    .and()
    // example portmapper() configuration
    .portmapper()
     .http(9090).mapsto(9443)
     .http(80).mapsto(443);
 }
 
 @override
 protected void configure(authenticationmanagerbuilder auth) throws exception {
  auth
   .inmemoryauthentication()
    .withuser("user")
     .password("password")
     .roles("user");
 }
}

配置基于容器的預認證

在這個場景中,servlet容器管理認證。

配置示例:

下面的配置使用httpservletrequest中的principal,若用戶的角色是“role_user”或者"role_admin",將會返回authentication結果。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@configuration
 @enablewebsecurity
 public class jeesecurityconfig extends websecurityconfigureradapter {
 
  @override
  protected void configure(httpsecurity http) throws exception {
   http
    .authorizerequests()
     .antmatchers("/").hasrole("user")
     .and()
    // example jee() configuration
    .jee()
     .mappableroles("role_user", "role_admin");
  }
 }

開發者希望使用基于容器預認證時,需要在web.xml中配置安全限制。例如:

?
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
<login-config>
  <auth-method>form</auth-method>
  <form-login-config>
   <form-login-page>/login</form-login-page>
   <form-error-page>/login?error</form-error-page>
  </form-login-config>
 </login-config>
 
 <security-role>
  <role-name>role_user</role-name>
 </security-role>
 <security-constraint>
  <web-resource-collection>
  <web-resource-name>public</web-resource-name>
   <description>matches unconstrained pages</description>
   <url-pattern>/login</url-pattern>
   <url-pattern>/logout</url-pattern>
   <url-pattern>/resources/</url-pattern>
  </web-resource-collection>
 </security-constraint>
 <security-constraint>
  <web-resource-collection>
   <web-resource-name>secured areas</web-resource-name>
   <url-pattern>/</url-pattern>
  </web-resource-collection>
  <auth-constraint>
   <role-name>role_user</role-name>
  </auth-constraint>
 </security-constraint>

配置基于x509的預認證

配置示例,下面的配置試圖從x509證書中提取用戶名,注意,為完成這個工作,客戶端請求證書需要配置到servlet容器中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@configuration
@enablewebsecurity
public class x509securityconfig extends websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) throws exception {
  http
   .authorizerequests()
    .antmatchers("/").hasrole("user")
    .and()
   // example x509() configuration
   .x509();
 }
}

配置remember-me服務

配置示例,下面的配置展示了如何允許基于token的remember-me的認證。若http參數中包含一個名為“remember-me”的參數,不管session是否過期,用戶記錄將會被記保存下來。

?
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
@configuration
 @enablewebsecurity
 public class remembermesecurityconfig extends websecurityconfigureradapter {
 
  @override
  protected void configure(authenticationmanagerbuilder auth)
    throws exception {
   auth
    .inmemoryauthentication()
     .withuser("user")
       .password("password")
       .roles("user");
  }
 
  @override
  protected void configure(httpsecurity http) throws exception {
   http
    .authorizerequests()
     .antmatchers("/").hasrole("user")
     .and()
    .formlogin()
     .permitall()
     .and()
    // example remember me configuration
    .rememberme();
  }
 }

限制httpservletrequest的請求訪問

配置示例,最基本的示例是配置所有的url訪問都需要角色"role_user".下面的配置要求每一個url的訪問都需要認證,并且授權訪問權限給用戶"admin"和"user".

?
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
@configuration
  @enablewebsecurity
  public class authorizeurlssecurityconfig extends websecurityconfigureradapter {
  
   @override
   protected void configure(httpsecurity http) throws exception {
    http
     .authorizerequests()
      .antmatchers("/").hasrole("user")
      .and()
     .formlogin();
   }
  
   @override
   protected void configure(authenticationmanagerbuilder auth)
     throws exception {
    auth
     .inmemoryauthentication()
      .withuser("user")
        .password("password")
        .roles("user")
        .and()
      .withuser("adminr")
        .password("password")
        .roles("admin","user");
   }
  }

同樣,也可以配置多個url。下面的配置要求以/admin/開始的url訪問權限為“admin”用戶。

?
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
@configuration
  @enablewebsecurity
  public class authorizeurlssecurityconfig extends websecurityconfigureradapter {
  
   @override
   protected void configure(httpsecurity http) throws exception {
    http
     .authorizerequests()
      .antmatchers("/admin/**").hasrole("admin")
      .antmatchers("/**").hasrole("user")
      .and()
     .formlogin();
   }
  
   @override
   protected void configure(authenticationmanagerbuilder auth)
     throws exception {
    auth
     .inmemoryauthentication()
      .withuser("user")
        .password("password")
        .roles("user")
        .and()
      .withuser("adminr")
        .password("password")
        .roles("admin","user");
   }
  }

注意:匹配起效是按照順序來的。因此如果下面的配置是無效的,因為滿足第一個規則后將不會檢查第二條規則:

?
1
2
3
4
http
 .authorizerequests()
  .antmatchers("/**").hasrole("user")
  .antmatchers("/admin/**").hasrole("admin")

增加csrf支持

默認情況下,當使用websecurityconfigureradapter時的默認構造方法時csrf是激活的。你可以使用如下方法關閉它:

?
1
2
3
4
5
6
7
8
9
10
11
@configuration
@enablewebsecurity
public class csrfsecurityconfig extends websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) throws exception {
  http
   .csrf().disable()
   ...;
 }
}

增加logout支持

默認支持,當使用websecurityconfigureradapter時logout是支持的。當用戶發出“/logout”請求時,系統將會銷毀session并且清空配置的rememberme()認證,然后清除securitycontextholder,最后跳向logout成功頁面或者登陸頁面。

?
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
@configuration
 @enablewebsecurity
 public class logoutsecurityconfig extends websecurityconfigureradapter {
 
  @override
  protected void configure(httpsecurity http) throws exception {
   http
    .authorizerequests()
     .antmatchers("/").hasrole("user")
     .and()
    .formlogin()
     .and()
    // sample logout customization
    .logout()
     .logout()
      .deletecookies("remove")
      .invalidatehttpsession(false)
      .logouturl("/custom-logout")
      .logoutsuccessurl("/logout-success");
  }
 
  @override
  protected void configure(authenticationmanagerbuilder auth)
    throws exception {
   auth
    .inmemoryauthentication()
     .withuser("user")
       .password("password")
       .roles("user");
  }
 }

匿名用戶控制

使用websecurityconfigureradapter時自動綁定。默認情況下,匿名用戶有一個anonymousauthenticationtoken標示,包含角色"role_anonymous"。

下面的配置展示了如何指定匿名用戶應該包含"role_anon".

?
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
  @configuration
@enablewebsecurity
public class anononymoussecurityconfig extends websecurityconfigureradapter {
 
 @override
 protected void configure(httpsecurity http) throws exception {
  http
   .authorizerequests()
    .antmatchers("/").hasrole("user")
    .and()
   .formlogin()
    .and()
   // sample anonymous customization
   .anonymous()
    .authorities("role_anon");
 }
 
 @override
 protected void configure(authenticationmanagerbuilder auth)
   throws exception {
  auth
   .inmemoryauthentication()
    .withuser("user")
      .password("password")
      .roles("user");
 }
}

基于表單的認證

若formloginconfigurer的loginpage(string)沒有指定,將會產生一個默認的login頁面。

示例配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@configuration
  @enablewebsecurity
  public class formloginsecurityconfig extends websecurityconfigureradapter {
  
   @override
   protected void configure(httpsecurity http) throws exception {
    http
     .authorizerequests()
      .antmatchers("/**").hasrole("user")
      .and()
     .formlogin();
   }
  
   @override
   protected void configure(authenticationmanagerbuilder auth)
     throws exception {
    auth
     .inmemoryauthentication()
      .withuser("user")
        .password("password")
        .roles("user");
   }
  }

下面的示例展示了自定義的表單認證:

?
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
@configuration
 @enablewebsecurity
 public class formloginsecurityconfig extends websecurityconfigureradapter {
 
  @override
  protected void configure(httpsecurity http) throws exception {
   http
    .authorizerequests()
     .antmatchers("/").hasrole("user")
     .and()
    .formlogin()
      .usernameparameter("j_username") // default is username
      .passwordparameter("j_password") // default is password
      .loginpage("/authentication/login") // default is /login with an http get
      .failureurl("/authentication/login?failed") // default is /login?error
      .loginprocessingurl("/authentication/login/process"); // default is /login with an http post
  }
 
  @override
  protected void configure(authenticationmanagerbuilder auth)
    throws exception {
   auth
    .inmemoryauthentication()
     .withuser("user")
       .password("password")
       .roles("user");
  }
 }

配置安全通道

為使配置生效,需至少配置一個通道的映射。

配置示例:

下面例子展示了如何將每個請求都使用https通道。

?
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
@configuration
 @enablewebsecurity
 public class channelsecurityconfig extends websecurityconfigureradapter {
 
  @override
  protected void configure(httpsecurity http) throws exception {
   http
    .authorizerequests()
     .antmatchers("/**").hasrole("user")
     .and()
    .formlogin()
     .and()
    .channelsecurity()
     .anyrequest().requiressecure();
  }
 
  @override
  protected void configure(authenticationmanagerbuilder auth)
    throws exception {
   auth
    .inmemoryauthentication()
     .withuser("user")
       .password("password")
       .roles("user");
  }
 }

配置http 基本認證

配置示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@configuration
 @enablewebsecurity
 public class httpbasicsecurityconfig extends websecurityconfigureradapter {
 
  @override
  protected void configure(httpsecurity http) throws exception {
   http
    .authorizerequests()
     .antmatchers("/**").hasrole("user").and()
     .httpbasic();
  }
 
  @override
  protected void configure(authenticationmanagerbuilder auth)
    throws exception {
   auth
    .inmemoryauthentication()
     .withuser("user")
      .password("password")
      .roles("user");
  }
 }

配置要觸發的httprequest

重寫requestmatcher方法、antmatcher()z、regexmatcher()等。

配置示例

下面的配置使httpsecurity接收以"/api/","/oauth/"開頭請求。

?
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
@configuration
 @enablewebsecurity
 public class requestmatcherssecurityconfig extends websecurityconfigureradapter {
 
  @override
  protected void configure(httpsecurity http) throws exception {
   http
    .requestmatchers()
     .antmatchers("/api/**","/oauth/**")
     .and()
    .authorizerequests()
     .antmatchers("/**").hasrole("user").and()
     .httpbasic();
  }
 
  @override
  protected void configure(authenticationmanagerbuilder auth)
    throws exception {
   auth
    .inmemoryauthentication()
     .withuser("user")
      .password("password")
      .roles("user");
  }
 }

下面的配置和上面的相同:

?
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
@configuration
  @enablewebsecurity
  public class requestmatcherssecurityconfig extends websecurityconfigureradapter {
  
   @override
   protected void configure(httpsecurity http) throws exception {
    http
     .requestmatchers()
      .antmatchers("/api/**")
      .antmatchers("/oauth/**")
      .and()
     .authorizerequests()
      .antmatchers("/**").hasrole("user").and()
      .httpbasic();
   }
  
   @override
   protected void configure(authenticationmanagerbuilder auth)
     throws exception {
    auth
     .inmemoryauthentication()
      .withuser("user")
       .password("password")
       .roles("user");
   }
  }

同樣也可以這樣使用:

?
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
@configuration
  @enablewebsecurity
  public class requestmatcherssecurityconfig extends websecurityconfigureradapter {
  
   @override
   protected void configure(httpsecurity http) throws exception {
    http
     .requestmatchers()
      .antmatchers("/api/**")
      .and()
     .requestmatchers()
      .antmatchers("/oauth/**")
      .and()
     .authorizerequests()
      .antmatchers("/**").hasrole("user").and()
      .httpbasic();
   }
  
   @override
   protected void configure(authenticationmanagerbuilder auth)
     throws exception {
    auth
     .inmemoryauthentication()
      .withuser("user")
       .password("password")
       .roles("user");
   }
  }

小結:

本文是從httpsecurity代碼中整理得來的,有助于對spring security的全面理解。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/davidwang456/p/4549344.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品国产高清一区二区三区 | av一道本| 91精品国产99久久久久久 | 亚洲国产色婷婷 | 国产一级桃视频播放 | 伊人二本二区 | 欧美日韩一区二区综合 | av在线在线 | 韩国一大片a毛片 | 福利在线小视频 | 日日狠狠久久偷偷四色综合免费 | 色骚综合 | 亚洲黑人在线观看 | 一级做a爰性色毛片免费 | 欧美日韩免费在线观看视频 | 欧美日韩在线中文 | 美国黄色小视频 | 国产日韩线路一线路二 | 成人短视频在线观看免费 | 毛片免费网 | av中文在线观看 | 欧美18videos性处按摩 | 精品亚洲视频在线 | 在线观看国产一区二区 | 精品一区二区三区电影 | 999av视频| 久久精品国产99国产精品亚洲 | 在线日韩欧美 | 黄色毛片a级 | 污黄视频在线播放 | 日本一区免费看 | 久久久线视频 | 久草视频福利在线观看 | 亚洲日本乱码在线观看 | 秋霞a级毛片在线看 | 久久精品一二三区白丝高潮 | 黄视频网址 | 一级黄色影片在线观看 | av亚洲在线观看 | 密室逃脱第一季免费观看完整在线 | 国产精品久久久久久久四虎电影 |