【发布时间】:2018-02-02 21:49:46
【问题描述】:
在以下扩展 WebSecurityConfigurerAdapter 的类中,我覆盖了 configure(HttpSecurity) 方法。
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
public void configureAuth(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("fabio")
.password("123")
.roles("ADMIN")
.and()
.withUser("joe")
.password("123")
.roles("GUEST");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/post/list").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.logout();
}
}
有了这个我应该能够进入localhost:8080/post/list页面而不必提交用户登录,因为它有.permitAll(),但是当我尝试进入它时它总是提示登录页面之前,只有之后我输入以前的凭据,我可以查看它。我该如何解决这个问题?
控制器类
@RestController
@RequestMapping("/post")
public class HomeController {
@Secured("ROLE_GUEST")
@RequestMapping("/list")
public String list(){
return "list...";
}
@Secured("ROLE_USER")
@RequestMapping("/drafts")
public String drafts(){
return "drafts...";
}
@Secured({"ROLE_ADMIN","ROLE_USER"})
@RequestMapping("/add")
public String add(){
return "adding...";
}
}
【问题讨论】:
-
试试
.antMatchers("/post/list**").permitAll() -
不行
标签: java spring spring-security