我使用Spring Security创建了一个项目。
在configure(HttpSecurity http)
中,我仅为USER设置了对"/home"
的访问权限,但登录后,它显示:
403禁止
我创建了Entity class named User implementing UserDetails
,然后在getAuthorities()
中重新运行了Arrays.asList(new SimpleGrantedAuthority("USER"));
对于http对象,我尝试直接使用.hasRole('USER')
方法而不是.access("hasRole('USER')")
来解决问题,
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.authorizeRequests()
.antMatchers("/home")
.access("hasRole('USER')")
.antMatchers("/","/**").access("permitAll")
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}
答案 0 :(得分:3)
您需要使用授权而不是角色。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/home").hasAuthority("USER")
.antMatchers("/","/**").access("permitAll")
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}