欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解SpringSecurity如何實現(xiàn)前后端分離

 更新時間:2023年03月30日 10:53:36   作者:DigitalDreamer  
這篇文章主要為大家介紹了詳解SpringSecurity如何實現(xiàn)前后端分離,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

Spring Security存在的問題

前后端分離模式是指由前端控制頁面路由,后端接口也不再返回html數(shù)據(jù),而是直接返回業(yè)務數(shù)據(jù),數(shù)據(jù)一般是JSON格式。

Spring Security默認支持的表單認證方式,會存在兩個問題:

  • 表單的HTTP Content-Type是application/x-www-form-urlencoded,不是JSON格式。
  • Spring Security會在用戶未登錄或登錄成功時會發(fā)起頁面重定向,重定向到登錄頁或登錄成功頁面。

要支持前后端分離的模式,我們要對這些問題進行改造。

改造Spring Security的認證方式

1. 登錄請求改成JSON方式

Spring Security默認提供賬號密碼認證方式,具體實現(xiàn)是在UsernamePasswordAuthenticationFilter類中。因為是表單提交,所以Filter中用request.getParameter(this.usernameParameter) 來獲取用戶賬號和密碼信息。當我們將請求類型改成application/json后,getParameter方法就獲取不到信息。

要解決這個問題,就要新建一個Filter來替換UsernamePasswordAuthenticationFilter ,然后重新實現(xiàn)獲取用戶的方法。

1.1 新建JSON版Filter - JsonUsernamePasswordAuthenticationFilter

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.data.util.Pair;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
public class JsonUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {
        if (!request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }
        Pair<String, String> usernameAndPassword = obtainUsernameAndPassword(request);
        String username = usernameAndPassword.getFirst();
        username = (username != null) ? username.trim() : "";
        String password = usernameAndPassword.getSecond();
        password = (password != null) ? password : "";
        UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username,
                password);
        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
    }
    @SneakyThrows
    private Pair<String, String> obtainUsernameAndPassword(HttpServletRequest request) {
        JSONObject map = JSON.parseObject(request.getInputStream(), JSONObject.class);
        return Pair.of(map.getString(getUsernameParameter()), map.getString(getPasswordParameter()));
    }
}

1.2 新建Configurer來注冊Filter - JsonUsernamePasswordLoginConfigurer

注冊Filter有兩種方式,一給是直接調(diào)用httpSecurity的addFilterAt(Filter filter, Class<? extends Filter> atFilter) ,另一個是通過AbstractHttpConfigurer 來注冊。因為我們繼承了原來的賬密認證方式,考慮到兼容原有邏輯,我們選擇Spring Security默認的Configurer注冊方式來注冊Filter。AbstractHttpConfigurer 在初始化 UsernamePasswordAuthenticationFilter 的時候,會額外設置一些信息。

新建一個JsonUsernamePasswordLoginConfigurer直接繼承AbstractAuthenticationFilterConfigurer。

import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.configurers.AbstractAuthenticationFilterConfigurer;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
public final class JsonUsernamePasswordLoginConfigurer<H extends HttpSecurityBuilder<H>> extends
        AbstractAuthenticationFilterConfigurer<H, JsonUsernamePasswordLoginConfigurer<H>, JsonUsernamePasswordAuthenticationFilter> {
	public JsonUsernamePasswordLoginConfigurer() {
		super(new JsonUsernamePasswordAuthenticationFilter(), null);
	}
        // 去掉登錄處理接口的權(quán)限校驗
	@Override
	protected RequestMatcher createLoginProcessingUrlMatcher(String loginProcessingUrl) {
		return new AntPathRequestMatcher(loginProcessingUrl, "POST");
	}
}

1.3 將自定義Configurer注冊到HttpSecurity上

這一步比較簡單,我們先關閉原來的表單認證,然后注冊我們自己的Configurer,實現(xiàn)JSON版認證方式。

http
    .formLogin().disable()
    .apply(new JsonUsernamePasswordLoginConfigurer<>())

經(jīng)過這三步,Spring Security就能識別JSON格式的用戶信息了。

2. 關閉頁面重定向

有幾個場景會觸發(fā)Spring Security的重定向:

  • 當前用戶未登錄,重定向到登錄頁面
  • 登錄驗證成功,重定向到默認頁面
  • 退出登錄成功,重定向到默認頁面

我們要對這幾個場景分別處理,給前端返回JSON格式的描述信息,而不是發(fā)起重定向。

2.1 當前用戶未登錄

用戶發(fā)起未登錄的請求會被AuthorizationFilter攔截,并拋出AccessDeniedException異常。異常被AuthenticationEntryPoint處理,默認會觸發(fā)重定向到登錄頁。Spring Security開放了配置,允許我們自定義AuthenticationEntryPoint。那么我們就通過自定義AuthenticationEntryPoint來取消重定向行為,將接口改為返回JSON信息。

http.exceptionHandling(it -> it.authenticationEntryPoint((request, response, authException) -> {
        String msg = "{\\"msg\\": \\"用戶未登錄\\"}";
        response.setStatus(HttpStatus.FORBIDDEN.value());
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        PrintWriter writer = response.getWriter();
        writer.write(msg);
        writer.flush();
        writer.close();
    }))

2.2 登錄成功/失敗

