2016-02-25 60 views
1

我正在学习Spring Security。我准备好登录系统了,我想添加角色。我看过很多关于它的教程和文档,但找不到我要找的内容。春季安全避免为角色创建表

我不想为角色创建一个额外的表,因为我的表用户有一个名为“type”的列,我想用它来授权。该栏的价值可以是“人”,“老师”或“组织”。所以,我想在该列上建立角色系统,而不是与名为“role”的表进行OneToMany或ManyToMany关系。

我该如何配置?

感谢

修订

我忘了,我使用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); 
    } 




} 
+0

你的'authenticationProvider'在哪里?还有,.. –

+0

好吧,我添加了整个配置,你可以看到提供商为“daoAuthenticationProvider” – Giuliano

回答

1

您可以定义在Java配置一个PasswordEncoder一个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 
} 

在上面的例子中,我假定你有一个PersonRespository能够访问到您的用户信息。有了这个UserDetailsService你不需要你的AuthenticationProvider。另外,User驻留在org.springframework.security.core.userdetails包中。

+0

好吧,我已经尝试过,但仍然无法正常工作。 – Giuliano

+0

我忘了说我正在使用Spring Data,我将添加代码 – Giuliano