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

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

SpringBoot 安全配置未按預期工作

SpringBoot 安全配置未按預期工作

明月笑刀無情 2022-12-15 15:58:17
我一直在嘗試配置 Spring Boot 安全性,以便在不需要身份驗證的情況下允許某些 url,并且在沒有身份驗證的情況下不允許任何其他請求。我很難做到這一點。根據我的理解,anyRequest().authenticated() 要求之前聲明的 antMatchers 需要身份驗證。怎么可能達到我的要求。我的 Http 安全配置@Override    protected void configure(HttpSecurity http) throws Exception {        http.cors().and().csrf().disable().authorizeRequests()                .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()                .antMatchers(HttpMethod.POST,SIGN_UP_URL).permitAll()                .antMatchers(HttpMethod.GET,banner_top_url).permitAll()                .antMatchers(HttpMethod.GET,banner_bottom_url).permitAll()                .antMatchers(HttpMethod.GET,javascript_url).permitAll()                .antMatchers(HttpMethod.GET,stylesheet_url).permitAll()                .antMatchers(HttpMethod.GET,photos_url).permitAll()                .antMatchers(HttpMethod.GET,transformed_photos_url).permitAll()                .antMatchers(HttpMethod.GET,preview_url).permitAll()                .antMatchers(HttpMethod.GET, "/", "/**/*.html", "/static/favicon.ico", "/**/*.js", "/**/*.js.map", "/**/*.css", "/**/*.png", "/**/*.jpg", "/**/*.jpeg", "/**/*.gif", "/**/*.ttf", "/**/*.json", "/**/*.woff", "/**/*.woff2", "/**/*.eot", "/**/*.svg").permitAll()// allows static content from resource folder                .antMatchers("/error").permitAll() // By default Security framework disables error pages (Unauthrorized)                .anyRequest().authenticated()                .and()                   }我假設必須在沒有身份驗證的情況下授予以下網址訪問權限。 SIGN_UP_URL banner_top_url banner_bottom_url javascript_url stylesheet_url photos_url transformed_photos_url preview_url問題是這一行:.anyRequest().authenticated() 如果我刪除它,那么 REST 接口中的所有端點都可以在沒有我不想要的身份驗證的情況下使用。
查看完整描述

3 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

我發現 Spring-Working 符合預期。也就是說,任何 antMAtchers 都將匹配 requestPath 而不是 resourcePath。下面提供了一個例子。

*localhost:8080/image.jpg*

指向應用程序的根目錄,即src/main/resources/static/image.jpg

現在為什么將 static 用作資源處理程序,那是因為在staticResourceConfiguration.java類中我有以下行 registry .addResourceHandler("/resources/**") .addResourceLocations("/resources/");

    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");



查看完整回答
反對 回復 2022-12-15
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

默認情況下,Spring-security 允許傳遞所有內容。你必須告訴 Spring 什么可以通過,什么不能通過。通過刪除 anyRequest().authenticated 你告訴 spring 與你提到的模式匹配的所有東西都被允許去,其余的做你默認做的事情,這意味著,繼續。這是 Spring Security 文檔:https ://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#jc-httpsecurity


查看完整回答
反對 回復 2022-12-15
?
呼如林

TA貢獻1798條經驗 獲得超3個贊

你為什么不通過 web.ignoring 全局排除靜態資源文件?


@Override

public void configure(WebSecurity web) throws Exception {

    web

      .ignoring()

         .antMatchers("/resources/**"); 

  }


查看完整回答
反對 回復 2022-12-15
  • 3 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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