Spring Security: перенос самозакрывающегося xml-тега диспетчера аутентификации в конфигурацию Java

У меня есть довольно готовая конфигурация Spring Security 3.2 J2EE xml, которую я почти закончил конвертировать в конфигурацию Java.

Файл Before xml:

<sec:global-method-security pre-post-annotations="enabled" />
<sec:authentication-manager />

<sec:http pattern="/css/**" security="none" />
<sec:http pattern="/js/**" security="none" />
....
<sec:http auto-config="true" create-session="never" use-expressions="true>
    <sec:session-management session-fixation-protection="none" />
    <sec:jee mappable-roles="admin,user" />
    <sec:intercept-url pattern="/operations/admin/**" access="hasRole('ROLE_admin')" />
    <sec:intercept-url pattern="/**" access="permitAll" />
</sec:http>

Самозакрывающийся тег администратора аутентификации - это моя проблема. Он собирает PreAuthenticatedAuthenticationProvider, созданный тегом jee. Я не совсем уверен, как скопировать его в Java Config:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
@ImportResource("classpath:security-context.xml")
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter{
    @Override
    public void configure(WebSecurity web) throws Exception{
        web
            .ignoring.antMatchers("/css/**")
            .and()
            .ignoring.antMatchers("/js/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http
            .sessionManagement()
            .sessionFixation().none()
            .sessionCreationPolicy(SessionCreationPolicy.NEVER)
        .and()
        .csrf().disable()
        .jee()
            .mappableAuthorities("admin","user")
        .and()
        .authorizeRequests()
            .antMatchers("/operations/admin/**").hasAuthority("admin")
            .anyRequest.permitAll();
    }
}

Теперь это работает только потому, что я импортирую свой старый файл security-context.xml, в котором нет ничего, кроме тега authentication-manager.

Я поиграл с объявлением bean-компонента AuthenticationManagerBuilder, но кажется, что для работы все требует конкретной ссылки на AuthenticationProvider или UserDetailsService. Конструктор ProviderManager по умолчанию устарел.

Я знаю, что запись jee() добавляет PreAuthenticatedAuthenticationProvider к sharedObjects внутри HttpSecurity, поэтому я мог бы столкнуться с проблемой извлечения PreAuthenticatedAuthenticationProvider из sharedObjects для создания AuthenticationManager, если это необходимо, но кажется, что это должна быть простая Java аналог конфига для самозакрывающегося тега xml, который мне просто не хватает.

1 ответ

Решение

Можете ли вы попробовать это на вашем классе SpringSecurityConfig:-

@Autowired
public void registerGlobal(AuthenticationManagerBuilder auth) throws Exception {
   auth.authenticationProvider(new AuthenticationManagerBeanDefinitionParser.NullAuthenticationProvider());
}
Другие вопросы по тегам