cors跨域后端服务器配置

master
chendt 4 years ago
parent 58ed47edc3
commit 4dfca47660

@ -17,6 +17,7 @@ import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsUtils;
@Configuration @Configuration
@EnableResourceServer @EnableResourceServer
@ -29,15 +30,12 @@ public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter
public void configure(HttpSecurity http) throws Exception { public void configure(HttpSecurity http) throws Exception {
http http
.addFilterBefore(loginAuthenticationFilter, UsernamePasswordAuthenticationFilter.class) .addFilterBefore(loginAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
// Since we want the protected resources to be accessible in the UI as well we need .csrf().disable().cors()
// session creation to be allowed (it's disabled by default in 2.0.6) .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) .and().authorizeRequests().requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
.and() .and().requestMatchers().anyRequest()
.requestMatchers().anyRequest() .and().anonymous()
.and() .and().authorizeRequests()
.anonymous()
.and()
.authorizeRequests()
//配置/p访问控制必须认证过后才可以访问 //配置/p访问控制必须认证过后才可以访问
.antMatchers("/p/**").authenticated(); .antMatchers("/p/**").authenticated();
} }

Loading…
Cancel
Save