登錄成功或失敗后的行為由AuthenticationSuccessHandler 和AuthenticationFailureHandler 來控制。原來是在**formLogin(it->it.successHandler(null))**里配置它們,由于上面我們自定義了JsonUsernamePasswordLoginConfigurer ,所以要在我們自己的Configurer 上配置AuthenticationSuccessHandler 和AuthenticationFailureHandler 。

http
    .formLogin().disable()
    .apply((SecurityConfigurerAdapter) new JsonUsernamePasswordLoginConfigurer<>()
            .successHandler((request, response, authentication) -> {
		String msg = "{\\"msg\\": \\"登錄成功\\"}";
		response.setStatus(HttpStatus.OK.value());
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
		PrintWriter writer = response.getWriter();
		writer.write(msg);
		writer.flush();
		writer.close();
            })
            .failureHandler((request, response, exception) -> {
                String msg = "{\\"msg\\": \\"用戶名密碼錯誤\\"}";
                response.setStatus(HttpStatus.UNAUTHORIZED.value());
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
		PrintWriter writer = response.getWriter();
		writer.write(msg);
		writer.flush();
		writer.close();
            }));

2.3 退出登錄

退出登錄是在LogoutConfigurer配置,退出成功后,會觸發(fā)LogoutSuccessHandler操作,我們也重寫它的處理邏輯。

http.logout(it -> it
        .logoutSuccessHandler((request, response, authentication) -> {
            String msg = "{\\"msg\\": \\"退出成功\\"}";
            response.setStatus(HttpStatus.OK.value());
            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
            PrintWriter writer = response.getWriter();
            writer.write(msg);
            writer.flush();
            writer.close();
        }))

3. 最后處理CSRF校驗

前后端分離后,如果頁面是放在CDN上,那么前段直至發(fā)起登錄請求之前,都沒機會從后端拿到CSRF Token。所以,登錄請求會被Spring Security的CsrfFilter攔截。

要避免這種情況,一種方式是發(fā)起登錄請求前,先調(diào)用接口獲取CSRF Token;另一種方式是先關閉登錄接口的CSRF校驗。方式二配置如下:

http.csrf(it -> it.ignoringRequestMatchers("/login", "POST"))

總結(jié)

至此,前后端分離的基本工作就完成了。在實踐的過程中必然還有其他問題,歡迎大家一起交流探討。

以上就是詳解SpringSecurity如何實現(xiàn)前后端分離的詳細內(nèi)容,更多關于SpringSecurity前后端分離的資料請關注腳本之家其它相關文章!

相關文章

  • 使用idea啟動DataX的方法示例

    使用idea啟動DataX的方法示例

    這篇文章主要介紹了使用idea啟動DataX的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • 用遞歸查找有序二維數(shù)組的方法詳解

    用遞歸查找有序二維數(shù)組的方法詳解

    本篇文章是對用遞歸查找有序二維數(shù)組的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • idea不能搜索任何插件的問題解決

    idea不能搜索任何插件的問題解決

    本文主要介紹了idea不能搜索任何插件的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • MybatisPlus查詢數(shù)據(jù)日期格式化問題解決方法

    MybatisPlus查詢數(shù)據(jù)日期格式化問題解決方法

    MyBatisPlus是MyBatis的增強工具,支持常規(guī)的CRUD操作以及復雜的聯(lián)表查詢等功能,這篇文章主要給大家介紹了關于MybatisPlus查詢數(shù)據(jù)日期格式化問題的解決方法,需要的朋友可以參考下
    2023-10-10
  • SpringBoot自定義對象參數(shù)實現(xiàn)自動類型轉(zhuǎn)換與格式化

    SpringBoot自定義對象參數(shù)實現(xiàn)自動類型轉(zhuǎn)換與格式化

    SpringBoot 通過自定義對象參數(shù),可以實現(xiàn)自動類型轉(zhuǎn)換與格式化,并可以級聯(lián)封裝,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-09-09
  • 解決Mybatis?plus實體類屬性與表字段不一致的問題

    解決Mybatis?plus實體類屬性與表字段不一致的問題

    這篇文章主要介紹了Mybatis?plus實體類屬性與表字段不一致解決方法,文末給大家提到了Mybatis-plus中數(shù)據(jù)庫表名和表字段名的相關知識,需要的朋友可以參考下
    2022-07-07
  • mybatis中xml之trim屬性說明

    mybatis中xml之trim屬性說明

    這篇文章主要介紹了mybatis中xml之trim屬性說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 第三方包jintellitype實現(xiàn)Java設置全局熱鍵

    第三方包jintellitype實現(xiàn)Java設置全局熱鍵

    本文主要介紹了,在java中使用第三方插件包jintellitype來實現(xiàn)全局熱鍵,非常的簡單,但是很實用,有需要的朋友可以參考下,歡迎一起來參與改進此項目
    2014-09-09
  • SWT(JFace)小制作 BugTracker

    SWT(JFace)小制作 BugTracker

    SWT(JFace)小制作 BugTracker
    2009-06-06
  • Java?中向?Arraylist?添加對象的示例代碼

    Java?中向?Arraylist?添加對象的示例代碼

    本文介紹了如何在 Java 中向 ArrayList 添加對象,并提供了示例和注意事項,通過掌握這些知識,讀者可以在自己的 Java 項目中有效地使用 ArrayList 來存儲和操作對象,需要的朋友可以參考下
    2023-11-11

最新評論