我正在尝试为安全路由/管理员配置Spring Security(仅允许访问管理员角色),但我想允许所有其他路由,而无需硬码文件中的所有路由。
是我的春季安全程序配置:
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");
如何允许所有路线并仅保护/管理员路线和孩子?
当我更改这条config
时 .mvcMatchers("/", "/login", "/registration", "/news/**").permitAll()
to
.mvcMatchers("/**").permitAll()
我的/管理员路线变得不安全,我可以在那里开放而无需登录。怎么办?
您应该只更改MVCMatchers
的顺序http
.authorizeRequests().mvcMatchers("/admin/**").hasAuthority("ADMIN").anyRequest()
.authenticated().mvcMatchers("/**").permitAll()
之后,如果将请求发送到" admin/〜",则会出现登录表单在另一种情况下,所有请求将通过