一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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

服務器之家 - 編程語言 - Java教程 - SpringSecurity學習之自定義過濾器的實現代碼

SpringSecurity學習之自定義過濾器的實現代碼

2021-07-12 12:45聶晨 Java教程

這篇文章主要介紹了SpringSecurity學習之自定義過濾器的實現代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

我們系統中的認證場景通常比較復雜,比如說用戶被鎖定無法登錄,限制登錄ip等。而springsecuriy最基本的是基于用戶與密碼的形式進行認證,由此可知它的一套驗證規范根本無法滿足業務需要,因此擴展勢在必行。那么我們可以考慮自己定義filter添加至springsecurity的過濾器棧當中,來實現我們自己的驗證需要。

本例中,基于前篇的數據庫的student表來模擬一個簡單的例子:當student的jointime在當天之后,那么才允許登錄

一、創建自己定義的filter

我們先在web包下創建好幾個包并定義如下幾個類

SpringSecurity學習之自定義過濾器的實現代碼

customerauthfilter:

?
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
package com.bdqn.lyrk.security.study.web.filter;
 
import com.bdqn.lyrk.security.study.web.authentication.userjointimeauthentication;
import org.springframework.security.authentication.authenticationmanager;
import org.springframework.security.core.authentication;
import org.springframework.security.core.authenticationexception;
import org.springframework.security.web.authentication.abstractauthenticationprocessingfilter;
import org.springframework.security.web.util.matcher.antpathrequestmatcher;
 
import javax.servlet.servletexception;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
 
 
public class customerauthfilter extends abstractauthenticationprocessingfilter {
 
  private authenticationmanager authenticationmanager;
 
 
  public customerauthfilter(authenticationmanager authenticationmanager) {
 
    super(new antpathrequestmatcher("/login", "post"));
    this.authenticationmanager = authenticationmanager;
 
  }
 
  @override
  public authentication attemptauthentication(httpservletrequest request, httpservletresponse response) throws authenticationexception, ioexception, servletexception {
    string username = request.getparameter("username");
    userjointimeauthentication usernamepasswordauthenticationtoken =new userjointimeauthentication(username);
    authentication authentication = this.authenticationmanager.authenticate(usernamepasswordauthenticationtoken);
    if (authentication != null) {
      super.setcontinuechainbeforesuccessfulauthentication(true);
    }
    return authentication;
  }
}

該類繼承abstractauthenticationprocessingfilter,這個filter的作用是對最基本的用戶驗證的處理,我們必須重寫attemptauthentication方法。authentication接口表示授權接口,通常情況下業務認證通過時會返回一個這個對象。super.setcontinuechainbeforesuccessfulauthentication(true) 設置成true的話,會交給其他過濾器處理。

二、定義userjointimeauthentication

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.bdqn.lyrk.security.study.web.authentication;
 
import org.springframework.security.authentication.abstractauthenticationtoken;
 
public class userjointimeauthentication extends abstractauthenticationtoken {
  private string username;
 
  public userjointimeauthentication(string username) {
    super(null);
    this.username = username;
  }
 
 
  @override
  public object getcredentials() {
    return null;
  }
 
  @override
  public object getprincipal() {
    return username;
  }
}

自定義授權方式,在這里接收username的值處理,其中getprincipal我們可以用來存放登錄名,getcredentials可以存放密碼,這些方法來自于authentication接口

三、定義authenticationprovider

?
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
package com.bdqn.lyrk.security.study.web.authentication;
 
import com.bdqn.lyrk.security.study.app.pojo.student;
import org.springframework.security.authentication.authenticationprovider;
import org.springframework.security.core.authentication;
import org.springframework.security.core.authenticationexception;
import org.springframework.security.core.userdetails.userdetails;
import org.springframework.security.core.userdetails.userdetailsservice;
 
import java.util.date;
 
/**
 * 基本的驗證方式
 *
 * @author chen.nie
 * @date 2018/6/12
 **/
public class userjointimeauthenticationprovider implements authenticationprovider {
  private userdetailsservice userdetailsservice;
 
  public userjointimeauthenticationprovider(userdetailsservice userdetailsservice) {
    this.userdetailsservice = userdetailsservice;
  }
 
  /**
   * 認證授權,如果jointime在當前時間之后則認證通過
   * @param authentication
   * @return
   * @throws authenticationexception
   */
  @override
  public authentication authenticate(authentication authentication) throws authenticationexception {
    string username = (string) authentication.getprincipal();
    userdetails userdetails = this.userdetailsservice.loaduserbyusername(username);
    if (!(userdetails instanceof student)) {
      return null;
    }
    student student = (student) userdetails;
    if (student.getjointime().after(new date()))
      return new userjointimeauthentication(username);
    return null;
  }
 
