spring security
spring security是能夠為j2ee項目提供綜合性的安全訪問控制解決方案的安全框架。它依賴于servlet過濾器。這些過濾器攔截進入請求,并且在應用程序處理該請求之前進行某些安全處理。
spring security對用戶請求的攔截過程如下:
背景
在一個前后端分離開發的項目中,使用springsecurity做安全框架,用jwt來實現權限管理提升restful api的安全性。首先遇到的就是跨域問題,但是在攜帶jwt請求過程中出現了服務端獲取不到jwt情況。
跨域問題
在開發過程中遇到cors (跨域資源共享) 的問題,簡單的在服務器端設置了允許跨域訪問,但是在攜帶jwt請求過程中出現
因為jwt是放在request header中,忽略了在跨域處理是加上允許自己定于的header字段
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
|
@component public class corsfilter implements filter { logger logger= loggerfactory.getlogger(corsfilter. class ); @override public void init(filterconfig filterconfig) throws servletexception { } @override public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception { httpservletrequest request= (httpservletrequest) servletrequest; httpservletresponse response= (httpservletresponse) servletresponse; response.setheader( "access-control-allow-origin" ,request.getheader( "origin" )); response.setheader( "access-control-allow-origin" , "*" ); //允許跨域訪問的域 response.setheader( "access-control-allow-methods" , "post,get,options,delete,put" ); //允許使用的請求方法 response.setheader( "access-control-expose-headers" , "*" ); response.setheader( "access-control-allow-headers" , "x-requested-with,cache-control,pragma,content-type,authorization" ); //允許使用的請求方法 response.setheader( "access-control-allow-credentials" , "true" ); //是否允許請求帶有驗證信息 filterchain.dofilter(servletrequest, servletresponse); } @override public void destroy() { } } |
在網上搜索提到要對options請求進行處理返回200,但是測試并沒有起到效果
這里的options請求實際上就是preflight請求
preflight請求
但是問題依然沒有解決,出現如下
google了之后才知道preflight 請求的相關信息
在我們調用后臺接口的時候,經常會發現請求了兩次,其實第一次發送的就是preflight request(預檢請求)。
為什么需要preflight request
我們都知道瀏覽器的同源策略,就是出于安全考慮,瀏覽器會限制從腳本發起的跨域http請求,像xmlhttprequest和fetch都遵循同源策略。
瀏覽器限制跨域請求一般有兩種方式:
瀏覽器限制發起跨域請求 跨域請求可以正常發起,但是返回的結果被瀏覽器攔截了
一般瀏覽器都是第二種方式限制跨域請求,那就是說請求已到達服務器,并有可能對數據庫里的數據進行了操作,但是返回的結果被瀏覽器攔截了,那么我們就獲取不到返回結果,這是一次失敗的請求,但是可能對數據庫里的數據產生了影響。
為了防止這種情況的發生,規范要求,對這種可能對服務器數據產生副作用的http請求方法,瀏覽器必須先使用options方法發起一個預檢請求,從而獲知服務器是否允許該跨域請求:如果允許,就發送帶數據的真實請求;如果不允許,則阻止發送帶數據的真實請求。
瀏覽器將cors請求分成兩類:簡單請求和非簡單請求。
簡單請求
1.請求方法是以下三種方法之一
- head
- get
- post
2.http的頭信息不超出以下幾種字段
- accept
- accept-language
- content-language
- last-event-id
- content-type:只限于三個值application/x-www-form-urlencoded、multipart/form-data、text/plain
凡是不同時滿足上面兩個條件,就屬于非簡單請求。
而瀏覽器對這兩種請求的處理是不一樣的。
非簡單請求
非簡單請求是那種對服務器有特殊要求的請求,比如請求方法是put或delete,或者content-type字段的類型是application/json。
非簡單請求的cors請求,會在正式通信之前,增加一次http查詢請求,稱為"預檢"請求(preflight)
與cors相關更詳細的看參考底部鏈接
解決方法
在我們后臺用了spring security作為安全框架,并且沒有對preflight這個請求做出相應的處理,那么這個請求會導致權限管控失敗。
處理起來也很簡單,只需要在spring security配置類configure方法中增加放行preflight請求
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
|
@override protected void configure(httpsecurity http) throws exception { http // 由于使用的是jwt,我們這里不需要csrf .csrf().disable() // 基于token,所以不需要session .sessionmanagement().sessioncreationpolicy(sessioncreationpolicy.stateless).and() .authorizerequests() // 所有 / 的所有請求 都放行 .requestmatchers(corsutils::ispreflightrequest).permitall() //對preflight放行 .antmatchers( "/*" ).permitall() .antmatchers( "/u" ).denyall() .antmatchers( "/article/**" ).permitall() .antmatchers( "/video/**" ).permitall() .antmatchers( "/api/**" ).permitall() .antmatchers( "/v2/api-docs" , "/configuration/ui" , "/swagger-resources/**" , "/configuration/**" , "/swagger-ui.html" , "/webjars/**" ) .permitall() .antmatchers( "/manage/**" ).hasrole( "admin" ) // 需要相應的角色才能訪問 // 除上面外的所有請求全部需要鑒權認證 .anyrequest().authenticated(); // 禁用緩存 http.headers().cachecontrol(); // 添加jwt filter http.addfilterbefore(authenticationtokenfilterbean(), usernamepasswordauthenticationfilter. class ); //添加未授權處理 http.exceptionhandling().authenticationentrypoint(getauthenticationentrypoint()); //權限不足處理 http.exceptionhandling().accessdeniedhandler(getaccessdeniedhandler()); } |
最終問題得到解決!
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
參考:
前端 | 淺談preflight request
跨域資源共享 CORS 詳解
原文鏈接:https://segmentfault.com/a/1190000012117774