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

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

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

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - SpringSecurity學(xué)習(xí)之自定義過(guò)濾器的實(shí)現(xiàn)代碼

SpringSecurity學(xué)習(xí)之自定義過(guò)濾器的實(shí)現(xiàn)代碼

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

這篇文章主要介紹了SpringSecurity學(xué)習(xí)之自定義過(guò)濾器的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

我們系統(tǒng)中的認(rèn)證場(chǎng)景通常比較復(fù)雜,比如說(shuō)用戶(hù)被鎖定無(wú)法登錄,限制登錄ip等。而springsecuriy最基本的是基于用戶(hù)與密碼的形式進(jìn)行認(rèn)證,由此可知它的一套驗(yàn)證規(guī)范根本無(wú)法滿(mǎn)足業(yè)務(wù)需要,因此擴(kuò)展勢(shì)在必行。那么我們可以考慮自己定義filter添加至springsecurity的過(guò)濾器棧當(dāng)中,來(lái)實(shí)現(xiàn)我們自己的驗(yàn)證需要。

本例中,基于前篇的數(shù)據(jù)庫(kù)的student表來(lái)模擬一個(gè)簡(jiǎn)單的例子:當(dāng)student的jointime在當(dāng)天之后,那么才允許登錄

一、創(chuàng)建自己定義的filter

我們先在web包下創(chuàng)建好幾個(gè)包并定義如下幾個(gè)類(lèi)

SpringSecurity學(xué)習(xí)之自定義過(guò)濾器的實(shí)現(xiàn)代碼

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;
  }
}

該類(lèi)繼承abstractauthenticationprocessingfilter,這個(gè)filter的作用是對(duì)最基本的用戶(hù)驗(yàn)證的處理,我們必須重寫(xiě)attemptauthentication方法。authentication接口表示授權(quán)接口,通常情況下業(yè)務(wù)認(rèn)證通過(guò)時(shí)會(huì)返回一個(gè)這個(gè)對(duì)象。super.setcontinuechainbeforesuccessfulauthentication(true) 設(shè)置成true的話(huà),會(huì)交給其他過(guò)濾器處理。

二、定義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;
  }
}

自定義授權(quán)方式,在這里接收username的值處理,其中g(shù)etprincipal我們可以用來(lái)存放登錄名,getcredentials可以存放密碼,這些方法來(lái)自于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;
 
/**
 * 基本的驗(yàn)證方式
 *
 * @author chen.nie
 * @date 2018/6/12
 **/
public class userjointimeauthenticationprovider implements authenticationprovider {
  private userdetailsservice userdetailsservice;
 
  public userjointimeauthenticationprovider(userdetailsservice userdetailsservice) {
    this.userdetailsservice = userdetailsservice;
  }
 
  /**
   * 認(rèn)證授權(quán),如果jointime在當(dāng)前時(shí)間之后則認(rèn)證通過(guò)
   * @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的認(rèn)證
   * @param authentication
   * @return
   */
  @override
  public boolean supports(class<?> authentication) {
    return authentication.getname().equals(userjointimeauthentication.class.getname());
  }
}

authenticationmanager會(huì)委托authenticationprovider進(jìn)行授權(quán)處理,在這里我們需要重寫(xiě)support方法,該方法定義provider支持的授權(quán)對(duì)象,那么在這里我們是對(duì)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的相關(guān)配置
 *
 * @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.配置靜態(tài)資源不進(jìn)行授權(quán)驗(yàn)證
      2.登錄地址及跳轉(zhuǎn)過(guò)后的成功頁(yè)不需要驗(yàn)證
      3.其余均進(jìn)行授權(quán)驗(yàn)證
     */
    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 {
    //設(shè)置自定義userservice
    auth.userdetailsservice(userservice);
    auth.authenticationprovider(new userjointimeauthenticationprovider(userservice));
  }
 
  @override
  public void configure(websecurity web) throws exception {
    super.configure(web);
  }
}

在這里面我們通過(guò)httpsecurity的方法來(lái)添加我們自定義的filter,一定要注意先后順序。在authenticationmanagerbuilder當(dāng)中還需要添加我們剛才定義的authenticationprovider

啟動(dòng)成功后,我們將student表里的jointime值改為早于今天的時(shí)間,進(jìn)行登錄可以發(fā)現(xiàn):

SpringSecurity學(xué)習(xí)之自定義過(guò)濾器的實(shí)現(xiàn)代碼

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

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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 日韩成人免费在线 | 精品成人佐山爱一区二区 | 看黄色片网站 | 青青草欧美 | 亚洲理论电影在线观看 | 国产精品一卡二卡三卡 | 国产精品欧美一区二区三区 | 久久66| 国内精品一区二区 | 久久国产精品视频 | av国产精品 | 国产在线一二三区 | 综合久久av | 精品久久久久久久久久久久久久 | 亚洲欧美在线精品 | 亚洲国产中文字幕 | 久久精品免费一区二区三区 | 成人激情毛片 | 激情欧美日韩一区二区 | 日韩精品一区在线 | 亚洲欧美一区二区三区国产精品 | 一级黄色大片在线观看 | 激情五月婷婷 | 精品免费久久久久久久苍 | 一级免费av | 国产精品色一区二区三区 | 福利片一区二区 | 亚洲国产一区二区三区四区 | 岛国免费 | 在线视频中文字幕 | 狠狠干av| 日本精品一区二 | 亚洲精品成人免费 | 精品免费av | 精品久久久久久久久久久久久久 | 久久国产精品无码网站 | 日本天天色 | 国产在线视频xxx | 一区二区三区影视 | 欧美成人精品一区二区 | 亚洲日本在线观看视频 |