  /**
   * 只處理userjointimeauthentication的認證
   * @param authentication
   * @return
   */
  @override
  public boolean supports(class<?> authentication) {
    return authentication.getname().equals(userjointimeauthentication.class.getname());
  }
}

authenticationmanager會委托authenticationprovider進行授權處理,在這里我們需要重寫support方法,該方法定義provider支持的授權對象,那么在這里我們是對userjointimeauthentication處理。

四、websecurityconfig

?
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
56
57
package com.bdqn.lyrk.security.study.app.config;
 
import com.bdqn.lyrk.security.study.app.service.userservice;
import com.bdqn.lyrk.security.study.web.authentication.userjointimeauthenticationprovider;
import com.bdqn.lyrk.security.study.web.filter.customerauthfilter;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.security.config.annotation.authentication.builders.authenticationmanagerbuilder;
import org.springframework.security.config.annotation.web.builders.httpsecurity;
import org.springframework.security.config.annotation.web.builders.websecurity;
import org.springframework.security.config.annotation.web.configuration.enablewebsecurity;
import org.springframework.security.config.annotation.web.configuration.websecurityconfigureradapter;
import org.springframework.security.web.authentication.usernamepasswordauthenticationfilter;
 
/**
 * spring-security的相關配置
 *
 * @author chen.nie
 * @date 2018/6/7
 **/
@enablewebsecurity
public class websecurityconfig extends websecurityconfigureradapter {
 
  @autowired
  private userservice userservice;
 
  @override
  protected void configure(httpsecurity http) throws exception {
    /*
      1.配置靜態資源不進行授權驗證
      2.登錄地址及跳轉過后的成功頁不需要驗證
      3.其余均進行授權驗證
     */
    http.
        authorizerequests().antmatchers("/static/**").permitall().
        and().authorizerequests().antmatchers("/user/**").hasrole("7022").
        and().authorizerequests().anyrequest().authenticated().
        and().formlogin().loginpage("/login").successforwardurl("/toindex").permitall()
        .and().logout().logouturl("/logout").invalidatehttpsession(true).deletecookies().permitall()
    ;
 
    http.addfilterbefore(new customerauthfilter(authenticationmanager()), usernamepasswordauthenticationfilter.class);
 
 
  }
 
  @override
  protected void configure(authenticationmanagerbuilder auth) throws exception {
    //設置自定義userservice
    auth.userdetailsservice(userservice);
    auth.authenticationprovider(new userjointimeauthenticationprovider(userservice));
  }
 
  @override
  public void configure(websecurity web) throws exception {
    super.configure(web);
  }
}

在這里面我們通過httpsecurity的方法來添加我們自定義的filter,一定要注意先后順序。在authenticationmanagerbuilder當中還需要添加我們剛才定義的authenticationprovider

啟動成功后,我們將student表里的jointime值改為早于今天的時間,進行登錄可以發現:

SpringSecurity學習之自定義過濾器的實現代碼

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

原文鏈接:https://www.cnblogs.com/niechen/p/9174096.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 羞羞色男人的天堂伊人久久 | 无遮挡h肉动漫在线观看电车 | 好男人资源免费播放 | 欧美搞逼视频 | 国产免费视频 | 亚洲欧美综合在线观看 | 国产成人亚洲综合网站不卡 | 午夜 在线播放 | 91九色porn偷拍在线 | 日韩毛片在线影视 | 国产精品videosse | 婷婷久久综合九色综合九七 | 五月天视频网 | 1024免费观看完整版在线播放 | 99午夜| 色呦阁| 美女草b | 国产成人啪精品午夜在线播放 | 精品一区视频 | 午夜私人影院在线观看 | 大陆男同志gayxxx| 国产精品手机视频一区二区 | 7777色鬼xxxx欧美色夫 | 婷婷草| www免费看| 亚洲AV蜜桃永久无码精品无码网 | 国产在线成人精品 | 免费久久久久 | 九九精品免视看国产成人 | 日本www色视频成人免费 | 美女福利网站 | 日本老妇成熟 | 精品久久久久久久久免费影院 | 成年性香蕉漫画在线观看 | 亚洲人的天堂男人爽爽爽 | 日韩操比视频 | 91啪在线观看国产在线 | 国产精品免费小视频 | 五月天网站 | 韩国最新三级网站在线播放 | 91普通话国产对白在线 |