我在 db: 中有 3 个表USER(login,password)
,ROLE(role_name)
并且USER_ROLE_LINK (user_id, role_id)
我想为具有特定角色的用户授予对特定页面的访问权限。
我在这个类中配置了安全性:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(NoOpPasswordEncoder.getInstance())
.usersByUsernameQuery("select login, password, active from USER where login=?")
.authoritiesByUsernameQuery("select ur.user_id, ur.role_id from USER u inner join USER_ROLE_LINK ur on u.id = ur.user_id where u.login=?");
}
}
它工作正常,只有那些至少拥有一个角色的用户才能访问该应用程序。现在我想为具有特定角色的用户授予特定页面的访问权限,该怎么做?
我已经尝试过这个:antMatchers("/mypage").hasRole("MODERATOR")但它会抛出403 error. 我应该如何告诉从表的列Spring中查找用户的角色?ROLErole_name
回首忆惘然
相关分类