最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringSecurity學習之自定義過濾器的實現(xiàn)代碼

 更新時間:2019年01月23日 11:09:17   作者:聶晨  
這篇文章主要介紹了SpringSecurity學習之自定義過濾器的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

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

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

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

CustomerAuthFilter:

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接口表示授權接口,通常情況下業(yè)務認證通過時會返回一個這個對象。super.setContinueChainBeforeSuccessfulAuthentication(true) 設置成true的話,會交給其他過濾器處理。

二、定義UserJoinTimeAuthentication

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

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

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.配置靜態(tài)資源不進行授權驗證
      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值改為早于今天的時間,進行登錄可以發(fā)現(xiàn):

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 詳解spring注解配置啟動過程

    詳解spring注解配置啟動過程

    這篇文章主要為大家詳細介紹了詳解spring注解配置啟動過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • SpringBoot全局異常與數(shù)據(jù)校驗的方法

    SpringBoot全局異常與數(shù)據(jù)校驗的方法

    這篇文章主要介紹了SpringBoot全局異常與數(shù)據(jù)校驗的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • Maven打包jar包沒有主屬性問題解決方案

    Maven打包jar包沒有主屬性問題解決方案

    這篇文章主要介紹了Maven打包jar包沒有主屬性問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Spring Cloud重試機制與各組件的重試總結

    Spring Cloud重試機制與各組件的重試總結

    這篇文章主要給大家介紹了關于Spring Cloud中重試機制與各組件的重試的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-11-11
  • java制作帶界面的聊天工具

    java制作帶界面的聊天工具

    這篇文章主要教大家如何利用java制作帶界面的聊天工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Java中的可重入鎖ReentrantLock簡析

    Java中的可重入鎖ReentrantLock簡析

    這篇文章主要介紹了Java中的可重入鎖ReentrantLock簡析,可重入是指同一個線程如果首次獲得了這把鎖,那么因為它是這把鎖的擁有者,因此有權利再次獲取這把鎖如果是不可重入鎖,那么第二次獲得鎖時,自己也會被鎖擋住,需要的朋友可以參考下
    2023-12-12
  • Java 獲取當前時間及實現(xiàn)時間倒計時功能【推薦】

    Java 獲取當前時間及實現(xiàn)時間倒計時功能【推薦】

    這篇文章主要介紹了Java 獲取當前時間及實現(xiàn)時間倒計時功能 ,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • Java 泛型 Generic機制實例詳解

    Java 泛型 Generic機制實例詳解

    這篇文章主要為大家介紹了Java 泛型 Generic機制實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Spring boot + LayIM + t-io 實現(xiàn)文件上傳、 監(jiān)聽用戶狀態(tài)的實例代碼

    Spring boot + LayIM + t-io 實現(xiàn)文件上傳、 監(jiān)聽用戶狀態(tài)的實例代碼

    這篇文章主要介紹了Spring boot + LayIM + t-io 實現(xiàn)文件上傳、 監(jiān)聽用戶狀態(tài)的實例代碼,需要的朋友可以參考下
    2017-12-12
  • Mybatis-plus出現(xiàn)數(shù)據(jù)庫id很大或者為負數(shù)的解決

    Mybatis-plus出現(xiàn)數(shù)據(jù)庫id很大或者為負數(shù)的解決

    本文主要介紹了Mybatis-plus出現(xiàn)數(shù)據(jù)庫id很大或者為負數(shù)的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02

最新評論

郧西县| 闸北区| 双柏县| 黑河市| 北海市| 石泉县| 栾城县| 平山县| 兴城市| 清河县| 隆化县| 泰宁县| 遂平县| 偃师市| 沅江市| 兴文县| 永康市| 诏安县| 舒兰市| 拉萨市| 赫章县| 鲁山县| 苗栗县| 延长县| 华安县| 武威市| 云阳县| 全州县| 桐庐县| 河西区| 武汉市| 镇原县| 霍邱县| 若羌县| 翁牛特旗| 宣汉县| 枣强县| 台南县| 巴彦淖尔市| 咸宁市| 永寿县|