Spring Security - 實(shí)現(xiàn)圖形驗(yàn)證碼(二)

Spring Security - 使用自定義AuthenticationProvider實(shí)現(xiàn)圖形驗(yàn)證碼

前面通過(guò)過(guò)濾器實(shí)現(xiàn)驗(yàn)證碼校驗(yàn),是從servlet層面實(shí)現(xiàn)的配置簡(jiǎn)單污朽,易于理解。Spring Security 還提供了另一種更為靈活的方法龙考。

通過(guò)自定義認(rèn)證同樣可以實(shí)現(xiàn)蟆肆。

一、自定義AuthenticationProvider

我們只是在常規(guī)的密碼校驗(yàn)前加了一層判斷圖形驗(yàn)證碼的認(rèn)證條件

所以可以通過(guò)繼承DaoAuthenticationProvider稍加修改即可實(shí)現(xiàn)需求

  • 通過(guò)構(gòu)造方法注入自定義的MyUserDetailsService晦款、MyPasswordEncoder
  • 重新additionalAuthenticationChecks()方法
  • 添加實(shí)現(xiàn)圖形驗(yàn)證碼校驗(yàn)邏輯
@Component
public class MyAuthenticationProvider extends DaoAuthenticationProvider {

    //構(gòu)造方法注入MyUserDetailsService和MyPasswordEncoder
    public MyAuthenticationProvider(MyUserDetailsService myUserDetailService, MyPasswordEncoder myPasswordEncoder) {
        this.setUserDetailsService(myUserDetailService);
        this.setPasswordEncoder(myPasswordEncoder);
    }

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        //實(shí)現(xiàn)圖形驗(yàn)證碼邏輯
        
        //驗(yàn)證碼錯(cuò)誤炎功,拋出異常
        if (!details.getImageCodeIsRight()) {
            throw new VerificationCodeException("驗(yàn)證碼錯(cuò)誤");
        }
        //調(diào)用父類完成密碼校驗(yàn)認(rèn)證
        super.additionalAuthenticationChecks(userDetails, authentication);
    }
}
@Component
public class MyPasswordEncoder implements PasswordEncoder {
    private static final PasswordEncoder INSTANCE = new MyPasswordEncoder();

    public String encode(CharSequence rawPassword) {
        return rawPassword.toString();
    }

    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return rawPassword.toString().equals(encodedPassword);
    }

    public static PasswordEncoder getInstance() {
        return INSTANCE;
    }

    private MyPasswordEncoder() {
    }
}
@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("查詢數(shù)據(jù)庫(kù)");
        //查詢用戶信息
        User user = userMapper.findByUserName(username);
        if (user==null){
            throw new UsernameNotFoundException(username+"用戶不存在");
        }
        //重新填充roles
        user.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRoles()));
        return user;
    }
}

authentication封裝了用戶的登錄驗(yàn)證信息

但是圖形驗(yàn)證碼是存在session中的,我們需要將request請(qǐng)求一同封裝進(jìn)authentication

這樣就可以在additionalAuthenticationChecks()中添加驗(yàn)證碼的校驗(yàn)邏輯了

其實(shí)我們要驗(yàn)證的所有信息可以當(dāng)成一個(gè)主體Principal缓溅,通過(guò)繼承實(shí)現(xiàn)Principal蛇损,經(jīng)過(guò)包裝,返回的Authentication認(rèn)證實(shí)體坛怪。

public interface Authentication extends Principal, Serializable {
    //獲取主體權(quán)限列表
    Collection<? extends GrantedAuthority> getAuthorities();
    //獲取主題憑證淤齐,一般為密碼
    Object getCredentials();
    //獲取主體攜帶的詳細(xì)信息
    Object getDetails();
    //獲取主體,一般為一個(gè)用戶名
    Object getPrincipal();
    //主體是否驗(yàn)證成功
    boolean isAuthenticated();
    
    void setAuthenticated(boolean var1) throws IllegalArgumentException;
}

一次完整的認(rèn)證通常包含多個(gè)AuthenticationProvider

