In the following class that extends WebSecurityConfigurerAdapter
i've overwritten the configure(HttpSecurity)
method.
@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();
}
}
With this i should be able to get to localhost:8080/post/list
page without having to commit to a user login since it has the .permitAll()
, but when i try to getting into it it always prompts the login page before, only after i enter the previous credentials i'm able to view it. How can i fix this ?
controller class
@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...";
}
}