亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

添加“授權”標頭會導致Spring Security保護允許的端點

添加“授權”標頭會導致Spring Security保護允許的端點

波斯汪 2022-09-07 17:45:31
所以,我有這個在我的WebSecurityConfigurerAdapterpublic class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {        http                // Use this configuration for endpoints starting with 'api'                .antMatcher("/api/**")                // Do not secure endpoints receiving callbacks                .authorizeRequests().antMatchers(""/api/v1/notification").permitAll()                // Allow only users with role "ROLE_API"                .anyRequest().hasRole(Users.UserRoles.ROLE_API.role.replace("ROLE_", ""))                .and()                .httpBasic()                .and()                // Do not create any sessions                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)                .and()                // Disable csrf                .csrf().disable();                }}這不應該安全。如果我在標頭中沒有調用該終結點,則允許請求,但是如果我添加標頭,則會收到 http 響應代碼。/api/v1/notificationAuthorization: Basic abcdHTTPAuthorization: Basic abcd401注意:只是隨機的,所以沒有這樣的用戶在我的數據庫中Basic abcd問題是,為什么添加 http 標頭會使端點再次受到保護?Authorization...
查看完整描述

1 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

好問題,這可能有點令人困惑,因為這意味著合法客戶端,只是密碼錯誤,就可以被拒絕一個沒有憑據的世界其他地方可以看到的頁面。


實際上,這是設計使然。一般來說,授權系統需要先知道用戶是誰,然后才能知道用戶是否可以執行 X、Y 或 Z 操作。即使使用公共終結點,當用戶處于上下文中時,終結點的行為也可能不同。因此,實際上,它們是單獨的系統,首先要進行身份驗證:如果請求提供憑據,則框架將嘗試對用戶進行身份驗證,并相應地接受或拒絕請求。


一個選項

我意識到你沒有問如何解決它(你可能對行為完全滿意,只是好奇),但你可以做的一件事是將其配置為忽略故障,只是為了那個端點:BasicAuthenticationFilter


static class IgnoreFailuresBasicAuthenticationFilter extends BasicAuthenticationFilter {

    private final BasicAuthenticationFilter everythingElse;


    public IgnoreFailuresBasicAuthenticationFilter(BasicAuthenticationFilter everythingElse) {

        super(everythingElse.getAuthenticationManager());

        this.everythingElse = everythingElse;

    }


    protected void doFilterInternal(request, response, chain) {

        if ("/api/v1/notification".equals(request.getPathInfo())) {

            super.doFilterInternal(request, response, chain);

        } else {

            this.everythingElse.doFilterInternal(request, response, chain);

        }

    }

}

然后替換 DSL 中的過濾器:


http

    .httpBasic()

        .withObjectPostProcessor(

            new ObjectPostProcessor<BasicAuthenticationFilter>() {

                public BasicAuthenticationFilter postProcess(BasicAuthenticationFilter filter) {

                    return new IgnoreFailuresBasicAuthenticationFilter(filter);

                }

            });

這將允許篩選器鏈繼續,即使基本身份驗證失敗也是如此。結果是,在身份驗證失敗的情況下,您將獲得403而不是401。


查看完整回答
反對 回復 2022-09-07
  • 1 回答
  • 0 關注
  • 89 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號