2017-05-19 1 views
-1

내 웹 응용 프로그램에 스프링 보안을 구현했습니다.스프링 보안 - 역할 기반 액세스

역할 기반 액세스를 구성하려고합니다. "ROLE_ADMIN"역할을 가진 사용자 만 로그인 할 수 있습니다.

"Role"모델을 추가하고 데이터베이스에 테이블을 추가했습니다. "ROLE_USER"역할을 가진 사용자는 계속 로그인 할 수 있습니다.

@Override 
protected void configure(HttpSecurity http) { 
    try { 
     http.csrf().disable() 
       .authorizeRequests() 
       .antMatchers("/resources/**").hasRole("ROLE_ADMIN") 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/login") 
       .permitAll() 
       .and() 
       .logout() 
       .permitAll(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

고마워요!

편집 : 완전한 봄 보안 설정

@Configuration 
@EnableWebSecurity 
@ComponentScan(basePackageClasses = UserDetailsServiceImpl.class) 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

@Autowired 
private UserDetailsService userDetailsService; 

@Bean 
public BCryptPasswordEncoder bCryptPasswordEncoder() { 
    return new BCryptPasswordEncoder(); 
} 

@Override 
public void configure(WebSecurity web) { 
    web.ignoring().antMatchers("/css/**", "/js/**"); 
} 

@Override 
protected void configure(HttpSecurity http) { 
    try { 
     http.csrf().disable() 
       .authorizeRequests() 
       .antMatchers("/resources/**").hasRole("ADMIN") 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/login") 
       .permitAll() 
       .and() 
       .logout() 
       .permitAll(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

@Bean 
public DaoAuthenticationProvider authenticationProvider() { 
    DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); 
    authProvider.setUserDetailsService(userDetailsService); 
    authProvider.setPasswordEncoder(bCryptPasswordEncoder()); 
    return authProvider; 
} 

@Autowired 
public void globalSecurityConfiguration(AuthenticationManagerBuilder auth) { 
    try { 
     auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

} 

답변

1

당신은 WebMvcConfigurerAdapter을 확장하고 있습니까? 또한 hasRole는 "ROLE_"문서에서

함께 제공된 문자열 앞에 것이다

역할이 필요하기 (즉 USER, ADMIN 등). 자동으로 삽입되므로 "ROLE_"로 시작하면 안됩니다.

예 :

@SpringBootApplication 
public class SampleWebSecureJdbcApplication extends WebMvcConfigurerAdapter { 

    public static void main(String[] args) throws Exception { 
     new SpringApplicationBuilder(SampleWebSecureJdbcApplication.class).run(args); 
    } 

    @Configuration 
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter { 

     @Autowired 
     private DataSource dataSource; 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
       http 
        .authorizeRequests()  
        .antMatchers("/resources/**", "/signup", "/about").permitAll()  
        .antMatchers("/admin/**").hasRole("ADMIN") 
        .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") 
        .anyRequest().authenticated()  
        .and() 
        .formLogin().loginPage("/login").failureUrl("/login?error").permitAll() 
        .and() 
        .logout().permitAll(); 
     } 

     @Override 
     public void configure(AuthenticationManagerBuilder auth) throws Exception { 
      auth.jdbcAuthentication().dataSource(this.dataSource); 
     } 

    } 

} 
관련 문제