Java 由于spring安全性,无法访问spring启动项目中的资产
我面临着一个相当有趣的问题。在我的Java 由于spring安全性,无法访问spring启动项目中的资产,java,spring-boot,spring-security,resources,thymeleaf,Java,Spring Boot,Spring Security,Resources,Thymeleaf,我面临着一个相当有趣的问题。在我的控制器类中,我添加了路径@RequestMapping(“/product”)。在类内部,当我在方法上放置路径时,如@GetMapping(“/update/{productId}”),这意味着当我将路径除以/时,我无法访问html页面中的资产文件。但如果我像@GetMapping(“/{productId}”)这样定义路径,它就可以正常工作。使用类似于@GetMapping(“/update/{productId}”)的路径,我的资产URL将变成这样 htt
控制器
类中,我添加了路径@RequestMapping(“/product”)
。在类内部,当我在方法上放置路径时,如@GetMapping(“/update/{productId}”)
,这意味着当我将路径除以/
时,我无法访问html
页面中的资产文件。但如果我像@GetMapping(“/{productId}”)
这样定义路径,它就可以正常工作。使用类似于@GetMapping(“/update/{productId}”)
的路径,我的资产URL将变成这样
http://localhost:8080/product/assets/js/editor/ckeditor/ckeditor.custom.js
您可以在URL中看到自动添加的/product/
。因此,html页面无法找到这些资产,因为没有product
文件夹和get404
使用像@GetMapping(“/{productId}”)
资产url这样的路径就可以了
http://localhost:8080/assets/js/editor/ckeditor/ckeditor.custom.js
我的控制器
类是
@Controller
@RequestMapping("/product")
public class AdminProductController {
@GetMapping("/update/{productId}")
ModelAndView modelAndView = new ModelAndView("admin/add-product");
//my code
return modelAndView;
}
}
在我的WebSecurityConfig
中,我像下面这样配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/", "/admin/login","/customer/login", "/logout","/file/**").permitAll()
.antMatchers("/admin/**","/category/**","/product/*").hasAnyAuthority("ROLE_ADMIN")
.antMatchers("/customer/**").hasAnyAuthority("ROLE_CUSTOMER")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/admin/login")
.loginProcessingUrl("/login")
.loginPage("/customer/login")
.loginProcessingUrl("/login")
.failureUrl("/customer/login?error")
.successHandler(successHandler);
http
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/")
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.permitAll();
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**", "/static/**", "/assets/**", "/css/**", "/fonts/**", "/js/**", "/images/**");
}
我还在WBMVCConfiure
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/static/**").addResourceLocations("classpath:META-INF/resources/static/");
}
下面是我的addproduct.html
P&S工艺-手工制作、手工印刷和天然
添加产品
必须使用以下方法链接所有资产:
th:href="@{/assets/css/<your css file name here>}"
th:href=“@{/assets/css/}”
与JS和放置在
src/main/resources/static
下的任何其他静态资源相同,您可以包括管理/添加产品
视图的内容吗?我现在已经包括了它。请你再检查一下好吗@CK1为什么要注册此资源位置registry.addResourceHandler
?默认情况下,SpringBoot在类路径中的static
folderYes下查找资源,我知道。但在添加之前,我面临着同样的问题。在堆栈溢出中看到一些答案后,我添加了它。如果您使用的是SpringBoot和嵌入式tomcat,则不需要配置任何资源处理程序。让Springboot来管理它。您将静态资源放在src/main/resources/static
下根据您的建议,我已经删除了资源处理程序。我所有的资源都已经在src/main/resources/static
下了。我已附加我的文件夹结构。但是问题仍然存在。能否在configure(HttpSecurity http)
方法中添加.antMatchers(“/assets/**”).permitAll()
?还可以尝试使用localhost:8080/assets/css/flag icon.css直接打开资源,您看到了什么?