с использованием Spring Boot и Spring Security. Мне было поручено реализовать механизм, который будет вызывать Identity Manager каждый раз, когда вызывается метод внешнего интерфейса. Как бы то ни было, возникла проблема: мы боимся перегружать IDM слишком большим количеством запросов. Так что я подумал, есть ли способ проверить токен не для каждого вызова, а вместо этого для каждого временного интервала.
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Value("${antPatterns}")
private String antPatterns;
@Value("${statistiche.security.enabled:true}")
private boolean securityEnabled;
@Bean
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
@Bean
public AuthoritiesExtractor authoritiesExtractor() {
return new AuthoritiesSSOExtractor();
}
@Bean
public PrincipalSSOExtractor principalExtractor() {
return new PrincipalSSOExtractor();
}
@Override
public void configure(HttpSecurity http) throws Exception {
//PRODUZIONE
if(securityEnabled) {
http.anonymous().disable()
.requestMatchers().antMatchers(antPatterns)
.and().authorizeRequests()
.antMatchers(antPatterns).authenticated()
.and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
}
else
{
http.anonymous().disable().antMatcher(antPatterns).authorizeRequests().anyRequest().permitAll();
}
}
}
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ConditionalOnProperty(name = "statistiche.security.enabled", havingValue = "true", matchIfMissing = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${antPatterns}")
private String antPatterns;
@Value("${statistiche.security.enabled:true}")
private boolean securityEnabled;
@Override
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/actuator/**").and().ignoring().mvcMatchers(HttpMethod.OPTIONS, "/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//http.antMatcher(antPatterns).authorizeRequests().anyRequest().permitAll();
if(securityEnabled)
{
http
.anonymous().disable()
.csrf().disable()
.authorizeRequests()
.antMatchers(antPatterns).authenticated()
.and()//.addFilterBefore(new JwtAuthenticationFilter(), BasicAuthenticationFilter.class)
.exceptionHandling().authenticationEntryPoint(getRestAuthenticationEntryPoint());
}else{
http.antMatcher(antPatterns).authorizeRequests().anyRequest().permitAll();
//http.antMatcher(antPatterns).authorizeRequests().anyRequest().permitAll();
}
}
@Bean
AuthenticationEntryPoint getRestAuthenticationEntryPoint() {
return new RestAuthenticationEntryPoint();
}
@Bean
AuthenticationFailureHandler authenticationFailureHandler() {
return new SimpleUrlAuthenticationFailureHandler();
}
}