【问题标题】:SpringMVC lot loading resource files (JS and CSS)SpringMVC 批量加载资源文件(JS 和 CSS)
【发布时间】:2018-03-28 06:40:59
【问题描述】:

我有以下 springMVC 网络应用程序。这是目录。我在 vendor 文件夹中有所有的 css 和 js:

这是我的安全配置。我尝试添加“/resources/vendor/”“/vendor/”,但似乎没有任何效果:

安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private DataSource dataSource;

    @Value("${spring.queries.users-query}")
    private String usersQuery;

    @Value("${spring.queries.roles-query}")
    private String rolesQuery;

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.
                jdbcAuthentication()
                .usersByUsernameQuery(usersQuery)
                .authoritiesByUsernameQuery(rolesQuery)
                .dataSource(dataSource)
                .passwordEncoder(bCryptPasswordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.
                authorizeRequests()
                .antMatchers("/", "/resources/vendor/**", "/vendor/**", "/css/**").permitAll()
                .antMatchers("/login").permitAll()
                .antMatchers("/registration").permitAll()
                .antMatchers("/admin/**").hasAuthority("ADMIN").anyRequest()
                .authenticated().and().csrf().disable().formLogin()
                .loginPage("/login").failureUrl("/login?error=true")
                .defaultSuccessUrl("/admin/home")
                .usernameParameter("email")
                .passwordParameter("password")
                .and().logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/").and().exceptionHandling()
                .accessDeniedPage("/access-denied");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
                .ignoring()
                .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**", "/vendor/**","/favicon.ico");
    }

}

还有网络配置。资源路径已配置:

WebMvcConfig

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * @return
     */
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder;
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

}

资源未加载。没有 404 错误:

这是相同文件工作的示例:

HTML 标头:

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>TEST</title>

    <!-- Bootstrap core CSS-->
    <link href="vendor/bootstrap/css/bootstrap.min.css" >
    <!-- Custom fonts for this template-->
    <link href="vendor/font-awesome/css/font-awesome.min.css" type="text/css">
    <!-- Custom styles for this template-->
    <link href="css/sb-admin.css" >

</head>

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    你需要把css和js放在\src\main\resources\static里面,所以如果你想使用vendor文件夹那么应该把它移到static文件夹里面。

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 1970-01-01
      • 2012-09-26
      • 2013-10-20
      • 2021-01-06
      • 1970-01-01
      • 2015-01-09
      • 2020-03-15
      • 2019-08-02
      相关资源
      最近更新 更多