ProviderManager管理

ProviderManagerUsernamePasswordAuthenticationFilter 調(diào)用

也就是說(shuō)袜匿,所有的 AuthenticationProvider包含的Authentication都來(lái)源于UsernamePasswordAuthenticationFilter

二更啄、自定義AuthenticationDetailsSource

UsernamePasswordAuthenticationFilter本身并沒(méi)有設(shè)置用戶詳細(xì)信息的流程,而且是通過(guò)標(biāo)準(zhǔn)接口 AuthenticationDetailsSource構(gòu)建的居灯,這意味著它是一個(gè)允許定制的特性锈死。

public interface AuthenticationDetailsSource<C, T> {
    T buildDetails(C var1);
}

UsernamePasswordAuthenticationFilter中使用的AuthenticationDetailsSource是一個(gè)標(biāo)準(zhǔn)的Web認(rèn)證源贫堰,攜帶

的是用戶的sessionIdIP地址

public class WebAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> {
    public WebAuthenticationDetailsSource() {
    }

    public WebAuthenticationDetails buildDetails(HttpServletRequest context) {
        return new WebAuthenticationDetails(context);
    }
}
public class WebAuthenticationDetails implements Serializable {
    private static final long serialVersionUID = 530L;
    private final String remoteAddress;
    private final String sessionId;

    public WebAuthenticationDetails(HttpServletRequest request) {
        this.remoteAddress = request.getRemoteAddr();
        HttpSession session = request.getSession(false);
        this.sessionId = session != null ? session.getId() : null;
    }

    private WebAuthenticationDetails(String remoteAddress, String sessionId) {
        this.remoteAddress = remoteAddress;
        this.sessionId = sessionId;
    }

可以看到我們是可以拿到HttpServletRequest的,我們可以實(shí)現(xiàn)自己WebAuthenticationDetails待牵,并擴(kuò)展自己需要的信息

public class MyWebAuthenticationDetails extends WebAuthenticationDetails {

    private boolean imageCodeIsRight;

    public boolean getImageCodeIsRight(){
        return this.imageCodeIsRight;
    }

    //補(bǔ)充用戶提交的驗(yàn)證碼和session保存的驗(yàn)證碼
    public MyWebAuthenticationDetails(HttpServletRequest request) {
        super(request);
        String captcha = request.getParameter("captcha");
        HttpSession session = request.getSession();
        String saveCaptcha = (String) session.getAttribute("captcha");
        if (StringUtils.isNotEmpty(saveCaptcha)){
            session.removeAttribute("captcha");
        }
        if (StringUtils.isNotEmpty(captcha) && captcha.equals(saveCaptcha)){
            this.imageCodeIsRight = true;
        }
    }
}

將他提供給一個(gè)自定義的AuthenticationDetailsSource

@Component
public class MyAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> {

    @Override
    public WebAuthenticationDetails buildDetails(HttpServletRequest request) {
        return new MyWebAuthenticationDetails(request);
    }
}

有了HttpServletRequest其屏,接下來(lái)再去實(shí)現(xiàn)我們的圖形驗(yàn)證碼驗(yàn)證邏輯

@Component
public class MyAuthenticationProvider extends DaoAuthenticationProvider {

    //構(gòu)造方法注入U(xiǎn)serDetailsService和PasswordEncoder
    public MyAuthenticationProvider(MyUserDetailsService myUserDetailService, MyPasswordEncoder myPasswordEncoder) {
        this.setUserDetailsService(myUserDetailService);
        this.setPasswordEncoder(myPasswordEncoder);
    }

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        //實(shí)現(xiàn)圖形驗(yàn)證碼邏輯
        //獲取詳細(xì)信息
        MyWebAuthenticationDetails details = (MyWebAuthenticationDetails) authentication.getDetails();
        //驗(yàn)證碼錯(cuò)誤,拋出異常
        if (!details.getImageCodeIsRight()) {
            throw new VerificationCodeException("驗(yàn)證碼錯(cuò)誤");
        }
        //調(diào)用父類完成密碼校驗(yàn)認(rèn)證
        super.additionalAuthenticationChecks(userDetails, authentication);
    }
}

