Referencing Spring Security configuration within Spring 3.1 Java Config

后端 未结 4 673
盖世英雄少女心
盖世英雄少女心 2021-02-04 21:12

I recently switched a majority of my Spring configuration to use the code based config in Spring 3.1. However, now that I\'ve switched, my Spring Security is not working correc

4条回答
  •  伪装坚强ぢ
    2021-02-04 21:44

    Do not use security namespace shortcuts and migrate all spring configuration from XML to Java. It will make fine tuning of your security much easier. I'm going to do it for our project soon after migrating to 3.1. You can find non-trivial almost plain-bean security XML config example here.

    edit: Finished config (linked above) migration. All config was put into one method deliberately to make it shorter and to demonstrate, that you don't need separate spring bean for every filter. Of course it's better to move complex init parts to separate methods (marked @Bean if necessary). You can find working example in the X509AnnotationTest.Config on the link above.

    @Bean
    public FilterChainProxy springSecurityFilterChain() throws Exception {
        // AuthenticationEntryPoint
        BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
        entryPoint.setRealmName("AppName Realm");
        // accessDecisionManager
        List voters = Arrays.asList(new RoleVoter(), new WebExpressionVoter());
        AccessDecisionManager accessDecisionManager = new AffirmativeBased(voters);
        // SecurityExpressionHandler
        SecurityExpressionHandler securityExpressionHandler = new DefaultWebSecurityExpressionHandler();
        // AuthenticationUserDetailsService
        UserDetailsByNameServiceWrapper authenticationUserDetailsService = new UserDetailsByNameServiceWrapper(authUserDetailService);
        authenticationUserDetailsService.afterPropertiesSet();
        // PreAuthenticatedAuthenticationProvider
        PreAuthenticatedAuthenticationProvider preAuthenticatedAuthenticationProvider = new PreAuthenticatedAuthenticationProvider();
        preAuthenticatedAuthenticationProvider.setPreAuthenticatedUserDetailsService(authenticationUserDetailsService);
        preAuthenticatedAuthenticationProvider.afterPropertiesSet();
        // AuthenticationManager
        List providers = Arrays.asList(preAuthenticatedAuthenticationProvider);
        AuthenticationManager authenticationManager = new ProviderManager(providers);
        // HttpSessionSecurityContextRepository
        HttpSessionSecurityContextRepository httpSessionSecurityContextRepository = new HttpSessionSecurityContextRepository();
        // SessionRegistry
        SessionRegistry sessionRegistry = new SessionRegistryImpl();
        // ConcurrentSessionControlStrategy
        ConcurrentSessionControlStrategy concurrentSessionControlStrategy = new ConcurrentSessionControlStrategy(sessionRegistry);
    
        // ConcurrentSessionFilter
        ConcurrentSessionFilter concurrentSessionFilter = new ConcurrentSessionFilter(sessionRegistry);
        concurrentSessionFilter.afterPropertiesSet();
        // SecurityContextPersistenceFilter
        SecurityContextPersistenceFilter securityContextPersistenceFilter = new SecurityContextPersistenceFilter(httpSessionSecurityContextRepository);
        // X509AuthenticationFilter
        X509AuthenticationFilter x509AuthenticationFilter = new X509AuthenticationFilter();
        x509AuthenticationFilter.setAuthenticationManager(authenticationManager);
        x509AuthenticationFilter.afterPropertiesSet();
        // RequestCacheAwareFilter
        RequestCacheAwareFilter requestCacheAwareFilter = new RequestCacheAwareFilter();
        // SecurityContextHolderAwareRequestFilter
        SecurityContextHolderAwareRequestFilter securityContextHolderAwareRequestFilter = new SecurityContextHolderAwareRequestFilter();
        // SessionManagementFilter
        SessionManagementFilter sessionManagementFilter = new SessionManagementFilter(httpSessionSecurityContextRepository, concurrentSessionControlStrategy);
        // ExceptionTranslationFilter
        ExceptionTranslationFilter exceptionTranslationFilter = new ExceptionTranslationFilter(entryPoint);
        exceptionTranslationFilter.setAccessDeniedHandler(new AccessDeniedHandlerImpl());
        exceptionTranslationFilter.afterPropertiesSet();
        // FilterSecurityInterceptor
        FilterSecurityInterceptor filterSecurityInterceptor = new FilterSecurityInterceptor();
        filterSecurityInterceptor.setAuthenticationManager(authenticationManager);
        filterSecurityInterceptor.setAccessDecisionManager(accessDecisionManager);
        LinkedHashMap> map = new LinkedHashMap>();
        map.put(new AntPathRequestMatcher("/**"), Arrays.asList(new SecurityConfig("isAuthenticated()")));
        ExpressionBasedFilterInvocationSecurityMetadataSource ms = new ExpressionBasedFilterInvocationSecurityMetadataSource(map, securityExpressionHandler);
        filterSecurityInterceptor.setSecurityMetadataSource(ms);
        filterSecurityInterceptor.afterPropertiesSet();
        // SecurityFilterChain
        SecurityFilterChain chain = new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**"),
                concurrentSessionFilter,
                securityContextPersistenceFilter,
                x509AuthenticationFilter,
                requestCacheAwareFilter,
                securityContextHolderAwareRequestFilter,
                sessionManagementFilter,
                exceptionTranslationFilter,
                filterSecurityInterceptor);
        return new FilterChainProxy(chain);
    }
    

提交回复
热议问题