package com.zcw.demospringsecurity.demo12;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.session.security.SpringSessionBackedSessionRegistry;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays;
/**
* @ClassName : WebSecurityConfig
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-04-12 17:18
*/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.antMatchers("/admin/**")
.hasRole("ADMIN")
.antMatchers("/user/**")
.hasRole("USER")
.antMatchers("/app/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
//启用CORS支持
.cors()
.and()
.formLogin();
}
@Bean
CorsConfigurationSource corsConfigurationSource(){
CorsConfiguration configuration = new CorsConfiguration();
//允许从百度站点跨域
configuration.setAllowedOrigins(Arrays.asList("https://www.baidu.com"));
//允许使用GET方法和POST方法
configuration.setAllowedMethods(Arrays.asList("GET","POST"));
//允许带凭证
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//对所有URL生效
source.registerCorsConfiguration("/**",configuration);
return source;
}
}
SpringSecurity解决跨域问题---CORS支持
猜你喜欢
转载自blog.csdn.net/qq_32370913/article/details/105552510
今日推荐
周排行