SpringSecurity-2.7中跨域問題解析
SpringSecurity-2.7中跨域問題
訪問測試
起因
寫這篇的起因是會(huì)了解到
SSM(@CrosOrigin)解決跨域,但是會(huì)在加入SpringSecurity配置后,這個(gè)跨域解決方案就失效了,而/login這個(gè)請求上是無法添加這個(gè)注解或者通過配置(WebMvcConfig)去解決跨域,所以只能使用SpringSecurity提供的.cros()去解決跨域,但是在學(xué)習(xí)過程中,如果稍微粗心,可能會(huì)出現(xiàn)跨域不通的問題,而以下將會(huì)說明SpringSecurity是如何配置跨域的
Postman發(fā)起的請求不屬于異步請求(區(qū)分前后端分離的JSON)

使用axios發(fā)起異步請求,
<!--
前端: 使用 Live Server 啟動(dòng)訪問 http://127.0.0.1:5500/index.html
后端: localhost:8080/login
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id="btn">發(fā)起異步請求</button>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
let btnEl = document.querySelector('#btn');
btnEl.onclick = function () {
console.log('click......................');
axios({
url: 'http://localhost:8080/login',
method: 'post',
data: {
username: 'zhangsan',
password: '123456',
},
}).then((res) => {
console.log(res);
});
};
</script>
</body>
</html>請求測試

SpringSecurity-配置
config
// 【/login】需要顯示的聲明出來,在前后端分離中,本文沒有采用的是 ajax 向后端發(fā)送異步請求
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
/**
* 請求配置
* authorizeHttpRequests: 開啟權(quán)限請求管理,針對 http 請求進(jìn)行授權(quán)配置
* mvcMatchers: 匹配請求
* - permitAll: 代表放行該資源,該資源位公共資源,無需認(rèn)證和授權(quán)可以直接訪問
* - anyRequest().authenticated(): 代表所有請求,必須認(rèn)證之后才能訪問
* - formLogin: 代表開啟表單認(rèn)證
* <strong>放行資源必須放在認(rèn)證資源之前</strong>
*/
http.authorizeHttpRequests((authorizeHttpRequests) ->
authorizeHttpRequests
// 預(yù)檢請求是怎么知道的:官網(wǎng)中有這樣一段描述,如圖
.antMatchers(HttpMethod.OPTIONS, "/login").permitAll()
.anyRequest().authenticated()
);
/**
* 跨域配置
*/
http.cors().configurationSource(corsConfigurationSource());
// WHITELIST 自定義的放行資源數(shù)組, /login不能出現(xiàn)在里面
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().antMatchers(WHITELIST);
}
}
// 此處關(guān)于下方的描述可以不更改,依然使用此配置
@Bean
CorsConfigurationSource corsConfigurationSource() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); // 新建一個(gè)跨域配置源
// 你只要點(diǎn)擊 .cors().configurationSource( <- 點(diǎn)擊進(jìn)入這個(gè)方法,查看出這個(gè)參數(shù),就明白為什么給了 CorsConfiguration,)
CorsConfiguration configuration = new CorsConfiguration(); // 新建一個(gè)跨域配置
configuration.setAllowCredentials(true); // 【這個(gè)憑證問題,后續(xù)會(huì)給出詳細(xì)解釋,在 axios 的配置中默認(rèn)是false,【axios 中 `withCredentials` 表示跨域請求時(shí)是否需要使用憑證】瀏覽器是否應(yīng)當(dāng)發(fā)送憑證信息,如cookie。
configuration.setAllowedMethods(Arrays.asList("*")); // 允許的請求方法,*表示允許所有方法
configuration.setAllowedHeaders(Arrays.asList("*")); // 允許的請求頭,*表示允許所有頭
configuration.setMaxAge(Duration.ofHours(1)); // 預(yù)檢請求的有效期,有效期內(nèi)不必再次發(fā)送,默認(rèn)是1800秒。
configuration.setAllowedOriginPatterns(Arrays.asList("*"));// 允許的請求源
source.registerCorsConfiguration("/**", configuration); // 注冊跨域配置
return source;
}
以上配置其實(shí)可以算正確,但是有合規(guī)
在調(diào)試后發(fā)現(xiàn),只需要將
/login加入(.antMatchers("/login").permitAll()),在所看到的視頻中的關(guān)于SpringSecurity的跨域就生效了,也可以不將預(yù)檢配置出來
到此這篇關(guān)于SpringSecurity-2.7中跨域問題的文章就介紹到這了,更多相關(guān)SpringSecurity跨域內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot不掃描@repository的問題及解決
這篇文章主要介紹了springboot不掃描@repository的問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
關(guān)于fastjson的@JSONField注解的一些問題(詳解)
下面小編就為大家?guī)硪黄P(guān)于fastjson的@JSONField注解的一些問題(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
在Java項(xiàng)目中實(shí)現(xiàn)CI/CD持續(xù)集成與持續(xù)部署
這篇文章主要為大家介紹了在Java項(xiàng)目中實(shí)現(xiàn)CI/CD持續(xù)集成與持續(xù)部署詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
JAVA 獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼
這篇文章主要介紹了JAVA 獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10
Java中Long類型傳入前端數(shù)值出錯(cuò)問題
這篇文章主要介紹了Java中Long類型傳入前端數(shù)值出錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04

