java - Spring安全permitall()适用于@RestController但不适用于@Controller

标签 java spring spring-security

在我的应用程序中,我希望在没有身份验证的情况下允许使用一些 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";
    }

enter image description here

enter image description here 那么如何允许用户在没有身份验证的情况下查看这些 View 呢?

最佳答案

所以我想通了。发生的情况是,spring 允许在没有身份验证的情况下加载 View 文件,但不允许加载相关的 css 和 js 文件。所以我必须将它们添加到 Permitall() 模式中。

关于java - Spring安全permitall()适用于@RestController但不适用于@Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61629257/

相关文章:

java - Shiro 与 SpringSecurity

java - 在 Spring Security Oauth2 中使用 RemoteTokenServices 配置资源服务器

java - 面向对象设计 : Which is better practice?

java - 为什么我们不需要在Android中实例化LayoutInflater?

spring - 如何将多个对象添加到 ModelAndView 中?

java - Spring Singleton VS Singleton 设计模式——类加载器

spring - Autowire Jdbc 模板

java - 什么是 Java 中大型列表的最佳列表实现

java - 查询芝麻存储库时变量中没有值?

session - GRAILS:如何通过Spring Security核心插件获取当前登录用户的数量?