Spring Security避免为角色创建表
我正在学习春季安全。我已准备好登录系统,我想添加角色。我看过很多关于它的教程和文档,但我找不到我想要的 我不想为角色创建额外的表,因为我的表用户有一个名为“type”的列,我想使用它进行授权。该列的值可以是“个人”、“教师”或“组织”。因此,我想将角色系统基于该列,而不是基于名为“role”的表的一对多关系 我如何配置它 谢谢 已更新 我忘了,我用的是Spring数据。这是我正在使用的代码Spring Security避免为角色创建表,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我正在学习春季安全。我已准备好登录系统,我想添加角色。我看过很多关于它的教程和文档,但我找不到我想要的 我不想为角色创建额外的表,因为我的表用户有一个名为“type”的列,我想使用它进行授权。该列的值可以是“个人”、“教师”或“组织”。因此,我想将角色系统基于该列,而不是基于名为“role”的表的一对多关系 我如何配置它 谢谢 已更新 我忘了,我用的是Spring数据。这是我正在使用的代码 @Configuration @EnableWebSecurity public class Spring
@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);
}
}
您可以在Java配置中使用
密码编码器定义UserDetailsService
,如下所示:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired private PersonRepository personRepository;
@Override
@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(username -> {
Person person = personRepository.findByUsername(username);
if (person == null) throw new UsernameNotFoundException("Invalid user");
return new User(person.getUsername(),
person.getPassword(),
Collections.singleton(new SimpleGrantedAuthority(person.getType())));
})
.passwordEncoder(passwordEncoder())
}
// Rest of the configuration
}
在上面的例子中,我假设您有一个PersonResposition
可以访问您的用户信息。使用此userdetails服务
您将不需要您的AuthenticationProvider
。另外,User
驻留在org.springframework.security.core.userdetails
包中。我忘了说我在使用Spring数据,我将添加代码您的身份验证提供者在哪里
?也添加这个..好的,我添加了整个配置,您可以将提供程序视为“daoAuthenticationProvider”