SpringBoot2.0 整合Oauth2 (四) 配置文件快速配置url過濾
文章目錄
1、添加url過濾配置
ignore:
urls:
- /oauth/token
- /notify/**
#- /**
clients:
- singleApp
2、添加配置類
/**
* ===================================
* 描 述 : 配置過濾
* 包 名 : top.qinxq.single.common.auth
* 創建人 : qinxq
* ===================================
*/
@Data
@Configuration
@ConditionalOnExpression("!'${ignore}'.isEmpty()")
@ConfigurationProperties(prefix = "ignore")
public class FilterIgnorePropertiesConfig {
private List<String> urls = new ArrayList<>();
private List<String> clients = new ArrayList<>();
}
3、配置資源服務器
/**
* ===================================
* 描 述 : 資源服務器
* 包 名 : top.qinxq.single.common.auth
* 創建人 : qinxq
* ===================================
*/
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Autowired
private FilterIgnorePropertiesConfig filterIgnorePropertiesConfig;
@Autowired
private AuthExceptionHandler authExceptionHandler;
@Override
public void configure(HttpSecurity http) throws Exception {
// //表單登錄 方式
// http.formLogin()
// .loginPage("/authentication/require")
// //登錄需要經過的url請求
// .loginProcessingUrl("/authentication/form");
// http
// .authorizeRequests()
// .antMatchers("/oauth/token").permitAll()
// .anyRequest()
// .authenticated()
// .and()
// //關閉跨站請求防護
// .csrf().disable();
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry =
http.authorizeRequests();
//加載配置文件中ignore過濾列表
filterIgnorePropertiesConfig.getUrls().forEach(url -> registry.antMatchers(url).permitAll());
registry.anyRequest().authenticated()
.and()
.csrf().disable();
http.csrf().disable();//關閉 csrf 允許跨域
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.accessDeniedHandler(authExceptionHandler)
.authenticationEntryPoint(authExceptionHandler);
}
}
相關鏈接
SpringCloud、SpringBoot2.0 整合Oauth2 (一) 基本配置
SpringCloud、SpringBoot2.0 整合Oauth2 (一) 基本配置
SpringCloud、SpringBoot2.0 整合Oauth2 (二) 自定義返回格式及用戶基本信息
SpringCloud、SpringBoot2.0 整合Oauth2 (二) 自定義返回格式及用戶基本信息