Как добавить обработчик отказа в доступе в spring-security-javaconfig

Я использую библиотеку spring-security-javaconfig для весенней безопасности. Если бы я использовал файлы конфигурации xml, я бы использовал что-то вроде этого, чтобы определить пользовательскую страницу отказа в доступе:

<http auto-config="true">
    <intercept-url pattern="/admin*" access="ROLE_ADMIN" />
    <access-denied-handler ref="accessDeniedHandler"/>
</http>

Вот мой класс конфигурации безопасности:

@Configuration
@EnableWebSecurity
public class SecurityConfigurator extends WebSecurityConfigurerAdapter {

    @Override
    protected void registerAuthentication(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
        auth.inMemoryAuthentication().withUser("admin").password("password").roles("ADMIN");

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeUrls().antMatchers( "/admin").hasRole("ADMIN");
    }
}

person pChip    schedule 09.08.2013    source источник
comment
Обратите внимание, что многократный вызов inMemoryAuthentication() фактически создает несколько экземпляров InMemoryUserDetailsManager. Если вы не хотите связывать все методы в цепочку, вы можете сохранить вызов auth.inMemoryAuthentication() в переменной. Или вы можете использовать цепочку методов в качестве схемы в примерах github.com/SpringSource/spring-security-javaconfig/blob/master/   -  person Rob Winch    schedule 10.08.2013


Ответы (1)


Я полагаю, это должно помочь:

HttpSecurity http = ...
http.exceptionHandling().accessDeniedHandler(myAccessDeniedHandler);
person Jk1    schedule 09.08.2013