2016-02-25 57 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