在我的应用程序中,我希望在没有身份验证的情况下允许使用一些 API。所以我在 Permitall() 中添加了该模式。但这仅当这些模式位于 @RestController 注释内时才有效。如果这些模式在@Controller注释中(我想返回 View ),Spring会要求进行身份验证,即使它们在permitall()下。
WebsecutiryConfig 类
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().
.authorizeRequests().antMatchers("/pattern1", "/pattern2").permitAll()
.anyRequest().authenticated()
}
带有@RestController注解的类
@RestController
public class RESTClass {
@GetMapping("/pattern1")
public String hello() {
return "my response";
}
带有@Controller注解的类
@Controller
public class ControllerClass {
@GetMapping("/pattern2")
public String hello(Model model) {
return "my view";
}
最佳答案
所以我想通了。发生的情况是,spring 允许在没有身份验证的情况下加载 View 文件,但不允许加载相关的 css 和 js 文件。所以我必须将它们添加到 Permitall() 模式中。
关于java - Spring安全permitall()适用于@RestController但不适用于@Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61629257/