三缨该、應(yīng)用自定義認(rèn)證

最后修改WebSecurityConfig 使其應(yīng)用自定義的MyAuthenticationDetailsSource偎行、MyAuthenticationProvider

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;
    @Autowired
    private MyUserDetailsService myUserDetailsService;
    @Autowired
    private MyAuthenticationDetailsSource myWebAuthenticationDetailsSource;
    @Autowired
    private MyAuthenticationProvider myAuthenticationProvider;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        //應(yīng)用MyAuthenticationProvider
        auth.authenticationProvider(myAuthenticationProvider);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/api/**").hasRole("ADMIN")
                .antMatchers("/user/api/**").hasRole("USER")
                .antMatchers("/app/api/**","/captcha.jpg").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                //AuthenticationDetailsSource
                .authenticationDetailsSource(myWebAuthenticationDetailsSource)
                .loginPage("/myLogin.html")
                // 指定處理登錄請(qǐng)求的路徑,修改請(qǐng)求的路徑,默認(rèn)為/login
                .loginProcessingUrl("/mylogin").permitAll()
                .failureHandler(new MyAuthenticationFailureHandler())
                .and()
                .csrf().disable();
    }



    @Bean
    public Producer kaptcha() {
        //配置圖形驗(yàn)證碼的基本參數(shù)
        Properties properties = new Properties();
        //圖片寬度
        properties.setProperty("kaptcha.image.width", "150");
        //圖片長(zhǎng)度
        properties.setProperty("kaptcha.image.height", "50");
        //字符集
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789");
        //字符長(zhǎng)度
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        Config config = new Config(properties);
        //使用默認(rèn)的圖形驗(yàn)證碼實(shí)現(xiàn)贰拿,也可以自定義
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

}

四蛤袒、測(cè)試

啟動(dòng)項(xiàng)目

訪問(wèn)api:http://localhost:8080/user/api/hi

image-20201019160019522.png

輸入正確用戶名密碼,正確驗(yàn)證碼

訪問(wèn)成功

頁(yè)面顯示hi,user.

重啟項(xiàng)目

輸入正確用戶名密碼膨更,錯(cuò)誤驗(yàn)證碼

訪問(wèn)失敗

返回失敗報(bào)文

{
"error_code": 401,
"error_name": "com.yang.springsecurity.exception.VerificationCodeException",
"message": "請(qǐng)求失敗,圖形驗(yàn)證碼校驗(yàn)異常"
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末妙真,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子荚守,更是在濱河造成了極大的恐慌珍德,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件矗漾,死亡現(xiàn)場(chǎng)離奇詭異锈候,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)敞贡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)泵琳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人誊役,你說(shuō)我怎么就攤上這事获列。” “怎么了蛔垢?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵蛛倦,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我啦桌,道長(zhǎng)溯壶,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任甫男,我火速辦了婚禮且改,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘板驳。我一直安慰自己又跛,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布若治。 她就那樣靜靜地躺著慨蓝,像睡著了一般感混。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上礼烈,一...
    開(kāi)封第一講書(shū)人閱讀 51,737評(píng)論 1 305
  • 那天弧满,我揣著相機(jī)與錄音,去河邊找鬼此熬。 笑死庭呜,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的犀忱。 我是一名探鬼主播募谎,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼阴汇!你這毒婦竟也來(lái)了数冬?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤搀庶,失蹤者是張志新(化名)和其女友劉穎拐纱,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體地来,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年熙掺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了未斑。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡币绩,死狀恐怖蜡秽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情缆镣,我是刑警寧澤芽突,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站董瞻,受9級(jí)特大地震影響寞蚌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜钠糊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一挟秤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧抄伍,春花似錦艘刚、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)箩朴。三九已至,卻和暖如春秋度,著一層夾襖步出監(jiān)牢的瞬間炸庞,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工静陈, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留燕雁,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓鲸拥,卻偏偏與公主長(zhǎng)得像拐格,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子刑赶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355