2017-01-03 2 views
0

Я настроил мои авторизации для страниц с использованием методы из WebSecurityConfigAdapter:Spring SECURITY- муравей согласовань с hasRole не работает

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    super.configure(auth); 
    auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder()); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    //super.configure(http); 
    http.servletApi().rolePrefix(""); 
    http 
     .logout() 
      .logoutRequestMatcher(new AntPathRequestMatcher("/logout")); 
    http.csrf().disable(); 
    http.authorizeRequests().antMatchers("/patient/mobile.mvc").hasRole("USER_PATIENT").and().formLogin(); 
    http.authorizeRequests().antMatchers("/registration/**").hasRole("USER_RECEPTIONIST").and().formLogin(); 
    http.authorizeRequests().antMatchers("/doctor/**").hasRole("USER_DOCTOR").and().formLogin(); 
} 

в моем UserDetailsService у меня есть следующие методы, чтобы дать пользователю свои полномочия:

public UserDetails loadUserByUsername(String userEmail) throws UsernameNotFoundException { 
    // 1: Check if exist receptionist with given name 
    Receptionist receptionist = loginService.getReceptionistByEmail(userEmail); 
    if (receptionist != null) { 
     return createReceptionistUserDetail(receptionist); 
    } 
... 
} 

private UserDetails createReceptionistUserDetail(Receptionist receptionist) { 
    List<GrantedAuthority> grantedAuthorities = new ArrayList<>(); 
    grantedAuthorities.add(new SimpleGrantedAuthority("USER_RECEPTIONIST")); 
    UserDetails ud = new User(receptionist.getEmail(), receptionist.getPasswordHash(), grantedAuthorities); 
    return ud; 
} 

У меня нет проблем с обычной проверкой подлинности и доступом, если роли не проверены, но когда я добавил доступ на основе ролей, любой пользователь не может получить доступ к своим веб-страницам.

Что может быть сделано неправильно?

ответ

0

Хорошо, кажется, что проблема в префиксе. Он по-прежнему не отключен с кодом выше.

Когда я изменил метод в методе для:

private UserDetails createReceptionistUserDetail(Receptionist receptionist) { 
    List<GrantedAuthority> grantedAuthorities = new ArrayList<>(); 
    grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER_RECEPTIONIST")); 
    UserDetails ud = new User(receptionist.getEmail(), receptionist.getPasswordHash(), grantedAuthorities); 
    return ud; 
} 

Пользователь может войти в систему без проблем. Я пытался найти простое решение, но решения здесь не так просто, что просто использовать префикс «ROLE_» ... :-)

Смежные вопросы