2016-07-12 3 views
1

異なる役割を持つ異なるユーザーからアクセス可能なWebアプリケーションを作成します。一部のロールはすべてのページを表示しますが、一部は一部のみを参照します。Java Springセキュリティが認識されないロール

SecurityConfigには次の設定がありますが、機能しません。

@Configuration 
@EnableWebSecurity 
@Import(value = { SecurityWebApplicationInitializer.class }) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    DataSource dataSource; 

    @Autowired 
    private AuthenticationService authenticationService; 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     Md5PasswordEncoder encoder = new Md5PasswordEncoder(); 
     auth.userDetailsService(authenticationService).passwordEncoder(encoder); 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers("/assets/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

    http.authorizeRequests() 
    .antMatchers("/login", "/page1").permitAll() 
    .antMatchers("/**").access("hasRole('RADMIN')") 
    .antMatchers("/login", "/page2").access("hasRole('ADMIN')") 
    .antMatchers("/page2").hasAnyRole("RADMIN", "ADMIN") 
    .and().formLogin() 
    .and().exceptionHandling().accessDeniedPage("/403"); 

    } 

} 

私はhasAnyAuthority()からhasAnyRole()、全く影響を変更しようとしました。私が作るすべての変更は、ADMINにログインさせることはほとんどありません(彼は/ 403または/ 404しか見ることができません)。

+0

標準春の治安当局はROLE_' 'で始まるので、これらは' ROLE_RADMIN'する必要があり、データベース名が変更されたコミットのいずれかでのように思えますあなた自身の['AccessDecisionVoter'](http://docs.spring.io/autorepo/docs/spring-security/4.1.0.RELEASE/apidocs/org/springframework/security/access/)を定義していない限り、' ROLE_ADMIN'と 'ROLE_ADMIN' AccessDecisionVoter.html)。 – Andreas

+0

これは、違いはありません... – mariobgr

+0

あなたは 'ROLE_ADMIN'がユーザに割り当てられていることを確認しましたか? – Andreas

答えて

0

私はSpring-Security 4.0.1.RELEASEを使用していますが、usersauthoritiesの2つの重要なテーブルがあります。表authoritiesには正確に2つの列(ユーザー名、権限)があり、役割は接頭辞ROLE_である必要があります。 Andreasのように、ROLE_ADMIN & ROLE_RADMINが指摘されています。

例えば、それはどんな違いが、その後、追加のページや役割を追加しますない以下のようなあなたの認証をシンプルに保つようにしてくださいと表示さ

@Override 
    protected void configure(HttpSecurity http) throws Exception { 

     http.authorizeRequests(). 
     anyRequest().authenticated().and().formLogin().loginPage("/login.html") 
     .loginProcessingUrl("/login").permitAll().and().logout().logoutSuccessUrl("/") 
     .logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll().and().exceptionHandling() 
     .accessDeniedPage("/403.html"); 
    } 
1

まあ問題は、それが得ることができるほど愚かでした。

は、このように認証するための何の役割(ROLE_NONE)はありませんでした...

関連する問題