Spring Security не может получить доступ к внешнему JavaScript - PullRequest
2 голосов
/ 18 января 2020

Я работаю с Spring Security, и когда я пытаюсь использовать внешние javascript или даже веб-файлы, блокирует доступ к безопасности.

Я пытался добавить javascript к моей веб-странице, например так: header. html

<script src="https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.4.0/sockjs.min.js"></script>
<script src="/webjars/stomp-websocket/stomp.min.js"></script>

Моя конфигурация безопасности выглядит следующим образом (обратите внимание на "/ webjars / ", "/*.js", "/ . js "). AllowAll ()):

AppConfig. java

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class AppConfig extends WebSecurityConfigurerAdapter {

@Bean
public LogoutSuccessHandler logoutSuccessHandler() {
    return new LogoutController();
}

@Bean
public AuthenticationController authenticationController() throws UnsupportedEncodingException {
    JwkProvider jwkProvider = new JwkProviderBuilder(domain).build();
    return AuthenticationController.newBuilder(domain, clientId, clientSecret)
            .withJwkProvider(jwkProvider)
            .build();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
    http
        .authorizeRequests()
        .antMatchers("/callback", "/login", "/", "/*.png", "/css/**", "/js/**", "/bibliographies", "/bibliographies/*", "/api/**", "/webjars/**", "/*.js", "/**.js").permitAll()
        .anyRequest().authenticated()
        .and()
        .logout().logoutSuccessHandler(logoutSuccessHandler()).permitAll();
}

При загрузке веб-страницы, с которой я хочу получить доступ к javascript Я получаю следующее исключение:

2020-01-18 16: 38: 39.941 DEBUG 7824 --- [nio-3000-exe c -4] osswaExceptionTranslationFilter: доступ запрещен (пользователь анонимный); перенаправление на точку входа аутентификации

org.springframework.security.access.AccessDeniedException: доступ запрещен в org.springframework.security.access.vote.AffirtivationBased.decide (AffirrativeBased. java: 84) ~ [spring -security-core-4.2.12.RELEASE.jar: 4.2.12.RELEASE] at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation (AbstractSecurityInterceptor. java: 233) ~ [spring-security-core- 4.2.12.RELEASE.jar: 4.2.12.RELEASE] at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke (FilterSecurityInterceptor. java: 124) ~ [spring-security-web-4.2.12 .RELEASE.jar: 4.2.12.RELEASE] at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter (FilterSecurityInterceptor. java: 91) ~ [spring-security-web-4.2.12.RELEASE. jar: 4.2.12.RELEASE] at org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy. java: 331) [spring-security-web-4.2.12.RELEASE.jar: 4.2.12. RELE ASE] в org.springframework.security.web.access.ExceptionTranslationFilter.doFilter (ExceptionTranslationFilter. java: 114) ~ [spring-security-web-4.2.12.RELEASE.jar: 4.2.12.RELEASE] в org. springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy. java: 331) [spring-security-web-4.2.12.RELEASE.jar: 4.2.12.RELEASE] в org.springframework.security.web. сеанс FilterChainProxy. java: 331) [spring-security-web-4.2.12.RELEASE.jar: 4.2.12.RELEASE]

Другие объявленные мной соответствия, такие как "/ api / ** "доступны без входа в систему, однако моя собственная безопасность блокирует мне доступ к . js файлам, даже если мои сопоставления кажутся нормальными.

Как я могу исправить эту проблему?

1 Ответ

1 голос
/ 21 января 2020

Добавьте:

<security:intercept-url pattern="/resources/**" access="permitAll" />

в ваш контекстный файл. Здесь resources - это каталог, в котором находятся все JS файлы.

Вы можете обратиться по этой ссылке: Spring security не позволяет загружать CSS или JS ресурсы

...