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

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

Tomcat 8 和 Spring Security Cors

Tomcat 8 和 Spring Security Cors

Cats萌萌 2024-01-17 16:32:27
我正在嘗試配置 Spring Security 以使其支持 CORS。我已經使用 Spring Boot 使用以下配置代碼使其在我的本地主機上工作:@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {? ? @Override? ? protected void configure(HttpSecurity http) throws Exception {? ? ? ? http.cors()? ? ? ? .and()? ? ? ? .antMatcher("/api/**")? ? ? ? .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)? ? ? ? .and()? ? ? ? .authorizeRequests()? ? ? ? .antMatchers(HttpMethod.POST, "/api/login").permitAll()? ? ? ? .antMatchers(HttpMethod.GET, "/api/websocket/**").permitAll()? ? ? ? .antMatchers("/api/**").authenticated()? ? ? ? .and()? ? ? ? .addFilterBefore(new JWTLoginFilter("/api/login", HttpMethod.POST, authenticationManager(), tokenAuthenticationService, myUserService), UsernamePasswordAuthenticationFilter.class)? ? ? ? .addFilterBefore(new JWTAuthenticationFilter(tokenAuthenticationService), UsernamePasswordAuthenticationFilter.class)? ? ? ? .csrf().disable();? ? }@Beanpublic CorsConfigurationSource corsConfigurationSource() {? ? final CorsConfiguration configuration = new CorsConfiguration();? ? configuration.setAllowedOrigins(ImmutableList.of("*"));? ? configuration.setAllowedMethods(ImmutableList.of("HEAD",? ? ? ? ? ? "GET", "POST", "PUT", "DELETE", "PATCH"));? ? // setAllowCredentials(true) is important, otherwise:? ? // The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.? ? configuration.setAllowCredentials(true);? ? // setAllowedHeaders is important! Without it, OPTIONS preflight request? ? // will fail with 403 Invalid CORS request? ? configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));? ? final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();? ? source.registerCorsConfiguration("/**", configuration);? ? return source;}}這是失敗的 OPTIONS 請求的屏幕截圖:以及我的本地主機上的工作請求:
查看完整描述

5 回答

?
UYOU

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

Spring security 提供了一種在 http 配置器中配置 CORS 的方法,有一種更簡潔的方法可以將 CORS 過濾器添加到應用程序中 -


@Component 

@Order(Ordered.HIGHEST_PRECEDENCE)

public class MyCORSFilterClass implements Filter {

@Override

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 

throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;

HttpServletResponse response = (HttpServletResponse) res;


response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

response.setHeader("Access-Control-Allow-Credentials", "true");

response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");

response.setHeader("Access-Control-Max-Age", "3600");

response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

chain.doFilter(req, res);

}

@Override

public void init(FilterConfig filterConfig) {

}


@Override

public void destroy() {

}

}

以最高優先級對過濾器進行排序可確保 javax.servlet.Filter 的 MyCORSFilterClassimplementation 是鏈中的第一個。


查看完整回答
反對 回復 2024-01-17
?
江戶川亂折騰

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

  1. 嘗試在本地方法中放置一個調試點corsConfigurationSource()并檢查它是否正在執行。如果它沒有被執行,請調查原因 - 可能通過啟用 Spring 的調試日志和/或重新檢查 Spring 配置。

  2. 另外,嘗試添加選項setAllowedMethods

    configuration.setAllowedMethods(ImmutableList.of("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));



查看完整回答
反對 回復 2024-01-17
?
慕無忌1623718

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

響應403反映授權失敗。您的服務器可能已設置為要求對選項請求進行授權。您必須確保選項配置為發送成功響應(2xx 狀態代碼),以允許瀏覽器發送實際請求。2xx 響應通知瀏覽器服務器處理 CORS 請求。

您的請求在本地有效的原因可能是您在提出請求時已獲得授權。因此,請檢查您的身份驗證以確保其正確。因此,飛行前請求不會發送任何授權標頭,因此您不應該在服務器端進行期望。


查看完整回答
反對 回復 2024-01-17
?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

檢查 tomcat 服務器是否在 $CATALINA_BASE/conf/web.xml 中配置了沖突的 CORS 過濾器



查看完整回答
反對 回復 2024-01-17
?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

Tomcat 也有自己的 cors 過濾器,如果您在 tomcat 之前使用其他服務器(例如 nodejs、apache 服務器 vs ),也請檢查其 cors 過濾器。



查看完整回答
反對 回復 2024-01-17
  • 5 回答
  • 0 關注
  • 242 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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