Java spring security不加载资源(图像、css)
我是spring security的新手,我尝试在我的项目中实现athorisation。一切正常,但图像和css文件未加载。我正在使用注释的方式来配置它 以下是我的项目结构: dispatcher spring web servlet中资源映射的配置:Java spring security不加载资源(图像、css),java,spring-mvc,spring-security,configuration,Java,Spring Mvc,Spring Security,Configuration,我是spring security的新手,我尝试在我的项目中实现athorisation。一切正常,但图像和css文件未加载。我正在使用注释的方式来配置它 以下是我的项目结构: dispatcher spring web servlet中资源映射的配置: <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
我试图像这样加载css文件:
<link rel="stylesheet" type="text/css"
href="<c:url value="${pageContext.request.contextPath}/resources/static/css/test_style.css"/>"
<img src="<c:url value='/resources/static/img/head.jpg' />" type="image/jpg" alt="BigLogo" height="650" width="1000"/>
这些图像是这样的:
<link rel="stylesheet" type="text/css"
href="<c:url value="${pageContext.request.contextPath}/resources/static/css/test_style.css"/>"
<img src="<c:url value='/resources/static/img/head.jpg' />" type="image/jpg" alt="BigLogo" height="650" width="1000"/>
因此,图像不会显示,css文件出现错误:
Refused to apply style from '<URL>' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
我做错了什么?
是否存在一些配置问题?
我搜索了很多,但没有找到一些可以帮助我的解决方案。
我将非常感谢您的帮助。我建议您添加以下内容:
<mvc:resources mapping="/css/**" location="/resources/static/css/" />
<mvc:resources mapping="/img/**" location="/resources/static/img/" />
在调度程序spring-web-servlet.xml中,然后只需调用
<link rel="stylesheet" type="text/css" href="./css/test_style.css"/>"
<img src="./img/head.jpg" type="image/jpg" alt="BigLogo" height="650" width="1000"/>
在Spring引导中,在/src/main/webapp/WEB-INF/images中添加映像文件夹 在配置文件中
@Configuration
public class AppConfig implements WebMvcConfigurer {
......
......
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/*").addResourceLocations("/WEB-INF/images/");
}
}
安全地
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/images/*").permitAll()
//To display the image on first landing page to all users
}
如果有帮助,请检查这个。谢谢,我是施里坎特·哈维尔。我刚刚缩小了css文件,但是我仍然得到这个错误。非常感谢你的回答。我试过了,但似乎不起作用。图像仍然没有显示,css也没有加载。