【发布时间】:2015-08-12 08:02:26
【问题描述】:
我正在尝试在某些控制器上使用与 @PreAuthorize("permitAll") 匹配的 url(基于蚂蚁),即
@Controller
@RequestMapping("/register")
public class RegistrationController {
...
@PreAuthorize("permitAll")
@RequestMapping(method = RequestMethod.GET)
public String register() { ... }
安全配置:
@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
我还尝试将 @EnableGlobalMethodSecurity 添加到我的 MVC 配置中:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MvcConfig extends WebMvcConfigurerAdapter { ... }
但这没有效果
但是,在点击 /register 时仍然提示我进行身份验证。如果我将“/register”添加到蚂蚁匹配器中,它可以工作,即 .antMatchers("/", "/register").permitAll()
我在这里缺少什么? @PreAuthorize 似乎对我的控制器没有影响
【问题讨论】:
-
我应该澄清控制器上的 @PreAuthorize("hasRole('ADMIN')") 确实有效:非管理员会收到 403 错误。问题特别是如何使用 @PreAuthorize 注释覆盖蚂蚁匹配器
标签: spring-mvc spring-security