我是 Spring Boot MVC 應用程式開發的新手。我有一個新應用程式,我通過 Maven 設定,我試圖在其中匯入 jquery 和引導程式。我通過 Maven 將兩者都匯入為 Jar 檔案。它們位于 Java Resources->Libraries->Maven Dependencies 中。
我正在使用彈簧框架 5.3.22 和彈簧安全 5.7.3
當我運行我的站點時,我可以看到在網路選項卡中添加了 jquery 和引導檔案,但是我收到以下錯誤:
“拒絕從 [website url] 執行腳本,因為它的 MIME 型別 ('text/html') 不可執行,并且啟用了嚴格的 MIME 型別檢查。”
和
“拒絕從 [website url] 應用樣式,因為它的 MIME 型別 ('text/html') 不是受支持的樣式表 MIME 型別,并且啟用了嚴格的 MIME 檢查。”
在我的 JSP 頁面上,我包含如下檔案:
<link href="webjars/bootstrap/5.2.0/css/bootstrap.min.css" rel="stylesheet" >
<script src="webjars/jquery/3.6.1/jquery.min.js" type="text/javascript"></script>
<script src="webjars/bootstrap/5.2.0/js/bootstrap.min.js" type="text/javascript"></script>
我的 Servlet 配置如下所示:
public class MySpringMvcDispatcherServeletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
//return DemoAppConfig.java class
return new Class[] {DemoAppConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
@Configuration
@EnableWebSecurity
public class DemoSecurityConfig {
@Bean
public InMemoryUserDetailsManager userDetailsManager() {
System.out.println("======>> Add Details manager");
//add in memory users/roles
}
@Bean
public CorsConfigurationSource corsConfigurationSource(){
System.out.println("======>> Add CORS");
CorsConfiguration corsConfiguration = new CorsConfiguration();
// Below config will allow only following origines from web browser
corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:8080/"));
// Whether user credentials are supported. By default, do not support
// If you want to allow credentials then set it true
corsConfiguration.setAllowCredentials(false);
// below will not allow DELETE methods, if you want then add DELETE also
corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "OPTION"));
// Below will set allowed headers list, Other headers will not be supported
corsConfiguration.setAllowedHeaders(Arrays.asList("accept", "authorization", "apikey", "tenantId"));
UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
// This will register above configurations on all resources from the root
// If you want different rules for different resources then create separate configuration
// and register on separate resource path uri
corsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return corsConfigurationSource;
}
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
// Ignore resources for any check
System.out.println("======>> Add web Customizer");
return (web) -> web.ignoring().antMatchers("/webjars/**","/resources/**", "/static/**", "/css/**", "/js/**", "/img/**", "/icon/**");
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
System.out.println("======>> Add Security Chain");
return http
.authorizeRequests(configurer ->
configurer
.antMatchers("/webjars/**","/resources/**", "/static/**", "/css/**", "/js/**", "/img/**", "/icon/**").permitAll()
.anyRequest().authenticated())
.formLogin(configurer ->
configurer
.loginPage("/showMyLoginPage")
.loginProcessingUrl("/authenticateTheUser")
.permitAll())
.build();
}
}
uj5u.com熱心網友回復:
我能夠通過覆寫 addResrouceHandlers 來做到這一點。在實作 WebMvcConfigurer 的 DemoAppConfig 檔案中,我添加了以下代碼:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.jason.springsecurity.demo")
public class DemoAppConfig implements WebMvcConfigurer{
//define bean for ViewResolver
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("================>Calling addResourceHandlers");
//my libraries
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
//webjars
if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
}
}
然后我可以像這樣將檔案添加到我的視圖中:
<link href="webjars/bootstrap/5.2.0/css/bootstrap.min.css" rel="stylesheet" >
<script src="webjars/jquery/3.6.1/jquery.min.js"></script>
<script src="webjars/bootstrap/5.2.0/js/bootstrap.min.js"></script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/513221.html
標籤:爪哇弹簧MVC弹簧安全
下一篇:僅在執行批處理時如何執行建構式?
