【发布时间】:2016-06-08 21:34:51
【问题描述】:
我正在学习 Spring Security。我已经准备好登录系统,我想添加角色。我看过很多关于它的教程和文档,但我找不到我要找的东西。
我不想为 Roles 创建额外的表,因为我的表用户有一个名为“type”的列,我想用它来进行授权。该列的值可以是“人”、“教师”或“组织”。因此,我想将角色系统基于该列,而不是与名为“role”的表建立 OneToMany o ManyToMany 关系。
我该如何配置?
谢谢
更新
我忘了,我正在使用 Spring Data。这是我正在使用的代码
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
private AuthenticationProvider authenticationProvider;
@Autowired
@Qualifier("daoAuthenticationProvider")
public void setAuthenticationProvider(AuthenticationProvider authenticationProvider) {
this.authenticationProvider = authenticationProvider;
}
@Bean
public PasswordEncoder passwordEncoder(BCryptPasswordEncoder passwordEncoder){
return passwordEncoder;
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider(BCryptPasswordEncoder passwordEncoder,
UserDetailsService userDetailsService){
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
daoAuthenticationProvider.setUserDetailsService(userDetailsService);
return daoAuthenticationProvider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/h2-console").disable()
.authorizeRequests().antMatchers("/").authenticated()
.antMatchers("/console/**").permitAll()
.antMatchers("/static/**").permitAll()
.antMatchers("/profile").hasAuthority("PERSON")
.and().formLogin().loginPage("/login").permitAll()
.and().exceptionHandling().accessDeniedPage("/login")
.and().logout().permitAll()
http.headers().frameOptions().disable();
}
@Autowired
public void configureAuthManager(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception{
authenticationManagerBuilder
.jdbcAuthentication().authoritiesByUsernameQuery("select type from users where username = ?").and()
.authenticationProvider(authenticationProvider);
}
}
【问题讨论】:
-
你的
authenticationProvider在哪里?也添加它.. -
好的,我添加了整个配置,你可以看到提供者为“daoAuthenticationProvider”
标签: spring spring-mvc spring-security