无法在Spring MVC Java配置中加载CSS和JS文件

时间:2019-04-16 10:21:23

标签: html css spring-mvc

我使用spring mvc开发了一个示例项目,其中包括spring security

当我添加css之类的引导文件时,在控制台中出现如下所示的错误

Refused to apply style from 'http://localhost:8098/security-jdbc-plaintext/resources/bootstrap.min.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

请看一下我的配置类

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.security")
@PropertySource("classpath:persistence-mysql.properties")
public class DemoAppConfig {

    //set up variable to hold the properties
    @Autowired
    private Environment env;

    //set up a logger for diagnostics
    private Logger logger =Logger.getLogger(getClass().getName());

    // define bean for view resolver 
    @Bean
    public ViewResolver viewResolver()
    {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;

    }
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/resources/**")
          .addResourceLocations("/resources/"); 
    }     
    //define a bean for security datasource
    @Bean
    public DataSource securityDataSource()
    { 
        // create connection pool
        ComboPooledDataSource securityDataSource
            =new ComboPooledDataSource();


        //set the jdbc driver class
        try {
            securityDataSource.setDriverClass(env.getProperty("jdbc.driver"));
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }

        // log the connection props
        logger.info(">>> jdbc.url=" + env.getProperty("jdbc.url"));
        logger.info(">>> jdbc.user=" + env.getProperty("jdbc.user"));

        //set database connection props
        securityDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
        securityDataSource.setUser(env.getProperty("jdbc.user"));
        securityDataSource.setPassword(env.getProperty("jdbc.password"));

        //set connection pool props
        securityDataSource.setInitialPoolSize(getIntProperty("connection.pool.initialPoolSize"));
        securityDataSource.setMinPoolSize(getIntProperty("connection.pool.minPoolSize"));
        securityDataSource.setMaxPoolSize(getIntProperty("connection.pool.maxPoolSize"));
        securityDataSource.setMaxIdleTime(getIntProperty("connection.pool.maxIdleTime"));

        return securityDataSource;
    }

    //need  helper method
    //read env property and convert to int

    private int getIntProperty(String propName)
    {

        String propVal = env.getProperty(propName);

        //now convert to int
        int intPropVal = Integer.parseInt(propVal);
                return  intPropVal;
    }

}

安全配置

@Configuration
@EnableWebSecurity
public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {

    //add a reference  for our security data source
    @Autowired
    private DataSource securityDataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //use jdbc authentication

        auth.jdbcAuthentication().dataSource(securityDataSource);
    } 
    //source override and implement
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
             .antMatchers("/resources/**").permitAll()   
                    .antMatchers("/").hasRole("EMPLOYEE")
                    .antMatchers("/leaders/**").hasRole("MANAGER")
                    .antMatchers("/systems/**").hasRole("ADMIN")
                .and() 
                .formLogin()  
                    .loginPage("/showMyLoginPage")
                    .loginProcessingUrl("/authenticateTheUser") 
                    .permitAll()
                .and().logout().permitAll()
                .and()
                .exceptionHandling().accessDeniedPage("/access-denied");
        }

}

请看看我的jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>    
<html>
    <head>
        <title>Custom login page</title>
        <style>
        .failed{
        color:red;
        }
        </style>
            <link href="<c:url value="/resources/bootstrap.min.css" />" rel="stylesheet">
    </head>     
    <body>
        <h3 class="danger"> My Custom login Page</h3>
            <form action="${pageContext.request.contextPath}/authenticateTheUser "
            method="POST">  

            <c:if test="${param.error != null}">

                <i class='failed'>Sorry! You entered invalid user name or password.</i>
            </c:if>   

            <c:if test="${param.logout != null}">

                <i >You have been logged out.</i>
            </c:if> 
            <p>
                User name: <input type="text" name="username" />
            </p>
            <p>
                Password: <input type="password" name="password" />
            </p>
            <input type='submit' value="Login">
            <!-- manually adding tokens -->
            <input type='hidden' name="${_csrf.parameterName}"
                                value='${_csrf.token}'/>
            </form>   
    </body>
</html>  

在tomcat服务器上我很喜欢

WARNING: No mapping for GET /security-jdbc-plaintext/resources/bootstrap.min.css

这有什么问题?

还有其他解决方案吗?

0 个答案:

没有答案