java - SpringMVC批量加载资源文件(JS和CSS)

标签 java spring spring-mvc spring-security

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

enter image description here

这是我的安全配置。我尝试添加“/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 错误:

enter image description here

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

enter image description here

HTML header :

<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>

最佳答案

您需要将 css 和 js 放入 \src\main\resources\static 中,因此如果您想使用供应商文件夹,则应将其移动到 static 文件夹中.

关于java - SpringMVC批量加载资源文件(JS和CSS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46775461/

相关文章:

java - 无法使用 .transform 更改 Path2D.Double

java - 如何在 selenium 中自动执行 cmd 提示符和 vb 实用程序文件中的命令?

java - ASM COMPUTE_FRAMES If + 赋值错误

java - 一对多映射不起作用 Spring data JPA

java - Spring Boot viewControllerHandlerMapping配置

ajax - Spring 安全性 + Ajax session 超时问题

Java Swing 布局和菜单

java - 如何更改maven web项目的默认行为?

javascript - JS $http.POST 方法 405 错误

spring - 如何在 Controller 中获取表单值