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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产福利你懂的 | 亚洲一区二区三区不卡在线播放 | 男生同性视频twink在线 | 免费叼嘿视频 | 欧美日韩国产精品自在自线 | 扒开腿开嫩苞 | 国产日韩欧美综合一区二区三区 | 好男人影视社区www在线观看 | 国产大秀视频一区二区三区 | 精品小视频在线 | 亚洲va在线va天堂va偷拍 | 爱草影院 | 精品免费久久久久久成人影院 | 娇喘嗯嗯 轻点啊视频福利 九九九九在线精品免费视频 | 国产东北3p真实在线456视频 | 亚洲精品丝袜在线一区波多野结衣 | 国产自拍啪啪 | 狠狠干2016 | 处女呦呦 | 99热免费在线观看 | 久久综合给会久久狠狠狠 | 下雨天小说词枝 | 97青草 | 91真人毛片一级在线播放 | 日韩久久影院 | 99久久精品免费精品国产 | 四虎www| 奇米影视奇米色777欧美 | 欧美灰丝袜丝交nylons | 513热点网深夜影院影院诶 | 不卡视频一区二区 | 精品99在线观看 | 国产女主播在线播放一区二区 | 男人天堂网www | 女人把私密部位张开让男人桶 | 扒开女人屁股眼看个够 | 女人张开腿让男人桶视频免费大全 | 91porny.首页| tiny4k欧美极品在线 | 亚洲天堂精品视频 | 成人观看免费大片在线观看 |