Spring mvc 如何配置";“春季安全”;用于安全/管理URL,但允许所有其他URL?

Spring mvc 如何配置";“春季安全”;用于安全/管理URL,但允许所有其他URL?,spring-mvc,security,spring-boot,spring-security,Spring Mvc,Security,Spring Boot,Spring Security,我正在尝试为安全路由/管理员配置Spring安全性(仅允许管理员角色访问),但我想允许所有其他路由,而不需要硬编码配置文件中的所有路由 这是我的spring安全程序配置: http. authorizeRequests() .mvcMatchers("/", "/login", "/registration", "/news/**").permitAll() .mvcMatchers("/admin/**").hasAuthor

我正在尝试为安全路由/管理员配置Spring安全性(仅允许管理员角色访问),但我想允许所有其他路由,而不需要硬编码配置文件中的所有路由

这是我的spring安全程序配置:

  http.
        authorizeRequests()
            .mvcMatchers("/", "/login", "/registration", "/news/**").permitAll()
            .mvcMatchers("/admin/**").hasAuthority("ADMIN").anyRequest().authenticated()
        .and()
            .csrf().disable()
            .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error=true")
            .defaultSuccessUrl("/admin/news/")
            .usernameParameter("username")
            .passwordParameter("password")
        .and()
            .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/")
        .and()
            .exceptionHandling()
            .accessDeniedPage("/access-denied");
如何允许所有路由并仅保护/管理路由和子路由

当我更改这行配置时

.mvcMatchers(“/”、“/login”、“/registration”、“/news/**”).permitAll()

.mvcMatchers(“/**”).permitAll()

我的/管理员路径变得不安全,我可以在那里打开而无需登录。
如何做到这一点?

您只需更改MVC匹配器的顺序即可

http
.authorizeRequests().mvcMatchers("/admin/**").hasAuthority("ADMIN").anyRequest()
.authenticated().mvcMatchers("/**").permitAll()
之后,如果您向“admin/~”发送请求,则会出现登录表单
在另一种情况下,在我更改顺序并修复多个重定向问题后,所有请求都将通过

。我的所有链接都重定向到登录表单。