1

Хочу запретить ходить на страницы /admin/** кроме пользователей с ролями ADMIN, и /user/** только для USER. Перечитал куча мануалов но так и не понял что следует дописать:

  @Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @Autowired
    public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(userDetailsService)
                .passwordEncoder(getShaPasswordEncoder());
    }





    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf()
                .disable()
                .authorizeRequests()
                .antMatchers("/resources/**", "/**").permitAll()
                .anyRequest().permitAll()
                .and();

        http.formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/j_spring_security_check")
                .failureUrl("/login?error")
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                .permitAll();

        http.logout()
                .permitAll()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login?logout")
                .invalidateHttpSession(true);


    }

1 ответ 1

3

Ну, например, вот так попробуйте:

.antMatchers("/user/**").access("hasRole('ROLE_USER')")
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")

И советую, почитать что-нибудь, хотя бы даже вот это: https://habrahabr.ru/post/226791/ (а лучше конечно документацию по Spring Security).

3
  • Это в какой http? Если вас не затруднит напишите куда нужно это прописать
    – amigojoe
    10 мая 2016 в 17:49
  • Не получается. Всё равно могу зайти под любым
    – amigojoe
    10 мая 2016 в 18:43
  • Разобралсся. спасибо
    – amigojoe
    11 мая 2016 в 7:13

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.